Skip to content

Commit

Permalink
libbacktrace: better backtrace_print when no debug info
Browse files Browse the repository at this point in the history
Fixes #59

	* print.c (print_syminfo_callback): New static function.
	(print_callback): Call backtrace_syminfo if there is no function
	or file name.
  • Loading branch information
ianlancetaylor committed Jul 18, 2024
1 parent 8e32931 commit 9e10af0
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions print.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,39 @@ struct print_data
FILE *f;
};

/* Print errors to stderr. */

static void
error_callback (void *data, const char *msg, int errnum)
{
struct print_data *pdata = (struct print_data *) data;

if (pdata->state->filename != NULL)
fprintf (stderr, "%s: ", pdata->state->filename);
fprintf (stderr, "libbacktrace: %s", msg);
if (errnum > 0)
fprintf (stderr, ": %s", strerror (errnum));
fputc ('\n', stderr);
}

/* Print one level of a backtrace if we couldn't get a file or function name.
Use syminfo to try to get a symbol name. */

static void print_syminfo_callback (void *data, uintptr_t pc,
const char *symname, uintptr_t symval,
uintptr_t symsize ATTRIBUTE_UNUSED)
{
struct print_data *pdata = (struct print_data *) data;

if (symname == NULL)
fprintf (pdata->f, "0x%lx ???\n\t???:0\n", (unsigned long) pc);
else
fprintf (pdata->f, "0x%lx ???\n\t%s+0x%lx:0\n",
(unsigned long) pc,
symname,
pc - symval);
}

/* Print one level of a backtrace. */

static int
Expand All @@ -55,6 +88,13 @@ print_callback (void *data, uintptr_t pc, const char *filename, int lineno,
{
struct print_data *pdata = (struct print_data *) data;

if (function == NULL && filename == NULL)
{
backtrace_syminfo (pdata->state, pc, print_syminfo_callback,
error_callback, data);
return 0;
}

fprintf (pdata->f, "0x%lx %s\n\t%s:%d\n",
(unsigned long) pc,
function == NULL ? "???" : function,
Expand All @@ -63,21 +103,6 @@ print_callback (void *data, uintptr_t pc, const char *filename, int lineno,
return 0;
}

/* Print errors to stderr. */

static void
error_callback (void *data, const char *msg, int errnum)
{
struct print_data *pdata = (struct print_data *) data;

if (pdata->state->filename != NULL)
fprintf (stderr, "%s: ", pdata->state->filename);
fprintf (stderr, "libbacktrace: %s", msg);
if (errnum > 0)
fprintf (stderr, ": %s", strerror (errnum));
fputc ('\n', stderr);
}

/* Print a backtrace. */

void __attribute__((noinline))
Expand Down

0 comments on commit 9e10af0

Please sign in to comment.