@@ -1255,7 +1255,12 @@ bool running_in_debugger(){
12551255}
12561256
12571257#if defined(__GLIBC__)
1258- // / print stacktrace with backtrace_symbols_fd() (glibc or macOS)
1258+ /* *
1259+ * print stacktrace with backtrace_symbols_fd() (glibc or macOS)
1260+ *
1261+ * ideally all functions should be async-signal-safe as defined by POSIX
1262+ * (glibc deviates sligntly, see also signal-safety(7))
1263+ */
12591264static void
12601265print_stacktrace_glibc ()
12611266{
@@ -1266,19 +1271,29 @@ print_stacktrace_glibc()
12661271#else
12671272 char path[MAX_PATH_SIZE];
12681273#ifdef __APPLE__
1269- const unsigned long tid = pthread_mach_thread_np (pthread_self ());
1274+ unsigned long tid = pthread_mach_thread_np (pthread_self ());
12701275#else
1271- const unsigned long tid = syscall (__NR_gettid);
1276+ unsigned long tid = syscall (__NR_gettid);
12721277#endif
1273- snprintf (path, sizeof path, " %s/ug-%lu" , get_temp_dir (), tid);
1278+ // snprintf(path, sizeof path, "%s/ug-%lu", get_temp_dir(), tid);
1279+ strncpy (path, get_temp_dir (), sizeof path);
1280+ path[sizeof path - 1 ] = ' \0 ' ;
1281+ strncat (path + strlen (path), " /ug-bt-" , sizeof path - strlen (path) - 1 );
1282+ while (tid != 0 && strlen (path) < sizeof path - 1 ) {
1283+ // (tid will be actually printed in reversed order (123->321))
1284+ size_t len = strlen (path);
1285+ path[len] = ' 0' + tid % 10 ;
1286+ path[len + 1 ] = ' \0 ' ;
1287+ tid /= 10 ;
1288+ }
12741289 int fd = open (path, O_CLOEXEC | O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
12751290 unlink (path);
12761291#endif
12771292 if (fd == -1 ) {
12781293 fd = STDERR_FILENO;
12791294 }
12801295 char backtrace_msg[] = " Backtrace:\n " ;
1281- write_all (fd, sizeof backtrace_msg, backtrace_msg);
1296+ write_all (fd, sizeof backtrace_msg - 1 , backtrace_msg);
12821297 array<void *, 256 > addresses{};
12831298 const int num_symbols = backtrace (addresses.data (), addresses.size ());
12841299 backtrace_symbols_fd (addresses.data (), num_symbols, fd);
0 commit comments