Re: backtrace_symbols() not showing line numbers, and small patch for backtrace.3 manpage.

2020-07-20 Thread Stuart Henderson
On 2020-07-17, Julian Smith  wrote:
> For me, backtrace_symbols() does not show line-numbers. Is this the
> intended behaviour?

It is working as shown in upstream's example:

https://github.com/conformal/backtrace/wiki

So I'd say this is as expected.

> I'm using:
> OpenBSD jules-obsd 6.7 GENERIC.MP#182 amd64
> libexecinfo-0.3p2v0
>
> With 'cc -g -Wl,--export-dynamic', i'm getting backtraces like:
>
> 0x69854ee369  at ./foo.c.exe
> 0x69854ee477  at ./foo.c.exe
> 0x69854ee4be  at ./foo.c.exe
> 0x69854ee13b <__start+315> at ./foo.c.exe
>
> [gdb can show line numbers on the same executable, so the information
> is definitely in the executable somewhere.]
>
> I'm testing with this code:
>
>
> #include 
> #include 
> #include 
> #include 
>
> void bar()
> {
> void*   bt[20];
> char**  strings;
> int i;
> int d;
> 
> d = backtrace(bt, sizeof(bt) / sizeof(bt[0]));
> if (d == -1)
> errx(1, "backtrace");
>
> strings = backtrace_symbols(bt, d);
>
> if (strings == NULL)
> errx(1, "backtrace_symbols");
>
> for (i = 0; i < d; i++)
> printf("%s\n", strings[i]);
>
> free(strings);
> }
>
> void foo(void)
> {
> bar();
> }
>
> int main(void)
> {
> foo();
> return 0;
> }
>===
>
> Build with:
> cc -g -o foo.c.exe foo.c -I /usr/local/include -g -L /usr/local/lib
> -lexecinfo -Wl,--export-dynamic
>
> Run:
> ./foo.c.exe
>
> Output:
> 0x69854ee369  at ./foo.c.exe
> 0x69854ee477  at ./foo.c.exe
> 0x69854ee4be  at ./foo.c.exe
> 0x69854ee13b <__start+315> at ./foo.c.exe
>
>
>
>
> Also, here's a small diff for /usr/local/man/man3/backtrace.3, which
> makes the code example more directly usable and fixes a misprint of
> '\n':
>
> --- /usr/local/man/man3/backtrace.3 Fri May  8 19:49:25 2020
> +++ backtrace.3 Fri Jul 17 11:09:15 2020
> @@ -63,14 +63,17 @@
>  The following code fragment illustrates the use of the backtrace
>  functionality:
>  .Bd -literal -offset indent
> +#include 
> +#include 
> +
>  void
>  print_backtrace(void)
>  {
> -   void*bt[BT_MAX_DEPTH];
> +   void*bt[20];
> char**strings;
> int i, d;
>  
> -d = backtrace(bt, BT_MAX_DEPTH);
> +d = backtrace(bt, sizeof(bt) / sizeof(bt[0]));
> if (d == -1)
> errx(1, "backtrace");
>  
> @@ -80,7 +83,7 @@
> errx(1, "backtrace_symbols");
>  
> for (i = 0; i < d; i++)
> -   printf("%s\n", strings[i]);
> +   printf("%s\\n", strings[i]);
>  
> free(strings);
>  }
>
> Thanks,
>
> - Jules
>



backtrace_symbols() not showing line numbers, and small patch for backtrace.3 manpage.

2020-07-17 Thread Julian Smith
For me, backtrace_symbols() does not show line-numbers. Is this the
intended behaviour?

I'm using:
OpenBSD jules-obsd 6.7 GENERIC.MP#182 amd64
libexecinfo-0.3p2v0

With 'cc -g -Wl,--export-dynamic', i'm getting backtraces like:

0x69854ee369  at ./foo.c.exe
0x69854ee477  at ./foo.c.exe
0x69854ee4be  at ./foo.c.exe
0x69854ee13b <__start+315> at ./foo.c.exe

[gdb can show line numbers on the same executable, so the information
is definitely in the executable somewhere.]

I'm testing with this code:


#include 
#include 
#include 
#include 

void bar()
{
void*   bt[20];
char**  strings;
int i;
int d;

d = backtrace(bt, sizeof(bt) / sizeof(bt[0]));
if (d == -1)
errx(1, "backtrace");

strings = backtrace_symbols(bt, d);

if (strings == NULL)
errx(1, "backtrace_symbols");

for (i = 0; i < d; i++)
printf("%s\n", strings[i]);

free(strings);
}

void foo(void)
{
bar();
}

int main(void)
{
foo();
return 0;
}
===

Build with:
cc -g -o foo.c.exe foo.c -I /usr/local/include -g -L /usr/local/lib
-lexecinfo -Wl,--export-dynamic

Run:
./foo.c.exe

Output:
0x69854ee369  at ./foo.c.exe
0x69854ee477  at ./foo.c.exe
0x69854ee4be  at ./foo.c.exe
0x69854ee13b <__start+315> at ./foo.c.exe




Also, here's a small diff for /usr/local/man/man3/backtrace.3, which
makes the code example more directly usable and fixes a misprint of
'\n':

--- /usr/local/man/man3/backtrace.3 Fri May  8 19:49:25 2020
+++ backtrace.3 Fri Jul 17 11:09:15 2020
@@ -63,14 +63,17 @@
 The following code fragment illustrates the use of the backtrace
 functionality:
 .Bd -literal -offset indent
+#include 
+#include 
+
 void
 print_backtrace(void)
 {
-   void*bt[BT_MAX_DEPTH];
+   void*bt[20];
char**strings;
int i, d;
 
-d = backtrace(bt, BT_MAX_DEPTH);
+d = backtrace(bt, sizeof(bt) / sizeof(bt[0]));
if (d == -1)
errx(1, "backtrace");
 
@@ -80,7 +83,7 @@
errx(1, "backtrace_symbols");
 
for (i = 0; i < d; i++)
-   printf("%s\n", strings[i]);
+   printf("%s\\n", strings[i]);
 
free(strings);
 }

Thanks,

- Jules

-- 
http://op59.net