How to know where a specific function is called without resorting to debugger?

I’m going through a large julia package where I’m particularly interested in how the computing is done in one specific function (I’m certain that this function is called, as I added a print line therein so that if the function is called it prints something). I tried to use ASTInterpreter2 to help me identify where the function is called. Unfortunately, the package is so complicated that ASTInterpreter2 fails to work (the issue is just too complicated to be filed with a reasonably small MWE). I also tried to add println(backtrace()) in that function, expecting that the result of backtrace() can give me some clue. But what backtrace() returns is just too hard to interpret for an average non-geek like myself.

Thanks in advance for any comments.

stacktrace() :slight_smile:

3 Likes

Thanks a lot! This is pretty much what I’m after. One quick question - stackback() seems to only print the names of the files in the stack without giving much information about the locations of the files. Is there a way to let stackback() elaborate a little by printing also the directory of each file involved?

You can also try TraceCalls.jl

trace = @trace ModuleName ...computation...
filter_lineage(tr -> tr.func==function_youre_interested_in, trace; keep_descendents=false)

This will show the callers of function_youre_interested_in, in tree form.

2 Likes

Thanks! I’ve tried the package. But here is what I just saw:

julia> trace = @trace FastSummation ...computation...
ERROR: syntax: "..." expression outside call

Can you re-list the commands that you just gave by capitalizing the keywords I should change for my own case? Thanks!

TraceCalls returns a tree of all the function calls that happened during the execution of ...computation... (replace it with whatever code you want to execute). There are many examples in the manual. filter_lineage(tr -> tr.func==foo, trace; keep_descendents=false) will get rid of all the function calls in the tree except foo, its callers, its callers’ callers, etc. It won’t give you very different information than stacktrace(), but it will look nicer, and traces are very manipulable objects. Plus, you don’t have to modify the code you’re exploring.

It should be true that only files in base have relative paths, while all of your files will be listed with absolute paths. I think there’s a function (functionloc, iirc) that’ll try to distinguish the two for you (used by less and edit and such)

Yes, you’re right. It’s functionloc and it gives the location of method definition. Thanks for mentioning this function. I didn’t know it until now.