如何在朱莉娅回溯中显示来电者?

Pee*_*oot 4 julia

有没有办法获取Julia函数调用者的文件/名称/行信息?

我发现这种方式来获取一些堆栈跟踪信息,如果调用者是另一个函数(但不是主上下文),我得到文件:line info:

module pd

   global g_bTraceOn = true

   export callerOfTraceIt

   function callerOfTraceIt()
     traceit( "hi" ) ;
   end

   function traceit( msg )
      global g_bTraceOn

      if ( g_bTraceOn )
         bt = backtrace() ;
         s = sprint(io->Base.show_backtrace(io, bt))

         println( "debug: $s: $msg" )
      end
   end
end

using pd

callerOfTraceIt( )
Run Code Online (Sandbox Code Playgroud)

由此可见:

$ julia bt.jl
debug:
 in traceit at C:\cygwin64\home\Peeter\julia\HarmonicBalance\bt.jl:15
 in callerOfTraceIt at C:\cygwin64\home\Peeter\julia\HarmonicBalance\bt.jl:8
 in include at boot.jl:245
 in include_from_node1 at loading.jl:128
 in process_options at client.jl:285
 in _start at client.jl:354: hi
Run Code Online (Sandbox Code Playgroud)

我真的很喜欢第二帧(traceit()的调用者),并且如果它可用,也会喜欢函数名.

tho*_*oly 5

如果你在@show bt里面做traceit,你会发现它只是一个指针列表,每个指针对应一个堆栈帧.那些来自julia代码(而不是C)的堆栈帧显示为show_backtrace.

您可以调用Profile.lookup(uint(bt[1]))从每个元素中提取文件/函数/行信息:

julia> Profile.lookup(uint(bt[1]))
LineInfo("rec_backtrace","/home/tim/src/julia-old/usr/bin/../lib/libjulia.so",-1,true,140293228378757)

julia> names(Profile.LineInfo)
5-element Array{Symbol,1}:
 :func 
 :file 
 :line 
 :fromC
 :ip   
Run Code Online (Sandbox Code Playgroud)

您可能希望忽略所有元素fromC == true.