Location generator for simple_ringbuffer_log_policy's value_type.
240 {
241 std::string ret;
243 {
247 {
249 for(auto *i : pb)
250 {
254 break;
255 }
256 }
257#ifndef _WIN32
258 bool done = false;
259
260 int temp[2];
261 struct _h
262 {
263 int fd;
264 } childreadh, childwriteh, readh, writeh;
265 if(-1 != ::pipe(temp))
266 {
267 using namespace scope;
268 childreadh.fd = temp[0];
269 readh.fd = temp[1];
270 if(-1 != ::pipe(temp))
271 {
272 writeh.fd = temp[0];
273 childwriteh.fd = temp[1];
274 auto unmypipes = make_scope_exit([&]() noexcept {
275 (void) ::close(readh.fd);
276 (void) ::close(writeh.fd);
277 });
278 auto unhispipes = make_scope_exit([&]() noexcept {
279 (void) ::close(childreadh.fd);
280 (void) ::close(childwriteh.fd);
281 });
282 (void) ::fcntl(readh.fd, F_SETFD, FD_CLOEXEC);
283 (void) ::fcntl(writeh.fd, F_SETFD, FD_CLOEXEC);
284
285 posix_spawn_file_actions_t child_fd_actions;
286 if(!::posix_spawn_file_actions_init(&child_fd_actions))
287 {
288 auto unactions =
make_scope_exit([&]()
noexcept { ::posix_spawn_file_actions_destroy(&child_fd_actions); });
289 if(!::posix_spawn_file_actions_adddup2(&child_fd_actions, childreadh.fd, STDIN_FILENO))
290 {
291 if(!::posix_spawn_file_actions_addclose(&child_fd_actions, childreadh.fd))
292 {
293 if(!::posix_spawn_file_actions_adddup2(&child_fd_actions, childwriteh.fd, STDOUT_FILENO))
294 {
295 if(!::posix_spawn_file_actions_addclose(&child_fd_actions, childwriteh.fd))
296 {
297 pid_t pid;
298 std::vector<const char *> argptrs(2);
299 argptrs[0] = "llvm-symbolizer";
300 if(!::posix_spawnp(&pid, "llvm-symbolizer", &child_fd_actions, nullptr, (char **) argptrs.data(), environ))
301 {
302 (void) ::close(childreadh.fd);
303 (void) ::close(childwriteh.fd);
304 std::string addrs;
305 addrs.reserve(1024);
306 for(
size_t n = 0; n <
len; n++)
307 {
311 if(
info.dli_fname ==
nullptr)
312 {
313 break;
314 }
315
316
317 addrs.append(
info.dli_fname);
318 addrs.append(" 0x");
319 const bool has_slash = (strrchr(
info.dli_fname,
'/') !=
nullptr);
320 const bool is_dll = (strstr(
info.dli_fname,
".so") !=
nullptr);
321 if(has_slash)
322 {
323 ssize_t diff;
324 if(is_dll)
325 {
327 }
328 else
329 {
331 }
332 char buffer[32];
333 snprintf(buffer, 32, "%zx", diff);
334 buffer[31] = 0;
335 addrs.append(buffer);
336 }
337 else
338 {
339 char buffer[32];
340 snprintf(buffer, 32,
"%zx", (uintptr_t)
backtrace[n]);
341 buffer[31] = 0;
342 addrs.append(buffer);
343 }
344 addrs.push_back('\n');
345 }
346
347
348 sigset_t toblock, oldset;
349 sigemptyset(&toblock);
350 sigaddset(&toblock, SIGPIPE);
351 pthread_sigmask(SIG_BLOCK, &toblock, &oldset);
353#ifdef __APPLE__
354 pthread_kill(pthread_self(), SIGPIPE);
355 int cleared = 0;
356 sigwait(&toblock, &cleared);
357#else
358 struct timespec ts = {0, 0};
359 sigtimedwait(&toblock, 0, &ts);
360#endif
361 pthread_sigmask(SIG_SETMASK, &oldset, nullptr);
362 });
363 (void) unsigmask;
364 ssize_t written = ::write(readh.fd, addrs.data(), addrs.size());
365 (void) ::close(readh.fd);
366 addrs.clear();
367 if(written != -1)
368 {
369 char buffer[1024];
370 for(;;)
371 {
372 auto bytes = ::read(writeh.fd, buffer, sizeof(buffer));
373 if(bytes < 1)
374 break;
375 addrs.append(buffer, bytes);
376 }
377 (void) ::close(writeh.fd);
378 unmypipes.release();
379 unhispipes.release();
380 }
381
382
385 int options = WEXITED | WSTOPPED;
386 if(-1 == ::waitid(P_PID, pid, &
info, options))
387 abort();
388 if(!addrs.empty())
389 {
390
391 size_t n = 0;
392 auto printitem = [&](size_t idx) {
393 if(n)
394 ret.append(", ");
395 auto idx2 = addrs.find(10, idx), idx3 = addrs.find(10, idx2 + 1);
396 ret.append(addrs.data() + idx2 + 1, idx3 - idx2 - 1);
397 n++;
398 };
399 size_t oldidx = 0;
400 for(size_t idx = addrs.find("\n\n"); idx != std::string::npos; oldidx = idx + 2, idx = addrs.find("\n\n", idx + 1))
401 {
402 printitem(oldidx);
403 }
404 done = true;
405 }
406 }
407 }
408 }
409 }
410 }
411 }
412 }
413 }
414 if(!done)
415#endif
416
417 {
418#if !QUICKCPPLIB_DISABLE_EXECINFO
420 if(!symbols)
421 ret.append("BACKTRACE FAILED!");
422 else
423 {
424 for(
size_t n = 0; n <
len; n++)
425 {
426 if(symbols[n])
427 {
428 if(n)
429 ret.append(", ");
430 ret.append(symbols[n]);
431 }
432 }
433 free(symbols);
434 }
435#endif
436 }
437 }
438 else
439 {
440 char temp[256];
443 ret.append(temp);
444 }
445 return ret;
446 }
A space packed stack backtrace letting you store twice or more stack frames in the same space.
Definition packed_backtrace.hpp:550
EXECINFO_DECL _Check_return_ _In_ size_t len
Definition execinfo_win64.h:55
EXECINFO_DECL _Check_return_ size_t backtrace(_Out_writes_(len) void **bt, _In_ size_t len)
Fill the array of void * at bt with up to len entries, returning entries filled.
@ info
Definition ringbuffer_log.hpp:93
constexpr auto make_scope_exit(T &&v)
Definition scope.hpp:153
uint8_t using_backtrace
Definition ringbuffer_log.hpp:129
char function[40]
Definition ringbuffer_log.hpp:125
char backtrace[40]
Definition ringbuffer_log.hpp:124