OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StackTrace.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <Shared/StackTrace.h>
18 
19 #define BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED 1
20 
21 #include "Shared/StringTransform.h"
23 
24 #include <boost/algorithm/string.hpp>
25 
26 std::string getCurrentStackTrace(uint32_t num_frames_to_skip,
27  const char* stop_at_this_frame,
28  bool skip_void_and_stl_frames) {
29  std::string stack_trace;
30 
31  uint32_t frame_skip_count = num_frames_to_skip;
32 
33  // get the entire stacktrace
34  auto st = boost::stacktrace::stacktrace();
35 
36  // process frames
37  for (auto& frame : st) {
38  // skip frame?
39  if (frame_skip_count > 0) {
40  frame_skip_count--;
41  continue;
42  }
43 
44  // get function name for this frame
45  std::string frame_string = frame.name();
46 
47  // trim to plain function or template name
48  size_t open_paren_or_angle = frame_string.find_first_of("(<");
49  if (open_paren_or_angle != std::string::npos) {
50  frame_string.erase(open_paren_or_angle, std::string::npos);
51  }
52 
53  // skip stuff that we usually don't want
54  if (skip_void_and_stl_frames) {
55  if (boost::istarts_with(frame_string, "void")) {
56  continue;
57  }
58  if (boost::istarts_with(frame_string, "std::")) {
59  continue;
60  }
61  }
62 
63  // stop when we hit a particular function?
64  if (stop_at_this_frame) {
65  if (boost::starts_with(frame_string, stop_at_this_frame)) {
66  break;
67  }
68  }
69 
70  // stop at main anyway
71  if (boost::starts_with(frame_string, "main")) {
72  break;
73  }
74 
75  // add file and line? (if we can get them)
76  if (frame.source_file().size()) {
77  frame_string +=
78  " (" + frame.source_file() + ":" + std::to_string(frame.source_line()) + ")";
79  }
80 
81  // add to string
82  frame_string = strip(frame_string);
83  if (frame_string.empty()) {
84  continue;
85  }
86  stack_trace += frame_string + std::string("\n");
87  }
88 
89  // done
90  return stack_trace;
91 }
std::string strip(std::string_view str)
trim any whitespace from the left and right ends of a string
std::string getCurrentStackTrace(uint32_t num_frames_to_skip, const char *stop_at_this_frame, bool skip_void_and_stl_frames)
Definition: StackTrace.cpp:26
std::string to_string(char const *&&v)