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 <string.h>
21#include <cxxabi.h>
22#include <sys/types.h>
23#include <pwd.h>
24#include <unistd.h>
25
26#include <iostream>
27#include <sstream>
28
29#ifndef __rtems__
30#include <execinfo.h>
31#else
32char** backtrace_symbols (void ** , int size) {
33 return 0;
34}
35#endif
36
37#include <ers/Context.hpp>
38#include <ers/Configuration.hpp>
39
40namespace
41{
42 std::string
43 demangle( char * mangled )
44 {
45 int status;
46 char * function_begin = ::strchr( mangled, '(' );
47 if ( function_begin ) {
48 char * function_end = ::strchr( ++function_begin, '+' );
49 if ( function_end && function_end != function_begin)
50 {
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
67 void
68 print_function( std::ostream & out, const char * function, int verbosity )
69 {
70 if ( verbosity )
71 {
72 out << function;
73 return;
74 }
75
76 const char * end = strchr( function, '(' );
77 if ( end )
78 {
79 const char * beg = end;
80 while ( beg > function ) {
81 if ( *(beg-1) == ' ' ) {
82 break;
83 }
84 --beg;
85 }
86 out.write( beg, end - beg );
87 out << "(...)";
88 } else {
89 out << function;
90 }
91 }
92}
93
94std::vector<std::string>
96{
97 std::vector<std::string> stack;
98 stack.reserve( stack_size() );
99 char ** symbols = backtrace_symbols( (void**)stack_symbols(), stack_size() );
100
101 if (symbols) {
102 for (int i = 1; i < stack_size(); i++) {
103 stack.push_back( demangle(symbols[i]) );
104 }
105 free(symbols);
106 }
107
108 return stack;
109}
110
115std::string
116ers::Context::position( int verbosity ) const
117{
118 std::ostringstream out;
119 print_function( out, function_name(), verbosity );
120 out << " at ";
121
122 const char * file = file_name();
123 if ( file[0] == '.'
124 && file[1] == '.'
125 && file[2] == '/' ) // file name starts with "../"
126 {
127 out << package_name() << (file + 2);
128 } else {
129 out << file;
130 }
131 out << ":" << line_number();
132 return out.str();
133}
134
135
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:116
virtual const char * package_name() const =0
virtual const char * file_name() const =0
std::vector< std::string > stack() const
Definition Context.cpp:95
virtual const char * function_name() const =0
FELIX Initialization std::string initerror FELIX queue timed out