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