/root/doris/be/src/util/stack_util.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "util/stack_util.h" |
19 | | |
20 | | #include <execinfo.h> |
21 | | #include <signal.h> |
22 | | #include <stdio.h> |
23 | | |
24 | | #include <boost/stacktrace.hpp> |
25 | | |
26 | | #include "common/stack_trace.h" |
27 | | #include "util/mem_info.h" |
28 | | #include "util/pretty_printer.h" |
29 | | |
30 | | namespace google { |
31 | | namespace glog_internal_namespace_ { |
32 | | void DumpStackTraceToString(std::string* stacktrace); |
33 | | } |
34 | | } // namespace google |
35 | | |
36 | | namespace doris { |
37 | | |
38 | 35 | std::string get_stack_trace(int start_pointers_index, std::string dwarf_location_info_mode) { |
39 | | #ifndef BE_TEST |
40 | | if (!config::enable_stacktrace) { |
41 | | return "no enable stacktrace"; |
42 | | } |
43 | | #endif |
44 | 35 | if (dwarf_location_info_mode.empty()) { |
45 | 35 | dwarf_location_info_mode = config::dwarf_location_info_mode; |
46 | 35 | } |
47 | 35 | #ifdef BE_TEST |
48 | 35 | auto tool = std::string {"libunwind"}; |
49 | | #else |
50 | | auto tool = config::get_stack_trace_tool; |
51 | | #endif |
52 | 35 | if (tool == "glog") { |
53 | 0 | return get_stack_trace_by_glog(); |
54 | 35 | } else if (tool == "boost") { |
55 | 0 | return get_stack_trace_by_boost(); |
56 | 35 | } else if (tool == "glibc") { |
57 | 0 | return get_stack_trace_by_glibc(); |
58 | 35 | } else if (tool == "libunwind") { |
59 | | #if defined(__APPLE__) // TODO |
60 | | return get_stack_trace_by_glog(); |
61 | | #endif |
62 | 35 | return get_stack_trace_by_libunwind(start_pointers_index, dwarf_location_info_mode); |
63 | 35 | } else { |
64 | 0 | return "no stack"; |
65 | 0 | } |
66 | 35 | } |
67 | | |
68 | 0 | std::string get_stack_trace_by_glog() { |
69 | 0 | std::string s; |
70 | 0 | google::glog_internal_namespace_::DumpStackTraceToString(&s); |
71 | 0 | return s; |
72 | 0 | } |
73 | | |
74 | 0 | std::string get_stack_trace_by_boost() { |
75 | 0 | return boost::stacktrace::to_string(boost::stacktrace::stacktrace()); |
76 | 0 | } |
77 | | |
78 | 0 | std::string get_stack_trace_by_glibc() { |
79 | 0 | void* trace[16]; |
80 | 0 | char** messages = (char**)nullptr; |
81 | 0 | int i, trace_size = 0; |
82 | |
|
83 | 0 | trace_size = backtrace(trace, 16); |
84 | 0 | messages = backtrace_symbols(trace, trace_size); |
85 | 0 | std::stringstream out; |
86 | 0 | for (i = 1; i < trace_size; ++i) { |
87 | 0 | out << messages[i] << "\n"; |
88 | 0 | } |
89 | 0 | return out.str(); |
90 | 0 | } |
91 | | |
92 | | std::string get_stack_trace_by_libunwind(int start_pointers_index, |
93 | 35 | const std::string& dwarf_location_info_mode) { |
94 | 35 | return "\n" + StackTrace().toString(start_pointers_index, dwarf_location_info_mode); |
95 | 35 | } |
96 | | |
97 | | } // namespace doris |