Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exported convenience function to get a callstack #3744

Closed
amitmurthy opened this issue Jul 18, 2013 · 15 comments
Closed

Exported convenience function to get a callstack #3744

amitmurthy opened this issue Jul 18, 2013 · 15 comments
Labels
domain:error handling Handling of exceptions by Julia or the user status:help wanted Indicates that a maintainer wants help on an issue or pull request

Comments

@amitmurthy
Copy link
Contributor

Currently, in case of errors, the callstack is only printed and displayed by the REPL.

It will be good to have an exported function(s) that can a) print the callstack when called and b) return a stringified version of the same.

See https://groups.google.com/d/topic/julia-users/S485_5jG2Nw/discussion

@Keno
Copy link
Member

Keno commented Jul 18, 2013

Base.show_backtrace(STDOUT,backtrace())
and

buf = IOBuffer()
Base.show_backtrace(buf,backtrace())
takebuf_string(buf)

?

@JeffBezanson
Copy link
Sponsor Member

Also backtrace and catch_backtrace for getting the traces.

@amitmurthy
Copy link
Contributor Author

backtrace and catch_backtrace are already exported.

Exported helper functions can be

How about callstack() = Base.show_backtrace(STDOUT, catch_backtrace()) and scallstack for the string variant?

For a user point of view, typical use would be in a catch block to either log to a file, or display it.

@Keno
Copy link
Member

Keno commented Jul 21, 2013

How about this: We export show_backtrace make both arguments optional (defaulting to STDOUT and backtrace()) respectively. Then you can get the string variant using:

sprint(show_backtrace)

@amitmurthy
Copy link
Contributor Author

We will need catch_backtrace() in a catch block, right?

@Keno
Copy link
Member

Keno commented Jul 21, 2013

if you want the backtrace of the exception caught, yes.

@amitmurthy
Copy link
Contributor Author

Exporting show_backtrace with both arguments optional is good. However, I would still prefer a less verbose, convenience function for the typical user code scenario (server side code) - i.e., logging caught exceptions.

@Keno
Copy link
Member

Keno commented Jul 21, 2013

Well, I'm open to suggestions for names, but I think sprint(show_backtrace,catch_backtrace()) isn't all that bad. scallstack seems ambiguous since you don't know which one to print (the caught one or the current one).

@amitmurthy
Copy link
Contributor Author

How about just callstack() which only returns the stringified catch trace. This will cover the typical scenario.
A typical debug statement to stdout will be println("Caught Exception $e at xxxx, (state: $var1, $var2) : ", callstack() ) in user code

For other scenarios, people can use sprint(show_backtrace,backtrace()) or show_backtrace(backtrace())

@Keno
Copy link
Member

Keno commented Jul 21, 2013

Yeah, I guess that's fine with me. I still don't think that name is ideal, but oh well. @JeffBezanson ?

@Keno
Copy link
Member

Keno commented Sep 23, 2013

Bump.

@ssfrr
Copy link
Contributor

ssfrr commented Jun 24, 2014

What if backtrace() and catch_backtrace() returned a BackTrace type, rather than just an array of Ptr{None}?

That way we can define the usual show(bt::BackTrace), etc. It seems like the existence of Base.show_backtrace is a good example of the "underscores in function names are a code smell" guideline.

@ivarne
Copy link
Sponsor Member

ivarne commented Jun 24, 2014

@ssfrr +1 for that idea.
There are probably not many uses of the raw pointer array anyway, so a simple wrapping immutable is probably not a hugely breaking change. BackTrace could also implement methods in order to behave like the original Array{Ptr{None}} (getindex, length, start).

@ihnorton ihnorton added status:help wanted Indicates that a maintainer wants help on an issue or pull request domain:error handling Handling of exceptions by Julia or the user labels Jul 23, 2015
@spurll
Copy link
Contributor

spurll commented Jan 11, 2016

Happened upon this while debugging this PR. Seems relevant: #14469

@KristofferC
Copy link
Sponsor Member

I think this is fixed with the stacktrace() function that gets printed as:

julia> stacktrace()
4-element Array{StackFrame,1}:
 eval(::Module, ::Expr) at REPL.jl:3                                             
 eval_user_input(::Any, ::Base.REPL.REPLBackend) at REPL.jl:69                   
 macro expansion at REPL.jl:100 [inlined]                                        
 (::getfield(Base.REPL, Symbol("##1#2")){Base.REPL.REPLBackend})() at event.jl:9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:error handling Handling of exceptions by Julia or the user status:help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

No branches or pull requests

9 participants