• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test/core/test_util/stack_tracer.h"
20 
21 #include <grpc/support/port_platform.h>
22 
23 #include <cstdio>
24 #include <string>
25 
26 #include "absl/debugging/stacktrace.h"
27 #include "absl/debugging/symbolize.h"
28 #include "src/core/util/examine_stack.h"
29 
30 namespace {
31 
32 constexpr int kPrintfPointerFieldWidth = 2 + (2 * sizeof(void*));
33 
DumpPCAndFrameSizeAndSymbol(void (* writerfn)(const char *,void *),void * writerfn_arg,void * pc,void * symbolize_pc,int framesize,const char * const prefix)34 void DumpPCAndFrameSizeAndSymbol(void (*writerfn)(const char*, void*),
35                                  void* writerfn_arg, void* pc,
36                                  void* symbolize_pc, int framesize,
37                                  const char* const prefix) {
38   char tmp[1024];
39   const char* symbol = "(unknown)";
40   if (absl::Symbolize(symbolize_pc, tmp, sizeof(tmp))) {
41     symbol = tmp;
42   }
43   char buf[1024];
44   if (framesize <= 0) {
45     snprintf(buf, sizeof(buf), "%s@ %*p  (unknown)  %s\n", prefix,
46              kPrintfPointerFieldWidth, pc, symbol);
47   } else {
48     snprintf(buf, sizeof(buf), "%s@ %*p  %9d  %s\n", prefix,
49              kPrintfPointerFieldWidth, pc, framesize, symbol);
50   }
51   writerfn(buf, writerfn_arg);
52 }
53 
DumpPCAndFrameSize(void (* writerfn)(const char *,void *),void * writerfn_arg,void * pc,int framesize,const char * const prefix)54 void DumpPCAndFrameSize(void (*writerfn)(const char*, void*),
55                         void* writerfn_arg, void* pc, int framesize,
56                         const char* const prefix) {
57   char buf[100];
58   if (framesize <= 0) {
59     snprintf(buf, sizeof(buf), "%s@ %*p  (unknown)\n", prefix,
60              kPrintfPointerFieldWidth, pc);
61   } else {
62     snprintf(buf, sizeof(buf), "%s@ %*p  %9d\n", prefix,
63              kPrintfPointerFieldWidth, pc, framesize);
64   }
65   writerfn(buf, writerfn_arg);
66 }
67 
DumpStackTrace(void * const stack[],int frame_sizes[],int depth,bool symbolize_stacktrace,void (* writerfn)(const char *,void *),void * writerfn_arg)68 void DumpStackTrace(void* const stack[], int frame_sizes[], int depth,
69                     bool symbolize_stacktrace,
70                     void (*writerfn)(const char*, void*), void* writerfn_arg) {
71   for (int i = 0; i < depth; i++) {
72     if (symbolize_stacktrace) {
73       DumpPCAndFrameSizeAndSymbol(writerfn, writerfn_arg, stack[i],
74                                   reinterpret_cast<char*>(stack[i]) - 1,
75                                   frame_sizes[i], "    ");
76     } else {
77       DumpPCAndFrameSize(writerfn, writerfn_arg, stack[i], frame_sizes[i],
78                          "    ");
79     }
80   }
81 }
82 
DebugWriteToString(const char * data,void * str)83 void DebugWriteToString(const char* data, void* str) {
84   reinterpret_cast<std::string*>(str)->append(data);
85 }
86 
87 }  // namespace
88 
89 namespace grpc_core {
90 namespace testing {
91 
GetCurrentStackTrace()92 std::string GetCurrentStackTrace() {
93   std::string result = "Stack trace:\n";
94   constexpr int kNumStackFrames = 32;
95   void* stack[kNumStackFrames];
96   int frame_sizes[kNumStackFrames];
97   int depth = absl::GetStackFrames(stack, frame_sizes, kNumStackFrames, 1);
98   DumpStackTrace(stack, frame_sizes, depth, true, DebugWriteToString, &result);
99   return result;
100 }
101 
InitializeStackTracer(const char * argv0)102 void InitializeStackTracer(const char* argv0) {
103   absl::InitializeSymbolizer(argv0);
104   SetCurrentStackTraceProvider(&GetCurrentStackTrace);
105 }
106 
107 }  // namespace testing
108 }  // namespace grpc_core
109