llvm.org GIT mirror llvm / bf0e75a
Revert "Work around a glibc bug: backtrace() spuriously fails if..." This commit has been breaking the FreeBSD bots: http://lab.llvm.org:8011/builders/lld-x86_64-freebsd This reverts commit r269992. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270267 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 4 years ago
2 changed file(s) with 4 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
143143 # function checks
144144 check_symbol_exists(arc4random "stdlib.h" HAVE_DECL_ARC4RANDOM)
145145 check_symbol_exists(backtrace "execinfo.h" HAVE_BACKTRACE)
146 check_symbol_exists(_Unwind_Backtrace "unwind.h" HAVE_UNWIND_BACKTRACE)
147146 check_symbol_exists(getpagesize unistd.h HAVE_GETPAGESIZE)
148147 check_symbol_exists(getrusage sys/resource.h HAVE_GETRUSAGE)
149148 check_symbol_exists(setrlimit sys/resource.h HAVE_SETRLIMIT)
4343 #endif
4444 #if HAVE_LINK_H
4545 #include
46 #endif
47 #if HAVE_UNWIND_BACKTRACE
48 #include
4946 #endif
5047
5148 using namespace llvm;
311308 }
312309 #endif // defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES) && ...
313310
314 #if defined(ENABLE_BACKTRACES) && defined(HAVE_UNWIND_BACKTRACE)
315 static int unwindBacktrace(void **StackTrace, int MaxEntries) {
316 if (MaxEntries < 0)
317 return 0;
318
319 // Skip the first frame ('unwindBacktrace' itself).
320 int Entries = -1;
321
322 auto HandleFrame = [&](_Unwind_Context *Context) -> _Unwind_Reason_Code {
323 // Apparently we need to detect reaching the end of the stack ourselves.
324 void *IP = (void *)_Unwind_GetIP(Context);
325 if (!IP)
326 return _URC_END_OF_STACK;
327
328 assert(Entries < MaxEntries && "recursively called after END_OF_STACK?");
329 if (Entries >= 0)
330 StackTrace[Entries] = IP;
331
332 if (++Entries == MaxEntries)
333 return _URC_END_OF_STACK;
334 return _URC_NO_REASON;
335 };
336
337 _Unwind_Backtrace(
338 [](_Unwind_Context *Context, void *Handler) {
339 return (*static_cast(Handler))(Context);
340 },
341 static_cast(&HandleFrame));
342 return std::max(Entries, 0);
343 }
344 #endif
345
346311 // PrintStackTrace - In the case of a program crash or fault, print out a stack
347312 // trace so that the user has an indication of why and where we died.
348313 //
349314 // On glibc systems we have the 'backtrace' function, which works nicely, but
350315 // doesn't demangle symbols.
351316 void llvm::sys::PrintStackTrace(raw_ostream &OS) {
352 #if defined(ENABLE_BACKTRACES)
353 static void *StackTrace[256];
354 int depth = 0;
355 #if defined(HAVE_BACKTRACE)
317 #if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
318 static void* StackTrace[256];
356319 // Use backtrace() to output a backtrace on Linux systems with glibc.
357 if (!depth)
358 depth = backtrace(StackTrace, static_cast(array_lengthof(StackTrace)));
359 #endif
360 #if defined(HAVE_UNWIND_BACKTRACE)
361 // Try _Unwind_Backtrace() if backtrace() failed.
362 if (!depth)
363 depth = unwindBacktrace(StackTrace,
320 int depth = backtrace(StackTrace,
364321 static_cast(array_lengthof(StackTrace)));
365 #endif
366 if (!depth)
367 return;
368
369322 if (printSymbolizedStackTrace(StackTrace, depth, OS))
370323 return;
371324 #if HAVE_DLFCN_H && __GNUG__
415368 }
416369 OS << '\n';
417370 }
418 #elif defined(HAVE_BACKTRACE)
371 #else
419372 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
420373 #endif
421374 #endif