/* Dumps the current stack trace to stderr. */ staticvoid __attribute__((noinline)) test_dump_stack(void); staticvoid test_dump_stack(void)
{ /* * Build and run this command: * * addr2line -s -e /proc/$PPID/exe -fpai {backtrace addresses} | \ * cat -n 1>&2 * * Note that the spacing is different and there's no newline.
*/
size_t i;
size_t n = 20; void *stack[n]; constchar *addr2line = "addr2line -s -e /proc/$PPID/exe -fpai"; constchar *pipeline = "|cat -n 1>&2"; char cmd[strlen(addr2line) + strlen(pipeline) + /* N bytes per addr * 2 digits per byte + 1 space per addr: */
n * (((sizeof(void *)) * 2) + 1) + /* Null terminator: */
1]; char *c = cmd;
n = backtrace(stack, n); /* * Skip the first 2 frames, which should be test_dump_stack() and * test_assert(); both of which are declared noinline. Bail if the * resulting stack trace would be empty. Otherwise, addr2line will block * waiting for addresses to be passed in via stdin.
*/ if (n <= 2) {
fputs(" (stack trace empty)\n", stderr); return;
}
c += sprintf(c, "%s", addr2line); for (i = 2; i < n; i++)
c += sprintf(c, " %lx", ((unsignedlong) stack[i]) - 1);
c += sprintf(c, "%s", pipeline); #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-result"
system(cmd); #pragma GCC diagnostic pop
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.