Coverage Report

Created: 2024-11-18 12:21

/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
10
std::string get_stack_trace(int start_pointers_index, std::string dwarf_location_info_mode) {
39
#ifdef ENABLE_STACKTRACE
40
    if (dwarf_location_info_mode.empty()) {
41
        dwarf_location_info_mode = config::dwarf_location_info_mode;
42
    }
43
    auto tool = config::get_stack_trace_tool;
44
    if (tool == "glog") {
45
        return get_stack_trace_by_glog();
46
    } else if (tool == "boost") {
47
        return get_stack_trace_by_boost();
48
    } else if (tool == "glibc") {
49
        return get_stack_trace_by_glibc();
50
    } else if (tool == "libunwind") {
51
#if defined(__APPLE__) // TODO
52
        return get_stack_trace_by_glog();
53
#endif
54
        return get_stack_trace_by_libunwind(start_pointers_index, dwarf_location_info_mode);
55
    } else {
56
        return "no stack";
57
    }
58
#endif
59
10
    return "no enable stack";
60
10
}
61
62
0
std::string get_stack_trace_by_glog() {
63
0
    std::string s;
64
0
    google::glog_internal_namespace_::DumpStackTraceToString(&s);
65
0
    return s;
66
0
}
67
68
0
std::string get_stack_trace_by_boost() {
69
0
    return boost::stacktrace::to_string(boost::stacktrace::stacktrace());
70
0
}
71
72
0
std::string get_stack_trace_by_glibc() {
73
0
    void* trace[16];
74
0
    char** messages = (char**)nullptr;
75
0
    int i, trace_size = 0;
76
77
0
    trace_size = backtrace(trace, 16);
78
0
    messages = backtrace_symbols(trace, trace_size);
79
0
    std::stringstream out;
80
0
    for (i = 1; i < trace_size; ++i) {
81
0
        out << messages[i] << "\n";
82
0
    }
83
0
    return out.str();
84
0
}
85
86
std::string get_stack_trace_by_libunwind(int start_pointers_index,
87
0
                                         const std::string& dwarf_location_info_mode) {
88
0
    return "\n" + StackTrace().toString(start_pointers_index, dwarf_location_info_mode);
89
0
}
90
91
} // namespace doris