DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Context.cpp
Go to the documentation of this file.
1/*
2 * DUNE DAQ modification notice:
3 * This file has been modified from the original ATLAS ers source for the DUNE DAQ project.
4 * Fork baseline commit: 8267df82a4f6fe6bf02c4014923eba19eddc4614 (2020-04-14).
5 * Renamed since fork: yes (from src/Context.cxx to src/Context.cpp).
6 *
7 * Original copyright:
8 * Copyright (C) 2001-2020 CERN for the benefit of the ATLAS collaboration.
9 * Licensed under the Apache License, Version 2.0.
10 */
11
12/*
13 * Context.cxx
14 * ers
15 *
16 * Created by Serguei Kolos on 26.11.05.
17 * Copyright 2004 CERN. All rights reserved.
18 *
19 */
20#include <cxxabi.h>
21#include <pwd.h>
22#include <string.h>
23#include <sys/types.h>
24#include <unistd.h>
25
26#include <iostream>
27#include <sstream>
28
29#ifndef __rtems__
30#include <execinfo.h>
31#else
32char**
33backtrace_symbols(void**, int size)
34{
35 return 0;
36}
37#endif
38
39#include <ers/Configuration.hpp>
40#include <ers/Context.hpp>
41
42namespace {
43std::string
44demangle(char* mangled)
45{
46 int status;
47 char* function_begin = ::strchr(mangled, '(');
48 if (function_begin) {
49 char* function_end = ::strchr(++function_begin, '+');
50 if (function_end && function_end != function_begin) {
51 std::string fname(function_begin, function_end - function_begin);
52 char* name = abi::__cxa_demangle(fname.c_str(), 0, 0, &status);
53
54 if (!name) {
55 return std::string(mangled);
56 }
57
58 std::ostringstream out;
59 out << std::string(mangled, function_begin - mangled) << name << function_end;
60 free(name);
61 return out.str();
62 }
63 }
64 return std::string(mangled);
65}
66
67void
68print_function(std::ostream& out, const char* function, int verbosity)
69{
70 if (verbosity) {
71 out << function;
72 return;
73 }
74
75 const char* end = strchr(function, '(');
76 if (end) {
77 const char* beg = end;
78 while (beg > function) {
79 if (*(beg - 1) == ' ') {
80 break;
81 }
82 --beg;
83 }
84 out.write(beg, end - beg);
85 out << "(...)";
86 } else {
87 out << function;
88 }
89}
90}
91
92std::vector<std::string>
94{
95 std::vector<std::string> stack;
96 stack.reserve(stack_size());
97 char** symbols = backtrace_symbols((void**)stack_symbols(), stack_size());
98
99 if (symbols) {
100 for (int i = 1; i < stack_size(); i++) {
101 stack.push_back(demangle(symbols[i]));
102 }
103 free(symbols);
104 }
105
106 return stack;
107}
108
113std::string
114ers::Context::position(int verbosity) const
115{
116 std::ostringstream out;
117 print_function(out, function_name(), verbosity);
118 out << " at ";
119
120 const char* file = file_name();
121 if (file[0] == '.' && file[1] == '.' && file[2] == '/') // file name starts with "../"
122 {
123 out << package_name() << (file + 2);
124 } else {
125 out << file;
126 }
127 out << ":" << line_number();
128 return out.str();
129}
virtual void *const * stack_symbols() const =0
virtual int line_number() const =0
virtual int stack_size() const =0
std::string position(int verbosity=ers::Configuration::instance().verbosity_level()) const
Definition Context.cpp:114
virtual const char * package_name() const =0
virtual const char * file_name() const =0
std::vector< std::string > stack() const
Definition Context.cpp:93
virtual const char * function_name() const =0
FELIX Initialization std::string initerror FELIX queue timed out