misc: Add routine to generate backtrace from within the application

E.g. to analyze the subscr_get/subscr_put behavior one can place
the generate_backtrace into the functions, recompile and then filter
the output with contrib/bt.py to get the function name, file and line.
This commit is contained in:
Holger Hans Peter Freyther 2009-10-28 09:12:43 +01:00
parent a8dffc512b
commit 25f30ba00a
3 changed files with 54 additions and 0 deletions

33
openbsc/contrib/bt.py Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env python
import os
f = open("unbalanced")
lines = []
for line in f:
lines.append(line)
filenames = {}
output = []
for line in lines:
if "[0x" in line:
start = line.find("[")
end = line.find("]")
addr = line[start+1:end]
try:
file = filenames[addr]
except KeyError:
r = os.popen("addr2line -fs -e ./bsc_hack %s" % addr)
all = r.read().replace("\n", ",")
file = all
filenames[addr] = file
line = line.replace(addr, file)
output.append(line)
g = open("unbalanced.2", "w")
g.write("".join(output))

View File

@ -32,4 +32,6 @@ int gsm_7bit_encode(u_int8_t *result, const char *data);
int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm);
int ms_pwr_dbm(enum gsm_band band, u_int8_t lvl);
void generate_backtrace();
#endif

View File

@ -23,8 +23,10 @@
#include <openbsc/gsm_data.h>
#include <openbsc/gsm_utils.h>
#include <execinfo.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
/* GSM 03.38 6.2.1 Charachter packing */
@ -148,4 +150,21 @@ int ms_pwr_dbm(enum gsm_band band, u_int8_t lvl)
return -EINVAL;
}
void generate_backtrace()
{
int i, nptrs;
void *buffer[100];
char **strings;
nptrs = backtrace(buffer, ARRAY_SIZE(buffer));
printf("backtrace() returned %d addresses\n", nptrs);
strings = backtrace_symbols(buffer, nptrs);
if (!strings)
return;
for (i = 1; i < nptrs; i++)
printf("%s\n", strings[i]);
free(strings);
}