xref: /aosp_15_r20/external/openthread/src/posix/platform/backtrace.cpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2022, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker  *  All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker  *
5*cfb92d14SAndroid Build Coastguard Worker  *  Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker  *  modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker  *  1. Redistributions of source code must retain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker  *  2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker  *     documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker  *  3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker  *     names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker  *     derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker  *
16*cfb92d14SAndroid Build Coastguard Worker  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker  *  POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker  */
28*cfb92d14SAndroid Build Coastguard Worker 
29*cfb92d14SAndroid Build Coastguard Worker /**
30*cfb92d14SAndroid Build Coastguard Worker  * @file
31*cfb92d14SAndroid Build Coastguard Worker  *   This file implements backtrace for posix.
32*cfb92d14SAndroid Build Coastguard Worker  */
33*cfb92d14SAndroid Build Coastguard Worker 
34*cfb92d14SAndroid Build Coastguard Worker #include "openthread-posix-config.h"
35*cfb92d14SAndroid Build Coastguard Worker #include "platform-posix.h"
36*cfb92d14SAndroid Build Coastguard Worker 
37*cfb92d14SAndroid Build Coastguard Worker #include <signal.h>
38*cfb92d14SAndroid Build Coastguard Worker #include <stdio.h>
39*cfb92d14SAndroid Build Coastguard Worker #include <stdlib.h>
40*cfb92d14SAndroid Build Coastguard Worker #include <string.h>
41*cfb92d14SAndroid Build Coastguard Worker #include <unistd.h>
42*cfb92d14SAndroid Build Coastguard Worker 
43*cfb92d14SAndroid Build Coastguard Worker #include "common/code_utils.hpp"
44*cfb92d14SAndroid Build Coastguard Worker #include "common/logging.hpp"
45*cfb92d14SAndroid Build Coastguard Worker 
46*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_POSIX_CONFIG_BACKTRACE_ENABLE
47*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_POSIX_CONFIG_ANDROID_ENABLE || defined(__GLIBC__)
48*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_POSIX_CONFIG_ANDROID_ENABLE
49*cfb92d14SAndroid Build Coastguard Worker #include <log/log.h>
50*cfb92d14SAndroid Build Coastguard Worker #include <utils/CallStack.h>
51*cfb92d14SAndroid Build Coastguard Worker #include <utils/Printer.h>
52*cfb92d14SAndroid Build Coastguard Worker 
53*cfb92d14SAndroid Build Coastguard Worker class LogPrinter : public android::Printer
54*cfb92d14SAndroid Build Coastguard Worker {
55*cfb92d14SAndroid Build Coastguard Worker public:
printLine(const char * string)56*cfb92d14SAndroid Build Coastguard Worker     virtual void printLine(const char *string) { otLogCritPlat("%s", string); }
57*cfb92d14SAndroid Build Coastguard Worker };
58*cfb92d14SAndroid Build Coastguard Worker 
dumpStack(void)59*cfb92d14SAndroid Build Coastguard Worker static void dumpStack(void)
60*cfb92d14SAndroid Build Coastguard Worker {
61*cfb92d14SAndroid Build Coastguard Worker     LogPrinter         printer;
62*cfb92d14SAndroid Build Coastguard Worker     android::CallStack callstack;
63*cfb92d14SAndroid Build Coastguard Worker 
64*cfb92d14SAndroid Build Coastguard Worker     callstack.update();
65*cfb92d14SAndroid Build Coastguard Worker     callstack.print(printer);
66*cfb92d14SAndroid Build Coastguard Worker }
67*cfb92d14SAndroid Build Coastguard Worker #else // OPENTHREAD_POSIX_CONFIG_ANDROID_ENABLE
68*cfb92d14SAndroid Build Coastguard Worker #include <cxxabi.h>
69*cfb92d14SAndroid Build Coastguard Worker #include <execinfo.h>
70*cfb92d14SAndroid Build Coastguard Worker 
demangleSymbol(int aIndex,const char * aSymbol)71*cfb92d14SAndroid Build Coastguard Worker static void demangleSymbol(int aIndex, const char *aSymbol)
72*cfb92d14SAndroid Build Coastguard Worker {
73*cfb92d14SAndroid Build Coastguard Worker     constexpr uint16_t kMaxNameSize = 256;
74*cfb92d14SAndroid Build Coastguard Worker     int                status;
75*cfb92d14SAndroid Build Coastguard Worker     char               program[kMaxNameSize + 1];
76*cfb92d14SAndroid Build Coastguard Worker     char               mangledName[kMaxNameSize + 1];
77*cfb92d14SAndroid Build Coastguard Worker     char              *demangledName = nullptr;
78*cfb92d14SAndroid Build Coastguard Worker     char              *functionName  = nullptr;
79*cfb92d14SAndroid Build Coastguard Worker     unsigned int       offset        = 0;
80*cfb92d14SAndroid Build Coastguard Worker     unsigned int       address       = 0;
81*cfb92d14SAndroid Build Coastguard Worker 
82*cfb92d14SAndroid Build Coastguard Worker     // Try to demangle a C++ name
83*cfb92d14SAndroid Build Coastguard Worker     if (sscanf(aSymbol, "%256[^(]%*[^_]%256[^)+]%*[^0x]%x%*[^0x]%x", program, mangledName, &offset, &address) == 4)
84*cfb92d14SAndroid Build Coastguard Worker     {
85*cfb92d14SAndroid Build Coastguard Worker         demangledName = abi::__cxa_demangle(mangledName, nullptr, nullptr, &status);
86*cfb92d14SAndroid Build Coastguard Worker         functionName  = demangledName;
87*cfb92d14SAndroid Build Coastguard Worker     }
88*cfb92d14SAndroid Build Coastguard Worker 
89*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(demangledName == nullptr);
90*cfb92d14SAndroid Build Coastguard Worker 
91*cfb92d14SAndroid Build Coastguard Worker     // If failed to demangle a C++ name, try to get a regular C symbol
92*cfb92d14SAndroid Build Coastguard Worker     if (sscanf(aSymbol, "%256[^(](%256[^)+]%*[^0x]%x%*[^0x]%x", program, mangledName, &offset, &address) == 4)
93*cfb92d14SAndroid Build Coastguard Worker     {
94*cfb92d14SAndroid Build Coastguard Worker         functionName = mangledName;
95*cfb92d14SAndroid Build Coastguard Worker     }
96*cfb92d14SAndroid Build Coastguard Worker 
97*cfb92d14SAndroid Build Coastguard Worker exit:
98*cfb92d14SAndroid Build Coastguard Worker     if (functionName != nullptr)
99*cfb92d14SAndroid Build Coastguard Worker     {
100*cfb92d14SAndroid Build Coastguard Worker         otLogCritPlat("#%2d: %s %s+0x%x [0x%x]\n", aIndex, program, functionName, offset, address);
101*cfb92d14SAndroid Build Coastguard Worker     }
102*cfb92d14SAndroid Build Coastguard Worker     else
103*cfb92d14SAndroid Build Coastguard Worker     {
104*cfb92d14SAndroid Build Coastguard Worker         otLogCritPlat("#%2d: %s\n", aIndex, aSymbol);
105*cfb92d14SAndroid Build Coastguard Worker     }
106*cfb92d14SAndroid Build Coastguard Worker 
107*cfb92d14SAndroid Build Coastguard Worker     if (demangledName != nullptr)
108*cfb92d14SAndroid Build Coastguard Worker     {
109*cfb92d14SAndroid Build Coastguard Worker         free(demangledName);
110*cfb92d14SAndroid Build Coastguard Worker     }
111*cfb92d14SAndroid Build Coastguard Worker }
112*cfb92d14SAndroid Build Coastguard Worker 
dumpStack(void)113*cfb92d14SAndroid Build Coastguard Worker static void dumpStack(void)
114*cfb92d14SAndroid Build Coastguard Worker {
115*cfb92d14SAndroid Build Coastguard Worker     constexpr uint8_t kMaxDepth = 50;
116*cfb92d14SAndroid Build Coastguard Worker     void             *stack[kMaxDepth];
117*cfb92d14SAndroid Build Coastguard Worker     char            **symbols;
118*cfb92d14SAndroid Build Coastguard Worker     int               depth;
119*cfb92d14SAndroid Build Coastguard Worker 
120*cfb92d14SAndroid Build Coastguard Worker     depth   = backtrace(stack, kMaxDepth);
121*cfb92d14SAndroid Build Coastguard Worker     symbols = backtrace_symbols(stack, depth);
122*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(symbols != nullptr);
123*cfb92d14SAndroid Build Coastguard Worker 
124*cfb92d14SAndroid Build Coastguard Worker     for (int i = 0; i < depth; i++)
125*cfb92d14SAndroid Build Coastguard Worker     {
126*cfb92d14SAndroid Build Coastguard Worker         demangleSymbol(i, symbols[i]);
127*cfb92d14SAndroid Build Coastguard Worker     }
128*cfb92d14SAndroid Build Coastguard Worker 
129*cfb92d14SAndroid Build Coastguard Worker     free(symbols);
130*cfb92d14SAndroid Build Coastguard Worker 
131*cfb92d14SAndroid Build Coastguard Worker exit:
132*cfb92d14SAndroid Build Coastguard Worker     return;
133*cfb92d14SAndroid Build Coastguard Worker }
134*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_POSIX_CONFIG_ANDROID_ENABLE
135*cfb92d14SAndroid Build Coastguard Worker static constexpr uint8_t kNumSignals           = 6;
136*cfb92d14SAndroid Build Coastguard Worker static constexpr int     kSignals[kNumSignals] = {SIGABRT, SIGILL, SIGSEGV, SIGBUS, SIGTRAP, SIGFPE};
137*cfb92d14SAndroid Build Coastguard Worker static struct sigaction  sSigActions[kNumSignals];
138*cfb92d14SAndroid Build Coastguard Worker 
resetSignalActions(void)139*cfb92d14SAndroid Build Coastguard Worker static void resetSignalActions(void)
140*cfb92d14SAndroid Build Coastguard Worker {
141*cfb92d14SAndroid Build Coastguard Worker     for (uint8_t i = 0; i < kNumSignals; i++)
142*cfb92d14SAndroid Build Coastguard Worker     {
143*cfb92d14SAndroid Build Coastguard Worker         sigaction(kSignals[i], &sSigActions[i], (struct sigaction *)nullptr);
144*cfb92d14SAndroid Build Coastguard Worker     }
145*cfb92d14SAndroid Build Coastguard Worker }
146*cfb92d14SAndroid Build Coastguard Worker 
signalCritical(int sig,siginfo_t * info,void * ucontext)147*cfb92d14SAndroid Build Coastguard Worker static void signalCritical(int sig, siginfo_t *info, void *ucontext)
148*cfb92d14SAndroid Build Coastguard Worker {
149*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(ucontext);
150*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(info);
151*cfb92d14SAndroid Build Coastguard Worker 
152*cfb92d14SAndroid Build Coastguard Worker     otLogCritPlat("------------------ BEGINNING OF CRASH -------------");
153*cfb92d14SAndroid Build Coastguard Worker     otLogCritPlat("*** FATAL ERROR: Caught signal: %d (%s)", sig, strsignal(sig));
154*cfb92d14SAndroid Build Coastguard Worker 
155*cfb92d14SAndroid Build Coastguard Worker     dumpStack();
156*cfb92d14SAndroid Build Coastguard Worker 
157*cfb92d14SAndroid Build Coastguard Worker     otLogCritPlat("------------------ END OF CRASH ------------------");
158*cfb92d14SAndroid Build Coastguard Worker 
159*cfb92d14SAndroid Build Coastguard Worker     resetSignalActions();
160*cfb92d14SAndroid Build Coastguard Worker     raise(sig);
161*cfb92d14SAndroid Build Coastguard Worker }
162*cfb92d14SAndroid Build Coastguard Worker 
platformBacktraceInit(void)163*cfb92d14SAndroid Build Coastguard Worker void platformBacktraceInit(void)
164*cfb92d14SAndroid Build Coastguard Worker {
165*cfb92d14SAndroid Build Coastguard Worker     struct sigaction sigact;
166*cfb92d14SAndroid Build Coastguard Worker 
167*cfb92d14SAndroid Build Coastguard Worker     memset(&sigact, 0, sizeof(struct sigaction));
168*cfb92d14SAndroid Build Coastguard Worker 
169*cfb92d14SAndroid Build Coastguard Worker     sigact.sa_sigaction = &signalCritical;
170*cfb92d14SAndroid Build Coastguard Worker     sigact.sa_flags     = SA_RESTART | SA_SIGINFO | SA_NOCLDWAIT;
171*cfb92d14SAndroid Build Coastguard Worker 
172*cfb92d14SAndroid Build Coastguard Worker     for (uint8_t i = 0; i < kNumSignals; i++)
173*cfb92d14SAndroid Build Coastguard Worker     {
174*cfb92d14SAndroid Build Coastguard Worker         sigaction(kSignals[i], &sigact, &sSigActions[i]);
175*cfb92d14SAndroid Build Coastguard Worker     }
176*cfb92d14SAndroid Build Coastguard Worker }
177*cfb92d14SAndroid Build Coastguard Worker #else  // OPENTHREAD_POSIX_CONFIG_ANDROID_ENABLE || defined(__GLIBC__)
platformBacktraceInit(void)178*cfb92d14SAndroid Build Coastguard Worker void platformBacktraceInit(void) {}
179*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_POSIX_CONFIG_ANDROID_ENABLE || defined(__GLIBC__)
180*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_POSIX_CONFIG_BACKTRACE_ENABLE
181