diff options
author | John Reiser <jreiser@bitwagon.com> | 2011-03-21 18:15:31 -0400 |
---|---|---|
committer | William Cohen <wcohen@redhat.com> | 2011-03-21 18:15:31 -0400 |
commit | 51a3033fa1dc443da470e2d1425eb50a1395b36a (patch) | |
tree | 3841978a870c9fdaa371c8208552f44b12f15411 | |
parent | c934a3d0f28d541ff37f88e0e0fdbb2679b92bd5 (diff) | |
download | memstomp-51a3033fa1dc443da470e2d1425eb50a1395b36a.tar.gz memstomp-51a3033fa1dc443da470e2d1425eb50a1395b36a.tar.xz memstomp-51a3033fa1dc443da470e2d1425eb50a1395b36a.zip |
Modernize translate_addresses_buf()
A number of improvements made in translate_addresses_buf():
- Make the loop check safer. Exit if loop iterator < 0.
- Restructure loop to convert "[naddr-1]" to "[naddr]".
- Make the reset of "naddr = naddr_orig" clearer.
-rw-r--r-- | backtrace-symbols.c | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/backtrace-symbols.c b/backtrace-symbols.c index d0892d6..018879d 100644 --- a/backtrace-symbols.c +++ b/backtrace-symbols.c @@ -194,12 +194,6 @@ static char ** translate_addresses_buf( int naddr ) { - int const naddr_orig = naddr; - char b; - int total = 0; - enum { Count, Print } state; - char *buf = &b; - int len = 0; char **ret_buf = NULL; bmos_arg arg; memset(&arg, 0, sizeof(arg)); arg.syms = slurp_symtab(abfd); @@ -207,22 +201,31 @@ static char ** translate_addresses_buf( /* iterate over the formating twice. * the first time we count how much space we need * the second time we do the actual printing */ + int const naddr_orig = naddr; + char b; /* space for terminating '\0' in Count state */ + char *buf = &b; + int len = 0; /* snprintf limit for *buf */ + int total = 0; + + enum { Count, Print } state; for (state=Count; state<=Print; state++) { if (state == Print) { + naddr = naddr_orig; ret_buf = malloc(total + sizeof(char *)*naddr); buf = (char *)(ret_buf + naddr); len = total; } - while (naddr) { + while (--naddr >= 0) { if (state == Print) - ret_buf[naddr-1] = buf; - arg.pc = addr[naddr-1]; + ret_buf[naddr] = buf; + arg.pc = addr[naddr]; arg.found = false; bfd_map_over_sections(abfd, find_address_in_section, &arg); if (!arg.found) { - total += snprintf(buf, len, "[0x%llx] \?\?() \?\?:0",(long long unsigned int) addr[naddr-1]) + 1; + total += 1+ snprintf(buf, len, + "[%p] \?\?() \?\?:0",(void *)addr[naddr]); } else { char const *name = arg.functionname; if (name == NULL || *name == '\0') @@ -235,17 +238,15 @@ static char ** translate_addresses_buf( arg.filename = h + 1; } total += snprintf(buf, len, "%s:%u\t%s()", - (arg.filename ? arg.filename : "??"), + (arg.filename ? arg.filename : "\?\?"), arg.line, name) + 1; } if (state == Print) { /* set buf just past the end of string */ - buf = buf + total + 1; + buf += total + 1; } - naddr--; } - naddr = naddr_orig; } if (arg.syms != NULL) { |