Line data Source code
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
32 : char** backtrace_symbols (void ** , int size) {
33 : return 0;
34 : }
35 : #endif
36 :
37 : #include <ers/Context.hpp>
38 : #include <ers/Configuration.hpp>
39 :
40 : namespace
41 : {
42 : std::string
43 0 : demangle( char * mangled )
44 : {
45 0 : int status;
46 0 : char * function_begin = ::strchr( mangled, '(' );
47 0 : if ( function_begin ) {
48 0 : char * function_end = ::strchr( ++function_begin, '+' );
49 0 : if ( function_end && function_end != function_begin)
50 : {
51 0 : std::string fname(function_begin, function_end - function_begin);
52 0 : char * name = abi::__cxa_demangle( fname.c_str(), 0, 0, &status );
53 :
54 0 : if (!name) {
55 0 : return std::string( mangled );
56 : }
57 :
58 0 : std::ostringstream out;
59 0 : out << std::string(mangled, function_begin - mangled) << name << function_end;
60 0 : free( name );
61 0 : return out.str();
62 0 : }
63 : }
64 0 : return std::string( mangled );
65 : }
66 :
67 : void
68 1076 : print_function( std::ostream & out, const char * function, int verbosity )
69 : {
70 1076 : if ( verbosity )
71 : {
72 0 : out << function;
73 0 : return;
74 : }
75 :
76 1076 : const char * end = strchr( function, '(' );
77 1076 : if ( end )
78 : {
79 : const char * beg = end;
80 59725 : while ( beg > function ) {
81 59422 : if ( *(beg-1) == ' ' ) {
82 : break;
83 : }
84 58649 : --beg;
85 : }
86 1076 : out.write( beg, end - beg );
87 1076 : out << "(...)";
88 : } else {
89 0 : out << function;
90 : }
91 : }
92 : }
93 :
94 : std::vector<std::string>
95 0 : ers::Context::stack( ) const
96 : {
97 0 : std::vector<std::string> stack;
98 0 : stack.reserve( stack_size() );
99 0 : char ** symbols = backtrace_symbols( (void**)stack_symbols(), stack_size() );
100 :
101 0 : if (symbols) {
102 0 : for (int i = 1; i < stack_size(); i++) {
103 0 : stack.push_back( demangle(symbols[i]) );
104 : }
105 0 : free(symbols);
106 : }
107 :
108 0 : return stack;
109 0 : }
110 :
111 : /** Pretty printed code position
112 : * format: package_name/file_name:line_number <function_name>
113 : * \return reference to string containing format
114 : */
115 : std::string
116 1076 : ers::Context::position( int verbosity ) const
117 : {
118 1076 : std::ostringstream out;
119 1076 : print_function( out, function_name(), verbosity );
120 1076 : out << " at ";
121 :
122 1076 : const char * file = file_name();
123 1076 : if ( file[0] == '.'
124 0 : && file[1] == '.'
125 0 : && file[2] == '/' ) // file name starts with "../"
126 : {
127 0 : out << package_name() << (file + 2);
128 : } else {
129 1076 : out << file;
130 : }
131 1076 : out << ":" << line_number();
132 2152 : return out.str();
133 1076 : }
134 :
135 :
|