How to get a stack trace of the current function stack?

To troubleshoot, I would like to be able to find and print the caller's stack of the current function. I tried the following:

/******************************************************************************* * * * * xxxTracePrint - stack trace print function * * * * RETURNS: OK or ERROR * */ static void xxxTracePrint ( INSTR *caller, int func, int nargs, int *args ) { char buf [250]; int ix; int len = 0; len += sprintf (&buf [len], "%#10x: %#10x (", (int)caller, func); for (ix = 0; ix < nargs; ix++) { if (ix != 0) len += sprintf (&buf [len], ", "); len += sprintf (&buf [len], "%#x", args [ix]); } len += sprintf (&buf [len], ")\n"); printf (buf); } /******************************************************************************* * * * * xxxTrace - stack trace * * * * RETURNS: OK or ERROR * */ int xxxTrace(int tcb) { REG_SET regs; if (tcb == 0) return (ERROR); taskRegsGet (tcb, &regs); trcStack (&regs, (FUNCPTR) xxxTracePrint, tcb); return (OK); } void DbgTest(void) { xxxTrace(taskIdSelf()); } 

but I get:

 JPAX-DP> DbgTest trcStack aborted: error in top frame value = 0 = 0x0 

Is it possible? How can i do this? I saw that for taskRegsGet () they say:

This procedure only works well if the task is known to be stable, non-fulfillment. For example, introspection is impractical because the results are unpredictable.

But what other method should I apply?

diab and cpu arch powerpc compiler

+5
source share
2 answers

You mentioned that taskRegsGet () mentions that it is not recommended to call the current task. However, I saw someone using taskDelay (1) with the comment "keep context power". I can’t take responsibility for this and I don’t know how reliable it is or what side effects it can have, but it can help get the right information about the current task:

 taskDelay (1); /* Force context save */ taskRegsGet (0, &regs); /* 0 task-id for myself */ trcStack (&regs, NULL, 0); /* NULL function pointer for default print fcn, 0 task-id for myself */ 
+1
source

If your compiler is GCC and the calling conventions of your architecture allow this (x86 is the first thing that comes to mind), I would recommend using __builtin_return_address (unsigned int level). More information can be found here: https://gcc.gnu.org/onlinedocs/gcc/Return-Address.html .

+1
source

Source: https://habr.com/ru/post/1269274/


All Articles