blob: 00a0a39b83a332640dfa1d414ec93067e3689e06 [file] [log] [blame]
Andreas Gampe5dd44d02016-08-02 17:20:03 -07001/*
2 * Copyright (C) 2016 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 "native_stack_dump.h"
18
Yi Kongc57c6802018-10-29 14:28:56 -070019#include <memory>
Andreas Gampe5dd44d02016-08-02 17:20:03 -070020#include <ostream>
21
22#include <stdio.h>
23
24#include "art_method.h"
25
26// For DumpNativeStack.
27#include <backtrace/Backtrace.h>
28#include <backtrace/BacktraceMap.h>
29
30#if defined(__linux__)
31
32#include <memory>
33#include <vector>
34
35#include <linux/unistd.h>
Christopher Ferris453e0e52018-03-06 14:02:55 -080036#include <poll.h>
Andreas Gampe5dd44d02016-08-02 17:20:03 -070037#include <signal.h>
38#include <stdlib.h>
39#include <sys/time.h>
40#include <sys/types.h>
41
Andreas Gampe46ee31b2016-12-14 10:11:49 -080042#include "android-base/stringprintf.h"
Christopher Ferrisb1f23f92018-03-07 14:10:49 -080043#include "android-base/strings.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080044
Andreas Gampe5dd44d02016-08-02 17:20:03 -070045#include "arch/instruction_set.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080046#include "base/aborting.h"
David Sehr891a50e2017-10-27 17:01:07 -070047#include "base/file_utils.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070048#include "base/memory_tool.h"
49#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080050#include "base/os.h"
Andreas Gampefcccbaf2016-08-02 17:20:03 -070051#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080052#include "base/utils.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070053#include "oat_quick_method_header.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070054#include "thread-current-inl.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070055
56#endif
57
58namespace art {
59
60#if defined(__linux__)
61
Andreas Gampe46ee31b2016-12-14 10:11:49 -080062using android::base::StringPrintf;
63
Andreas Gampe5dd44d02016-08-02 17:20:03 -070064static constexpr bool kUseAddr2line = !kIsTargetBuild;
65
David Srbecky87da30e2019-01-30 15:51:23 +000066std::string FindAddr2line() {
67 if (!kIsTargetBuild) {
68 constexpr const char* kAddr2linePrebuiltPath =
69 "/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/bin/x86_64-linux-addr2line";
70 const char* env_value = getenv("ANDROID_BUILD_TOP");
71 if (env_value != nullptr) {
72 return std::string(env_value) + kAddr2linePrebuiltPath;
73 }
74 }
75 return std::string("/usr/bin/addr2line");
76}
77
Andreas Gampe5dd44d02016-08-02 17:20:03 -070078ALWAYS_INLINE
Andreas Gampefcccbaf2016-08-02 17:20:03 -070079static inline void WritePrefix(std::ostream& os, const char* prefix, bool odd) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -070080 if (prefix != nullptr) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -070081 os << prefix;
Andreas Gampe5dd44d02016-08-02 17:20:03 -070082 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -070083 os << " ";
Andreas Gampe5dd44d02016-08-02 17:20:03 -070084 if (!odd) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -070085 os << " ";
Andreas Gampe5dd44d02016-08-02 17:20:03 -070086 }
87}
88
Andreas Gampefcccbaf2016-08-02 17:20:03 -070089// The state of an open pipe to addr2line. In "server" mode, addr2line takes input on stdin
90// and prints the result to stdout. This struct keeps the state of the open connection.
91struct Addr2linePipe {
92 Addr2linePipe(int in_fd, int out_fd, const std::string& file_name, pid_t pid)
93 : in(in_fd, false), out(out_fd, false), file(file_name), child_pid(pid), odd(true) {}
94
95 ~Addr2linePipe() {
96 kill(child_pid, SIGKILL);
97 }
98
99 File in; // The file descriptor that is connected to the output of addr2line.
100 File out; // The file descriptor that is connected to the input of addr2line.
101
102 const std::string file; // The file addr2line is working on, so that we know when to close
103 // and restart.
104 const pid_t child_pid; // The pid of the child, which we should kill when we're done.
105 bool odd; // Print state for indentation of lines.
106};
107
108static std::unique_ptr<Addr2linePipe> Connect(const std::string& name, const char* args[]) {
109 int caller_to_addr2line[2];
110 int addr2line_to_caller[2];
111
112 if (pipe(caller_to_addr2line) == -1) {
113 return nullptr;
114 }
115 if (pipe(addr2line_to_caller) == -1) {
116 close(caller_to_addr2line[0]);
117 close(caller_to_addr2line[1]);
118 return nullptr;
119 }
120
121 pid_t pid = fork();
122 if (pid == -1) {
123 close(caller_to_addr2line[0]);
124 close(caller_to_addr2line[1]);
Calin Juravle0ed6c802017-03-27 18:12:05 -0700125 close(addr2line_to_caller[0]);
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700126 close(addr2line_to_caller[1]);
127 return nullptr;
128 }
129
130 if (pid == 0) {
131 dup2(caller_to_addr2line[0], STDIN_FILENO);
132 dup2(addr2line_to_caller[1], STDOUT_FILENO);
133
134 close(caller_to_addr2line[0]);
135 close(caller_to_addr2line[1]);
136 close(addr2line_to_caller[0]);
137 close(addr2line_to_caller[1]);
138
139 execv(args[0], const_cast<char* const*>(args));
140 exit(1);
141 } else {
142 close(caller_to_addr2line[0]);
143 close(addr2line_to_caller[1]);
Yi Kongc57c6802018-10-29 14:28:56 -0700144 return std::make_unique<Addr2linePipe>(addr2line_to_caller[0],
145 caller_to_addr2line[1],
146 name,
147 pid);
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700148 }
149}
150
151static void Drain(size_t expected,
152 const char* prefix,
153 std::unique_ptr<Addr2linePipe>* pipe /* inout */,
154 std::ostream& os) {
155 DCHECK(pipe != nullptr);
156 DCHECK(pipe->get() != nullptr);
157 int in = pipe->get()->in.Fd();
158 DCHECK_GE(in, 0);
159
160 bool prefix_written = false;
161
162 for (;;) {
Christopher Ferris453e0e52018-03-06 14:02:55 -0800163 constexpr uint32_t kWaitTimeExpectedMilli = 500;
164 constexpr uint32_t kWaitTimeUnexpectedMilli = 50;
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700165
Christopher Ferris453e0e52018-03-06 14:02:55 -0800166 int timeout = expected > 0 ? kWaitTimeExpectedMilli : kWaitTimeUnexpectedMilli;
167 struct pollfd read_fd{in, POLLIN, 0};
168 int retval = TEMP_FAILURE_RETRY(poll(&read_fd, 1, timeout));
169 if (retval == -1) {
170 // An error occurred.
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700171 pipe->reset();
172 return;
173 }
174
175 if (retval == 0) {
176 // Timeout.
177 return;
178 }
179
Christopher Ferris453e0e52018-03-06 14:02:55 -0800180 if (!(read_fd.revents & POLLIN)) {
181 // addr2line call exited.
182 pipe->reset();
183 return;
184 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700185
186 constexpr size_t kMaxBuffer = 128; // Relatively small buffer. Should be OK as we're on an
187 // alt stack, but just to be sure...
188 char buffer[kMaxBuffer];
189 memset(buffer, 0, kMaxBuffer);
190 int bytes_read = TEMP_FAILURE_RETRY(read(in, buffer, kMaxBuffer - 1));
Christopher Ferris453e0e52018-03-06 14:02:55 -0800191 if (bytes_read <= 0) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700192 // This should not really happen...
193 pipe->reset();
194 return;
195 }
Christopher Ferris453e0e52018-03-06 14:02:55 -0800196 buffer[bytes_read] = '\0';
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700197
198 char* tmp = buffer;
199 while (*tmp != 0) {
200 if (!prefix_written) {
201 WritePrefix(os, prefix, (*pipe)->odd);
202 prefix_written = true;
203 }
204 char* new_line = strchr(tmp, '\n');
205 if (new_line == nullptr) {
206 os << tmp;
207
208 break;
209 } else {
210 char saved = *(new_line + 1);
211 *(new_line + 1) = 0;
212 os << tmp;
213 *(new_line + 1) = saved;
214
215 tmp = new_line + 1;
216 prefix_written = false;
217 (*pipe)->odd = !(*pipe)->odd;
218
219 if (expected > 0) {
220 expected--;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700221 }
222 }
223 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700224 }
225}
226
227static void Addr2line(const std::string& map_src,
228 uintptr_t offset,
229 std::ostream& os,
230 const char* prefix,
231 std::unique_ptr<Addr2linePipe>* pipe /* inout */) {
232 DCHECK(pipe != nullptr);
233
Christopher Ferrisb1f23f92018-03-07 14:10:49 -0800234 if (map_src == "[vdso]" || android::base::EndsWith(map_src, ".vdex")) {
235 // addr2line will not work on the vdso.
236 // vdex files are special frames injected for the interpreter
237 // so they don't have any line number information available.
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700238 return;
239 }
240
241 if (*pipe == nullptr || (*pipe)->file != map_src) {
242 if (*pipe != nullptr) {
243 Drain(0, prefix, pipe, os);
244 }
245 pipe->reset(); // Close early.
246
David Srbecky87da30e2019-01-30 15:51:23 +0000247 std::string addr2linePath = FindAddr2line();
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700248 const char* args[7] = {
David Srbecky87da30e2019-01-30 15:51:23 +0000249 addr2linePath.c_str(),
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700250 "--functions",
251 "--inlines",
252 "--demangle",
253 "-e",
254 map_src.c_str(),
255 nullptr
256 };
257 *pipe = Connect(map_src, args);
258 }
259
260 Addr2linePipe* pipe_ptr = pipe->get();
261 if (pipe_ptr == nullptr) {
262 // Failed...
263 return;
264 }
265
266 // Send the offset.
267 const std::string hex_offset = StringPrintf("%zx\n", offset);
268
269 if (!pipe_ptr->out.WriteFully(hex_offset.data(), hex_offset.length())) {
270 // Error. :-(
271 pipe->reset();
272 return;
273 }
274
275 // Now drain (expecting two lines).
276 Drain(2U, prefix, pipe, os);
277}
278
Andreas Gampeca620d72016-11-08 08:09:33 -0800279static bool RunCommand(const std::string& cmd) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700280 FILE* stream = popen(cmd.c_str(), "r");
281 if (stream) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700282 pclose(stream);
283 return true;
284 } else {
285 return false;
286 }
287}
288
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700289static bool PcIsWithinQuickCode(ArtMethod* method, uintptr_t pc) NO_THREAD_SAFETY_ANALYSIS {
290 uintptr_t code = reinterpret_cast<uintptr_t>(EntryPointToCodePointer(
291 method->GetEntryPointFromQuickCompiledCode()));
292 if (code == 0) {
293 return pc == 0;
294 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700295 uintptr_t code_size = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].GetCodeSize();
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700296 return code <= pc && pc <= (code + code_size);
297}
298
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700299void DumpNativeStack(std::ostream& os,
300 pid_t tid,
301 BacktraceMap* existing_map,
302 const char* prefix,
303 ArtMethod* current_method,
Christopher Ferrisb2749312018-03-23 13:03:45 -0700304 void* ucontext_ptr,
305 bool skip_frames) {
Roland Levillain05e34f42018-05-24 13:19:05 +0000306 // Historical note: This was disabled when running under Valgrind (b/18119146).
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700307
308 BacktraceMap* map = existing_map;
309 std::unique_ptr<BacktraceMap> tmp_map;
310 if (map == nullptr) {
311 tmp_map.reset(BacktraceMap::Create(getpid()));
312 map = tmp_map.get();
313 }
314 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid, map));
Christopher Ferrisb2749312018-03-23 13:03:45 -0700315 backtrace->SetSkipFrames(skip_frames);
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700316 if (!backtrace->Unwind(0, reinterpret_cast<ucontext*>(ucontext_ptr))) {
317 os << prefix << "(backtrace::Unwind failed for thread " << tid
Andreas Gampeef295362016-10-11 20:04:11 -0700318 << ": " << backtrace->GetErrorString(backtrace->GetError()) << ")" << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700319 return;
320 } else if (backtrace->NumFrames() == 0) {
Andreas Gampeef295362016-10-11 20:04:11 -0700321 os << prefix << "(no native stack frames for thread " << tid << ")" << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700322 return;
323 }
324
325 // Check whether we have and should use addr2line.
326 bool use_addr2line;
327 if (kUseAddr2line) {
328 // Try to run it to see whether we have it. Push an argument so that it doesn't assume a.out
329 // and print to stderr.
David Srbecky87da30e2019-01-30 15:51:23 +0000330 use_addr2line = (gAborting > 0) && RunCommand(FindAddr2line() + " -h");
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700331 } else {
332 use_addr2line = false;
333 }
334
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700335 std::unique_ptr<Addr2linePipe> addr2line_state;
336
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700337 for (Backtrace::const_iterator it = backtrace->begin();
338 it != backtrace->end(); ++it) {
339 // We produce output like this:
340 // ] #00 pc 000075bb8 /system/lib/libc.so (unwind_backtrace_thread+536)
341 // In order for parsing tools to continue to function, the stack dump
342 // format must at least adhere to this format:
343 // #XX pc <RELATIVE_ADDR> <FULL_PATH_TO_SHARED_LIBRARY> ...
344 // The parsers require a single space before and after pc, and two spaces
345 // after the <RELATIVE_ADDR>. There can be any prefix data before the
346 // #XX. <RELATIVE_ADDR> has to be a hex number but with no 0x prefix.
347 os << prefix << StringPrintf("#%02zu pc ", it->num);
348 bool try_addr2line = false;
349 if (!BacktraceMap::IsValid(it->map)) {
Christopher Ferris77b38df2018-01-18 16:16:49 -0800350 os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 " ???"
351 : "%08" PRIx64 " ???",
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700352 it->pc);
353 } else {
Christopher Ferris77b38df2018-01-18 16:16:49 -0800354 os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 " "
355 : "%08" PRIx64 " ",
Christopher Ferrisf88b5c02017-07-19 14:18:33 -0700356 it->rel_pc);
Christopher Ferris8bd7d1b2018-01-08 11:12:40 -0800357 if (it->map.name.empty()) {
Christopher Ferris77b38df2018-01-18 16:16:49 -0800358 os << StringPrintf("<anonymous:%" PRIx64 ">", it->map.start);
Christopher Ferris8bd7d1b2018-01-08 11:12:40 -0800359 } else {
360 os << it->map.name;
361 }
Christopher Ferris53ef6a62018-02-09 23:13:27 -0800362 if (it->map.offset != 0) {
363 os << StringPrintf(" (offset %" PRIx64 ")", it->map.offset);
364 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700365 os << " (";
366 if (!it->func_name.empty()) {
367 os << it->func_name;
368 if (it->func_offset != 0) {
369 os << "+" << it->func_offset;
370 }
Christopher Ferris8bd7d1b2018-01-08 11:12:40 -0800371 // Functions found using the gdb jit interface will be in an empty
372 // map that cannot be found using addr2line.
373 if (!it->map.name.empty()) {
374 try_addr2line = true;
375 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700376 } else if (current_method != nullptr &&
377 Locks::mutator_lock_->IsSharedHeld(Thread::Current()) &&
378 PcIsWithinQuickCode(current_method, it->pc)) {
379 const void* start_of_code = current_method->GetEntryPointFromQuickCompiledCode();
David Sehr709b0702016-10-13 09:12:37 -0700380 os << current_method->JniLongName() << "+"
Christopher Ferris77b38df2018-01-18 16:16:49 -0800381 << (it->pc - reinterpret_cast<uint64_t>(start_of_code));
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700382 } else {
383 os << "???";
384 }
385 os << ")";
386 }
Andreas Gampeef295362016-10-11 20:04:11 -0700387 os << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700388 if (try_addr2line && use_addr2line) {
Andreas Gampe15a678a2018-11-06 17:14:10 -0800389 Addr2line(it->map.name, it->rel_pc, os, prefix, &addr2line_state);
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700390 }
391 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700392
393 if (addr2line_state != nullptr) {
394 Drain(0, prefix, &addr2line_state, os);
395 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700396}
397
398void DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) {
399 if (tid == GetTid()) {
400 // There's no point showing that we're reading our stack out of /proc!
401 return;
402 }
403
404 std::string kernel_stack_filename(StringPrintf("/proc/self/task/%d/stack", tid));
405 std::string kernel_stack;
406 if (!ReadFileToString(kernel_stack_filename, &kernel_stack)) {
407 os << prefix << "(couldn't read " << kernel_stack_filename << ")\n";
408 return;
409 }
410
411 std::vector<std::string> kernel_stack_frames;
412 Split(kernel_stack, '\n', &kernel_stack_frames);
yuanhao7a8b3f22018-02-01 15:58:53 +0800413 if (kernel_stack_frames.empty()) {
414 os << prefix << "(" << kernel_stack_filename << " is empty)\n";
415 return;
416 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700417 // We skip the last stack frame because it's always equivalent to "[<ffffffff>] 0xffffffff",
418 // which looking at the source appears to be the kernel's way of saying "that's all, folks!".
419 kernel_stack_frames.pop_back();
420 for (size_t i = 0; i < kernel_stack_frames.size(); ++i) {
421 // Turn "[<ffffffff8109156d>] futex_wait_queue_me+0xcd/0x110"
422 // into "futex_wait_queue_me+0xcd/0x110".
423 const char* text = kernel_stack_frames[i].c_str();
424 const char* close_bracket = strchr(text, ']');
425 if (close_bracket != nullptr) {
426 text = close_bracket + 2;
427 }
428 os << prefix;
429 if (include_count) {
430 os << StringPrintf("#%02zd ", i);
431 }
Andreas Gampeef295362016-10-11 20:04:11 -0700432 os << text << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700433 }
434}
435
436#elif defined(__APPLE__)
437
438void DumpNativeStack(std::ostream& os ATTRIBUTE_UNUSED,
439 pid_t tid ATTRIBUTE_UNUSED,
440 BacktraceMap* existing_map ATTRIBUTE_UNUSED,
441 const char* prefix ATTRIBUTE_UNUSED,
442 ArtMethod* current_method ATTRIBUTE_UNUSED,
Christopher Ferrisa0b25272018-03-27 17:04:44 -0700443 void* ucontext_ptr ATTRIBUTE_UNUSED,
444 bool skip_frames ATTRIBUTE_UNUSED) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700445}
446
447void DumpKernelStack(std::ostream& os ATTRIBUTE_UNUSED,
448 pid_t tid ATTRIBUTE_UNUSED,
449 const char* prefix ATTRIBUTE_UNUSED,
450 bool include_count ATTRIBUTE_UNUSED) {
451}
452
453#else
454#error "Unsupported architecture for native stack dumps."
455#endif
456
457} // namespace art