| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2020 the gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* 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, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "test/core/util/stack_tracer.h" |
| 22 |
|
| 23 |
#include <cstdio> |
| 24 |
#include <string> |
| 25 |
|
| 26 |
#include "absl/debugging/stacktrace.h" |
| 27 |
#include "absl/debugging/symbolize.h" |
| 28 |
|
| 29 |
#include "src/core/lib/gprpp/examine_stack.h" |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
constexpr int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*); |
| 34 |
|
| 35 |
void DumpPCAndFrameSizeAndSymbol(void (*writerfn)(const char*, void*), |
| 36 |
void* writerfn_arg, void* pc, |
| 37 |
void* symbolize_pc, int framesize, |
| 38 |
const char* const prefix) { |
| 39 |
char tmp[1024]; |
| 40 |
const char* symbol = "(unknown)"; |
| 41 |
if (absl::Symbolize(symbolize_pc, tmp, sizeof(tmp))) { |
| 42 |
symbol = tmp; |
| 43 |
} |
| 44 |
char buf[1024]; |
| 45 |
if (framesize <= 0) { |
| 46 |
snprintf(buf, sizeof(buf), "%s@ %*p (unknown) %s\n", prefix, |
| 47 |
kPrintfPointerFieldWidth, pc, symbol); |
| 48 |
} else { |
| 49 |
snprintf(buf, sizeof(buf), "%s@ %*p %9d %s\n", prefix, |
| 50 |
kPrintfPointerFieldWidth, pc, framesize, symbol); |
| 51 |
} |
| 52 |
writerfn(buf, writerfn_arg); |
| 53 |
} |
| 54 |
|
| 55 |
void DumpPCAndFrameSize(void (*writerfn)(const char*, void*), |
| 56 |
void* writerfn_arg, void* pc, int framesize, |
| 57 |
const char* const prefix) { |
| 58 |
char buf[100]; |
| 59 |
if (framesize <= 0) { |
| 60 |
snprintf(buf, sizeof(buf), "%s@ %*p (unknown)\n", prefix, |
| 61 |
kPrintfPointerFieldWidth, pc); |
| 62 |
} else { |
| 63 |
snprintf(buf, sizeof(buf), "%s@ %*p %9d\n", prefix, |
| 64 |
kPrintfPointerFieldWidth, pc, framesize); |
| 65 |
} |
| 66 |
writerfn(buf, writerfn_arg); |
| 67 |
} |
| 68 |
|
| 69 |
void DumpStackTrace(void* const stack[], int frame_sizes[], int depth, |
| 70 |
bool symbolize_stacktrace, |
| 71 |
void (*writerfn)(const char*, void*), void* writerfn_arg) { |
| 72 |
for (int i = 0; i < depth; i++) { |
| 73 |
if (symbolize_stacktrace) { |
| 74 |
DumpPCAndFrameSizeAndSymbol(writerfn, writerfn_arg, stack[i], |
| 75 |
reinterpret_cast<char*>(stack[i]) - 1, |
| 76 |
frame_sizes[i], " "); |
| 77 |
} else { |
| 78 |
DumpPCAndFrameSize(writerfn, writerfn_arg, stack[i], frame_sizes[i], |
| 79 |
" "); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void DebugWriteToString(const char* data, void* str) { |
| 85 |
reinterpret_cast<std::string*>(str)->append(data); |
| 86 |
} |
| 87 |
|
| 88 |
} // namespace |
| 89 |
|
| 90 |
namespace grpc_core { |
| 91 |
namespace testing { |
| 92 |
|
| 93 |
std::string GetCurrentStackTrace() { |
| 94 |
std::string result = "Stack trace:\n"; |
| 95 |
constexpr int kNumStackFrames = 32; |
| 96 |
void* stack[kNumStackFrames]; |
| 97 |
int frame_sizes[kNumStackFrames]; |
| 98 |
int depth = absl::GetStackFrames(stack, frame_sizes, kNumStackFrames, 1); |
| 99 |
DumpStackTrace(stack, frame_sizes, depth, true, DebugWriteToString, &result); |
| 100 |
return result; |
| 101 |
} |
| 102 |
|
| 103 |
void InitializeStackTracer(const char* argv0) { |
| 104 |
absl::InitializeSymbolizer(argv0); |
| 105 |
SetCurrentStackTraceProvider(&GetCurrentStackTrace); |
| 106 |
} |
| 107 |
|
| 108 |
} // namespace testing |
| 109 |
} // namespace grpc_core |