What's the difference between a stack-trace and a back-trace?
Asked Answered
D

1

29

I really though I'd find an answer online, but I couldn't. Is there any difference at all? People say a 'backtrace' is generated upon something throwing an exception, while a stack trace is a list of method calls from the point when the application was started to the point where the exception was thrown. If we supposed a stack-trace as an array, then the last element would be the method where the exception was thrown. Would it be the reverse case for a back-trace? In a programming language like Ruby, for example, if we have:

begin
  raise 1
rescue
  p $!.backtrace ; p caller(0) #=> displays the back-trace, then the stack-trace
end

They will output 2 different arrays, which suggests to me there's something fundamentally different about them.

Delouse answered 20/10, 2014 at 13:41 Comment(0)
K
25

"Backtrace", "stack trace", "stack backtrace", etc. are names for the same thing. "Backtrace" specifically likely comes from the Linux tool of the same name. A stack trace doesn't refer to exceptions only - the current state of the program's call stack can always be displayed as a stack trace (which backtrace does, but so do many debugger views and tools). It's just helpful to output a stack trace during exceptions or errors.

Edit: Whoops, thought I was still in the 'C' tag - but looking at the documentation, both Exception.backtrace and caller should hold arrays where the top of the call stack is first in the array. There look to be differences in how deep they go and some formatting.

Koontz answered 20/10, 2014 at 13:47 Comment(3)
Linux barely existed when Ruby was created, I doubt that any terms in Ruby are derived from there. Ruby was created February 1993, Linux sometime in the summer of 1991. SLS, the very first Linux distribution was just 5 months old and buggy as hell, when Ruby started.Glazer
@JörgWMittag Speculation on my part and you're probably right, though that depends on when Exception.backtrace was added in the first place. It's more likely to have been the popular term at the time since GNU tools tend to use that term (eg. GDB, and the backtrace tool, which existed on Unix first). Like I said - here by accident! I'm surprised that Ruby is that old actually.Koontz
Conceived on February 24th, 1993, published in 1995, 1.0 on Christmas 1996.Glazer

© 2022 - 2024 — McMap. All rights reserved.