On 26 Nov 2011, at 7:17 AM, 成 嘉伟 wrote: int callstack[128]; int frames = backtrace((void**) callstack, 128); char** strs = backtrace_symbols((void**) callstack, frames);
My examination of your code ought to end here. The first parameter to backtrace() and backtrace_symbols() is a pointer to an array of void pointers. You are passing a pointer to an array of ints. Casting the pointers does not change the size or type of the actual contents. That your code works on the architecture you're running on is a happy coincidence.
sscanf(strs[i], "%*d %s %*s %s %*s %d", &moduleName, &functionSymbol, &offset);
You assume that the offset reported by backtrace_symbols() is a line offset, which would require that the function know and care about the debugging data for your code. On Darwin, the function is documented to use dladdr(), which returns a struct that has no field for line numbers. I see that on other platforms, if you pass -g to gcc, you can get line numbers, but apparently not in Darwin.
I'm pretty sure it's a byte offset into the function, though the odd offsets in your last two frames disturb me.
— F
|