Coverage Report

Created: 2025-04-25 22:38

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