OmniSciDB  d2f719934e
 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 2019 OmniSci, 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 
22 
23 #include <boost/algorithm/string.hpp>
24 
25 std::string getCurrentStackTrace(uint32_t num_frames_to_skip,
26  const char* stop_at_this_frame,
27  bool skip_void_and_stl_frames) {
28  std::string stack_trace;
29 
30  uint32_t frame_skip_count = num_frames_to_skip;
31 
32  // get the entire stacktrace
33  auto st = boost::stacktrace::stacktrace();
34 
35  // process frames
36  for (auto& frame : st) {
37  // skip frame?
38  if (frame_skip_count > 0) {
39  frame_skip_count--;
40  continue;
41  }
42 
43  // get function name for this frame
44  std::string frame_string = frame.name();
45 
46  // trim to plain function or template name
47  size_t open_paren_or_angle = frame_string.find_first_of("(<");
48  if (open_paren_or_angle != std::string::npos) {
49  frame_string.erase(open_paren_or_angle, std::string::npos);
50  }
51 
52  // skip stuff that we usually don't want
53  if (skip_void_and_stl_frames) {
54  if (boost::istarts_with(frame_string, "void")) {
55  continue;
56  }
57  if (boost::istarts_with(frame_string, "std::")) {
58  continue;
59  }
60  }
61 
62  // stop when we hit a particular function?
63  if (stop_at_this_frame) {
64  if (boost::starts_with(frame_string, stop_at_this_frame)) {
65  break;
66  }
67  }
68 
69  // stop at main anyway
70  if (boost::starts_with(frame_string, "main")) {
71  break;
72  }
73 
74  // add file and line? (if we can get them)
75  if (frame.source_file().size()) {
76  frame_string +=
77  " (" + frame.source_file() + ":" + std::to_string(frame.source_line()) + ")";
78  }
79 
80  // add to string
81  stack_trace += frame_string + std::string("\n");
82  }
83 
84  // done
85  return stack_trace;
86 }
std::string getCurrentStackTrace(uint32_t num_frames_to_skip, const char *stop_at_this_frame, bool skip_void_and_stl_frames)
Definition: StackTrace.cpp:25
std::string to_string(char const *&&v)