How to show the caller back to Julia?

Is there a way to get the file / name / line information for the caller Julia?

I found this way to get information about the stacktrace , and if the caller is another function (but not the main context), I get a file: 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( )

      

It shows:

$ 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

      

I would love just the second frame (the one calling traceit ()), and would also love the function name if available.

+3


source to share


1 answer


If you execute @show bt

internally traceit

, you only find a list of pointers in it, each corresponding to a single stack of stacks. These stack frames, which come from julia code (not C), are rendered show_backtrace

.

You can call Profile.lookup(uint(bt[1]))

to extract file / function / line information from each element:



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   

      

You probably want to ignore all elements with fromC == true

.

+5


source







All Articles