• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "runtime.h"
18 
19 #include <signal.h>
20 #include <string.h>
21 #include <sys/utsname.h>
22 
23 #include "base/logging.h"
24 #include "base/mutex.h"
25 #include "base/stringprintf.h"
26 #include "thread.h"
27 #include "utils.h"
28 
29 namespace art {
30 
31 struct Backtrace {
Dumpart::Backtrace32   void Dump(std::ostream& os) {
33     DumpNativeStack(os, GetTid(), "\t", true);
34   }
35 };
36 
37 struct OsInfo {
Dumpart::OsInfo38   void Dump(std::ostream& os) {
39     utsname info;
40     uname(&info);
41     // Linux 2.6.38.8-gg784 (x86_64)
42     // Darwin 11.4.0 (x86_64)
43     os << info.sysname << " " << info.release << " (" << info.machine << ")";
44   }
45 };
46 
GetSignalName(int signal_number)47 static const char* GetSignalName(int signal_number) {
48   switch (signal_number) {
49     case SIGABRT: return "SIGABRT";
50     case SIGBUS: return "SIGBUS";
51     case SIGFPE: return "SIGFPE";
52     case SIGILL: return "SIGILL";
53     case SIGPIPE: return "SIGPIPE";
54     case SIGSEGV: return "SIGSEGV";
55 #if defined(SIGSTKFLT)
56     case SIGSTKFLT: return "SIGSTKFLT";
57 #endif
58     case SIGTRAP: return "SIGTRAP";
59   }
60   return "??";
61 }
62 
GetSignalCodeName(int signal_number,int signal_code)63 static const char* GetSignalCodeName(int signal_number, int signal_code) {
64   // Try the signal-specific codes...
65   switch (signal_number) {
66     case SIGILL:
67       switch (signal_code) {
68         case ILL_ILLOPC: return "ILL_ILLOPC";
69         case ILL_ILLOPN: return "ILL_ILLOPN";
70         case ILL_ILLADR: return "ILL_ILLADR";
71         case ILL_ILLTRP: return "ILL_ILLTRP";
72         case ILL_PRVOPC: return "ILL_PRVOPC";
73         case ILL_PRVREG: return "ILL_PRVREG";
74         case ILL_COPROC: return "ILL_COPROC";
75         case ILL_BADSTK: return "ILL_BADSTK";
76       }
77       break;
78     case SIGBUS:
79       switch (signal_code) {
80         case BUS_ADRALN: return "BUS_ADRALN";
81         case BUS_ADRERR: return "BUS_ADRERR";
82         case BUS_OBJERR: return "BUS_OBJERR";
83       }
84       break;
85     case SIGFPE:
86       switch (signal_code) {
87         case FPE_INTDIV: return "FPE_INTDIV";
88         case FPE_INTOVF: return "FPE_INTOVF";
89         case FPE_FLTDIV: return "FPE_FLTDIV";
90         case FPE_FLTOVF: return "FPE_FLTOVF";
91         case FPE_FLTUND: return "FPE_FLTUND";
92         case FPE_FLTRES: return "FPE_FLTRES";
93         case FPE_FLTINV: return "FPE_FLTINV";
94         case FPE_FLTSUB: return "FPE_FLTSUB";
95       }
96       break;
97     case SIGSEGV:
98       switch (signal_code) {
99         case SEGV_MAPERR: return "SEGV_MAPERR";
100         case SEGV_ACCERR: return "SEGV_ACCERR";
101       }
102       break;
103     case SIGTRAP:
104       switch (signal_code) {
105         case TRAP_BRKPT: return "TRAP_BRKPT";
106         case TRAP_TRACE: return "TRAP_TRACE";
107       }
108       break;
109   }
110   // Then the other codes...
111   switch (signal_code) {
112     case SI_USER:     return "SI_USER";
113 #if defined(SI_KERNEL)
114     case SI_KERNEL:   return "SI_KERNEL";
115 #endif
116     case SI_QUEUE:    return "SI_QUEUE";
117     case SI_TIMER:    return "SI_TIMER";
118     case SI_MESGQ:    return "SI_MESGQ";
119     case SI_ASYNCIO:  return "SI_ASYNCIO";
120 #if defined(SI_SIGIO)
121     case SI_SIGIO:    return "SI_SIGIO";
122 #endif
123 #if defined(SI_TKILL)
124     case SI_TKILL:    return "SI_TKILL";
125 #endif
126   }
127   // Then give up...
128   return "?";
129 }
130 
131 struct UContext {
UContextart::UContext132   explicit UContext(void* raw_context) : context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {}
133 
Dumpart::UContext134   void Dump(std::ostream& os) {
135     // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets).
136 #if defined(__APPLE__)
137     DumpRegister32(os, "eax", context->__ss.__eax);
138     DumpRegister32(os, "ebx", context->__ss.__ebx);
139     DumpRegister32(os, "ecx", context->__ss.__ecx);
140     DumpRegister32(os, "edx", context->__ss.__edx);
141     os << '\n';
142 
143     DumpRegister32(os, "edi", context->__ss.__edi);
144     DumpRegister32(os, "esi", context->__ss.__esi);
145     DumpRegister32(os, "ebp", context->__ss.__ebp);
146     DumpRegister32(os, "esp", context->__ss.__esp);
147     os << '\n';
148 
149     DumpRegister32(os, "eip", context->__ss.__eip);
150     os << "                   ";
151     DumpRegister32(os, "eflags", context->__ss.__eflags);
152     DumpX86Flags(os, context->__ss.__eflags);
153     os << '\n';
154 
155     DumpRegister32(os, "cs",  context->__ss.__cs);
156     DumpRegister32(os, "ds",  context->__ss.__ds);
157     DumpRegister32(os, "es",  context->__ss.__es);
158     DumpRegister32(os, "fs",  context->__ss.__fs);
159     os << '\n';
160     DumpRegister32(os, "gs",  context->__ss.__gs);
161     DumpRegister32(os, "ss",  context->__ss.__ss);
162 #else
163     DumpRegister32(os, "eax", context.gregs[REG_EAX]);
164     DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
165     DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
166     DumpRegister32(os, "edx", context.gregs[REG_EDX]);
167     os << '\n';
168 
169     DumpRegister32(os, "edi", context.gregs[REG_EDI]);
170     DumpRegister32(os, "esi", context.gregs[REG_ESI]);
171     DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
172     DumpRegister32(os, "esp", context.gregs[REG_ESP]);
173     os << '\n';
174 
175     DumpRegister32(os, "eip", context.gregs[REG_EIP]);
176     os << "                   ";
177     DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
178     DumpX86Flags(os, context.gregs[REG_EFL]);
179     os << '\n';
180 
181     DumpRegister32(os, "cs",  context.gregs[REG_CS]);
182     DumpRegister32(os, "ds",  context.gregs[REG_DS]);
183     DumpRegister32(os, "es",  context.gregs[REG_ES]);
184     DumpRegister32(os, "fs",  context.gregs[REG_FS]);
185     os << '\n';
186     DumpRegister32(os, "gs",  context.gregs[REG_GS]);
187     DumpRegister32(os, "ss",  context.gregs[REG_SS]);
188 #endif
189   }
190 
DumpRegister32art::UContext191   void DumpRegister32(std::ostream& os, const char* name, uint32_t value) {
192     os << StringPrintf(" %6s: 0x%08x", name, value);
193   }
194 
DumpX86Flagsart::UContext195   void DumpX86Flags(std::ostream& os, uint32_t flags) {
196     os << " [";
197     if ((flags & (1 << 0)) != 0) {
198       os << " CF";
199     }
200     if ((flags & (1 << 2)) != 0) {
201       os << " PF";
202     }
203     if ((flags & (1 << 4)) != 0) {
204       os << " AF";
205     }
206     if ((flags & (1 << 6)) != 0) {
207       os << " ZF";
208     }
209     if ((flags & (1 << 7)) != 0) {
210       os << " SF";
211     }
212     if ((flags & (1 << 8)) != 0) {
213       os << " TF";
214     }
215     if ((flags & (1 << 9)) != 0) {
216       os << " IF";
217     }
218     if ((flags & (1 << 10)) != 0) {
219       os << " DF";
220     }
221     if ((flags & (1 << 11)) != 0) {
222       os << " OF";
223     }
224     os << " ]";
225   }
226 
227   mcontext_t& context;
228 };
229 
HandleUnexpectedSignal(int signal_number,siginfo_t * info,void * raw_context)230 void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
231   static bool handlingUnexpectedSignal = false;
232   if (handlingUnexpectedSignal) {
233     LogMessageData data(__FILE__, __LINE__, INTERNAL_FATAL, -1);
234     LogMessage::LogLine(data, "HandleUnexpectedSignal reentered\n");
235     _exit(1);
236   }
237   handlingUnexpectedSignal = true;
238 
239   gAborting++;  // set before taking any locks
240   MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
241 
242   bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
243                       signal_number == SIGFPE || signal_number == SIGSEGV);
244 
245   OsInfo os_info;
246   const char* cmd_line = GetCmdLine();
247   if (cmd_line == NULL) {
248     cmd_line = "<unset>";  // Because no-one called InitLogging.
249   }
250   pid_t tid = GetTid();
251   std::string thread_name(GetThreadName(tid));
252   UContext thread_context(raw_context);
253   Backtrace thread_backtrace;
254 
255   LOG(INTERNAL_FATAL) << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"
256                       << StringPrintf("Fatal signal %d (%s), code %d (%s)",
257                                       signal_number, GetSignalName(signal_number),
258                                       info->si_code,
259                                       GetSignalCodeName(signal_number, info->si_code))
260                       << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << "\n"
261                       << "OS: " << Dumpable<OsInfo>(os_info) << "\n"
262                       << "Cmdline: " << cmd_line << "\n"
263                       << "Thread: " << tid << " \"" << thread_name << "\"\n"
264                       << "Registers:\n" << Dumpable<UContext>(thread_context) << "\n"
265                       << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace);
266 
267   if (getenv("debug_db_uid") != NULL || getenv("art_wait_for_gdb_on_crash") != NULL) {
268     LOG(INTERNAL_FATAL) << "********************************************************\n"
269                         << "* Process " << getpid() << " thread " << tid << " \"" << thread_name << "\""
270                         << " has been suspended while crashing.\n"
271                         << "* Attach gdb:\n"
272                         << "*     gdb -p " << tid << "\n"
273                         << "********************************************************\n";
274     // Wait for debugger to attach.
275     while (true) {
276     }
277   }
278 
279   // Remove our signal handler for this signal...
280   struct sigaction action;
281   memset(&action, 0, sizeof(action));
282   sigemptyset(&action.sa_mask);
283   action.sa_handler = SIG_DFL;
284   sigaction(signal_number, &action, NULL);
285   // ...and re-raise so we die with the appropriate status.
286   kill(getpid(), signal_number);
287 }
288 
InitPlatformSignalHandlers()289 void Runtime::InitPlatformSignalHandlers() {
290   // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens.
291   struct sigaction action;
292   memset(&action, 0, sizeof(action));
293   sigemptyset(&action.sa_mask);
294   action.sa_sigaction = HandleUnexpectedSignal;
295   // Use the three-argument sa_sigaction handler.
296   action.sa_flags |= SA_SIGINFO;
297   // Use the alternate signal stack so we can catch stack overflows.
298   action.sa_flags |= SA_ONSTACK;
299 
300   int rc = 0;
301   rc += sigaction(SIGABRT, &action, NULL);
302   rc += sigaction(SIGBUS, &action, NULL);
303   rc += sigaction(SIGFPE, &action, NULL);
304   rc += sigaction(SIGILL, &action, NULL);
305   rc += sigaction(SIGPIPE, &action, NULL);
306   rc += sigaction(SIGSEGV, &action, NULL);
307 #if defined(SIGSTKFLT)
308   rc += sigaction(SIGSTKFLT, &action, NULL);
309 #endif
310   rc += sigaction(SIGTRAP, &action, NULL);
311   CHECK_EQ(rc, 0);
312 }
313 
314 }  // namespace art
315