2015-01-09 3 views
3

줄리아 기능 호출자의 파일/이름/라인 정보를 가져 오는 방법이 있습니까?Julia 백 트레이스에서 발신자를 표시하는 방법은 무엇입니까?

나는 this way to get some stacktrace info을 발견하고, 호출자가 다른 함수 (하지만 주요 상황) 인 경우 I 파일 수 : 라인 정보 :

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() 

이 보여줍니다

$ 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 

내가 좋겠를 정말 두 번째 프레임 (traceit()의 호출자)을 좋아하고 함수 이름이 있으면 사용할 수도 있습니다.

답변

5

안에 @show bt을 넣으면, 그저 하나의 스택 프레임에 해당하는 포인터 목록 일뿐입니다. 줄리아 코드 (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 

당신은 가능성이 fromC == true 모든 요소를 ​​무시해야합니다.

+0

감사합니다. Tim. 다음은 이것을 사용하는 방법입니다. https://github.com/peeterjoot/julia/blob/master/HarmonicBalance/pd.jl ... 내 첫 번째 줄리아 코드 중 일부는 일반적으로 내 새로운 언어로 된 첫 번째 코드. –

관련 문제