Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining Current Call Stack (For Diagnostic Purposes)

For diagnostic purposes I sometimes need to store the call stack that lead to a given state transition (such as granting a lock, committing a transaction, etc.) so that when something goes wrong later I can find out who originally triggered the state transition.

Currently, the only way I am aware of to retrieve the call stack looks like the following code snippet, which I consider terribly ugly:

StackTraceElement[] cause; try {   throw new Exception(); } catch (Exception e) {   cause = e.getStackTrace(); } 

Does somebody know of a better way to accomplish this?

like image 851
Thilo-Alexander Ginkel Avatar asked Apr 01 '09 16:04

Thilo-Alexander Ginkel


People also ask

What is current call stack?

In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or machine stack, and is often shortened to just "the stack".

What are functions should be used to get a printout of the current call stack?

Description. By default traceback() prints the call stack of the last uncaught error, i.e., the sequence of calls that lead to the error. This is useful when an error occurs with an unidentifiable error message. It can also be used to print the current stack or arbitrary lists of calls.

How do you read a call stack?

Call stack is set of lines, which is usually read from top to bottom - meaning moving from current locations to callers. The bottom line was executed first. The top line is executed last and it is the current routine.

What is the difference between call stack and stack trace?

A call stack is typically "the current stack of operations" - i.e. while it's running. A stack trace is typically a copy of the call stack which is logged at some sort of failure, e.g. an exception.


1 Answers

I think you can get the same thing with:

StackTraceElement[] cause = Thread.currentThread().getStackTrace(); 
like image 84
bruno conde Avatar answered Oct 02 '22 07:10

bruno conde