调试器不会从调用函数返回到被调用函数

时间:2014-12-22 01:14:29

标签: r rstudio

我已经获取了我的代码并且正在对下面的代码进行调试(myfun1)。如图所示,我在第13行设置了断点。enter image description here

我的代码如下

myfun1 = function()
{
  print("This is myfun1, before calling the myfun2()")
  myfun2()
  print("This is myfun1, after calling the myfun2()")
  print("Does the debugger come back to this point")
}

myfun2 = function()
{
  print("This is myfun2, before calling the myfun3()")
  myfun3()
  print("This is  myfun2, after calling the myfun3()")
  print("This is the last but one line of myfun2()")
  print("This is the last line of myfun2()")
}

myfun3 = function()
{
  print("This is myfun3")
}

这些是我用于调试的步骤序列

  1. 我从myfun1开始,然后做了' s'在line4上进入myfun2()
  2. 调试器直接将我带到第13行,因为我有一个断点 设置
  3. 现在我做了' n'在myfun2()中有几次带我去了 后续行,即line14和line15 resp。
  4. 但是,如果我这样做,那就是' n'在第15行,我希望它回到myfun1()。但是,它只是退出(我能够让它回到myfun1() 通过在调用myfun2()之后在myfun1中设置断点(比如在第5行或第6行),但是不应该 即使否则返回?)
  5. 请参阅下面的调试日志

    > debugSource('~/Desktop/foo1.R')
    > debug(myfun1)
    > myfun1()
    debugging in: myfun1()
    debug at ~/Desktop/foo1.R#2: {
        print("This is myfun1, before calling the myfun2()")
        myfun2()
        print("This is myfun1, after calling the myfun2()")
        print("Does the debugger come back to this point")
    }
    Browse[2]> c
    [1] "This is myfun1, before calling the myfun2()"
    [1] "This is myfun2, before calling the myfun3()"
    [1] "This is myfun3"
    Called from: eval(expr, envir, enclos)
    Browse[1]> n
    debug at ~/Desktop/foo1.R#13: print("This is  myfun2, after calling the myfun3()")
    Browse[2]> n
    [1] "This is  myfun2, after calling the myfun3()"
    debug at ~/Desktop/foo1.R#14: print("This is the last but one line of myfun2()")
    Browse[2]> n
    [1] "This is the last but one line of myfun2()"
    debug at ~/Desktop/foo1.R#15: print("This is the last line of myfun2()")
    Browse[2]> n
    [1] "This is the last line of myfun2()"
    [1] "This is myfun1, after calling the myfun2()"
    [1] "Does the debugger come back to this point"
    exiting from: myfun1()
    

1 个答案:

答案 0 :(得分:1)

我假设RStudio的GUI只是R中基本调试功能的包装器。通过单击边距来设置断点等同于调用setBreakpoint().

这些函数不合时宜地调用trace(),它在函数级别上运行。也就是说,跟踪器用一个能够跟踪的新函数替换当前函数;并且该替换函数包括on.exit调用以在函数完成时停止跟踪。因此,在myfun2退出后,tacser会被禁用,因此您无法在调用myfun2myfun2)的函数内部进行调试。

如果需要在更高级别进行调试,请将断点设置为更高级别。我不确定您是否可以在RStudio GUI中更改调试器,但如果您自己致电trace(),则可以拨打recover()而不是browser(),这样您就可以跳转到调用堆栈的不同部分。