@@ -205,11 +205,13 @@ static const Signaltype listofsignals[] = {
205205 * \return size of array
206206 * */
207207template <typename T, int size>
208- int GetArrayLength (T(&)[size])
208+ int GetArrayLength (const T (&)[size])
209209{
210210 return size;
211211}
212212
213+ // 32 vs. 64bit
214+ #define ADDRESSDISPLAYLENGTH ((sizeof (long )==8 )?12 :8 )
213215
214216/*
215217 * Try to print the callstack.
@@ -218,34 +220,53 @@ int GetArrayLength(T(&)[size])
218220static void print_stacktrace (FILE* f, bool demangling)
219221{
220222#if defined(__GNUC__)
221- void *array[50 ]= {0 };
223+ void *array[32 ]= {0 }; // the less resources the better...
222224 const int depth = backtrace (array, (int )GetArrayLength (array));
223225 char **symbolstrings = backtrace_symbols (array, depth);
224226 if (symbolstrings) {
225- fprintf (f, " Callstack:\n " );
227+ fputs ( " Callstack:\n " , f );
226228 const int offset=3 ; // the first two entries are simply within our own exception handling code, third is within libc
227229 for (int i = offset; i < depth; ++i) {
228230 const char * const symbol = symbolstrings[i];
231+ // fprintf(f, "\"%s\"\n", symbol);
229232 char * realname = nullptr ;
230- const char * const firstBracket = strchr (symbol, ' (' );
231- if (demangling && firstBracket) {
232- const char * const plus = strchr (firstBracket, ' +' );
233- if (plus) {
233+ const char * const firstBracketName = strchr (symbol, ' (' );
234+ const char * const firstBracketAddress = strchr (symbol, ' [' );
235+ const char * const secondBracketAddress = strchr (firstBracketAddress, ' ]' );
236+ const char * const beginAddress = firstBracketAddress+3 ;
237+ const int addressLen = int (secondBracketAddress-beginAddress);
238+ const int padLen = int (ADDRESSDISPLAYLENGTH-addressLen);
239+ // fprintf(f, "AddressDisplayLength=%d addressLen=%d padLen=%d\n", ADDRESSDISPLAYLENGTH, addressLen, padLen);
240+ if (demangling && firstBracketName) {
241+ const char * const plus = strchr (firstBracketName, ' +' );
242+ if (plus && (plus>(firstBracketName+1 ))) {
234243 char input_buffer[512 ]= {0 };
235- strncpy (input_buffer, firstBracket +1 , plus-firstBracket -1 );
244+ strncpy (input_buffer, firstBracketName +1 , plus-firstBracketName -1 );
236245 char output_buffer[1024 ]= {0 };
237246 size_t length = GetArrayLength (output_buffer);
238247 int status=0 ;
239248 realname = abi::__cxa_demangle (input_buffer, output_buffer, &length, &status); // non-NULL on success
240249 }
241250 }
242-
243- fprintf (f, " %d. %s\n " ,
244- i-offset, (realname) ? realname : symbolstrings[i]);
251+ fprintf (f, " #%d 0x" ,
252+ i-offset);
253+ if (padLen>0 )
254+ fprintf (f, " %0*d" ,
255+ padLen, 0 );
256+ if (realname) {
257+ fprintf (f, " %.*s in %s\n " ,
258+ (int )(secondBracketAddress-firstBracketAddress-3 ), firstBracketAddress+3 ,
259+ realname);
260+ }
261+ else {
262+ fprintf (f, " %.*s in %.*s\n " ,
263+ (int )(secondBracketAddress-firstBracketAddress-3 ), firstBracketAddress+3 ,
264+ (int )(firstBracketAddress-symbol), symbol);
265+ }
245266 }
246267 free (symbolstrings);
247268 } else {
248- fprintf (f, " Callstack could not be obtained\n " );
269+ fputs ( " Callstack could not be obtained\n " , f );
249270 }
250271#endif
251272}
@@ -269,40 +290,41 @@ static void CppcheckSignalHandler(int signo, siginfo_t * info, void * /*context*
269290{
270291 const char * const signame=signal_name (signo);
271292 bool bPrintCallstack=true ;
293+ FILE* f=stderr;
272294 switch (signo) {
273295 case SIGILL:
274- fprintf (stderr , " Internal error (caught signal %d=%s at 0x%p)\n " ,
296+ fprintf (f , " Internal error (caught signal %d=%s at 0x%p)\n " ,
275297 signo, signame, info->si_addr );
276298 break ;
277299 case SIGFPE:
278- fprintf (stderr , " Internal error (caught signal %d=%s at 0x%p)\n " ,
300+ fprintf (f , " Internal error (caught signal %d=%s at 0x%p)\n " ,
279301 signo, signame, info->si_addr );
280302 break ;
281303 case SIGSEGV:
282- fprintf (stderr , " Internal error (caught signal %d=%s at 0x%p)\n " ,
304+ fprintf (f , " Internal error (caught signal %d=%s at 0x%p)\n " ,
283305 signo, signame, info->si_addr );
284306 break ;
285307 /*
286308 case SIGBUS:
287- fprintf(stderr , "Internal error (caught signal %d=%s at 0x%p)\n",
309+ fprintf(f , "Internal error (caught signal %d=%s at 0x%p)\n",
288310 signo, signame, info->si_addr);
289311 break;
290312 case SIGTRAP:
291- fprintf(stderr , "Internal error (caught signal %d=%s at 0x%p)\n",
313+ fprintf(f , "Internal error (caught signal %d=%s at 0x%p)\n",
292314 signo, signame, info->si_addr);
293315 break;
294316 */
295317 case SIGINT:
296318 bPrintCallstack=false ;
297319 break ;
298320 default :
299- fprintf (stderr , " Internal error (caught signal %d)\n " ,
321+ fprintf (f , " Internal error (caught signal %d)\n " ,
300322 signo);
301323 break ;
302324 }
303325 if (bPrintCallstack) {
304- print_stacktrace (stderr, false );
305- fprintf (stderr, " Please report this to the cppcheck developers!\n " );
326+ print_stacktrace (f, true );
327+ fputs ( " \n Please report this to the cppcheck developers!\n " , f );
306328 }
307329 abort ();
308330}
0 commit comments