blob: 14f3f45f9e006cda2f24b46c2aa4b883fdc86a48 [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
19#include <ostream>
20
21#include <stdio.h>
22
23#include "art_method.h"
24
25// For DumpNativeStack.
26#include <backtrace/Backtrace.h>
27#include <backtrace/BacktraceMap.h>
28
29#if defined(__linux__)
30
31#include <memory>
32#include <vector>
33
34#include <linux/unistd.h>
Christopher Ferris5e074ed2018-03-06 14:02:55 -080035#include <poll.h>
Andreas Gampe5dd44d02016-08-02 17:20:03 -070036#include <signal.h>
37#include <stdlib.h>
38#include <sys/time.h>
39#include <sys/types.h>
40
Andreas Gampe46ee31b2016-12-14 10:11:49 -080041#include "android-base/stringprintf.h"
Christopher Ferris0f0cb322018-03-07 14:10:49 -080042#include "android-base/strings.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080043
Andreas Gampe5dd44d02016-08-02 17:20:03 -070044#include "arch/instruction_set.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080045#include "base/aborting.h"
David Sehr891a50e2017-10-27 17:01:07 -070046#include "base/file_utils.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070047#include "base/memory_tool.h"
48#include "base/mutex.h"
David Sehr8f4b0562018-03-02 12:01:51 -080049#include "base/os.h"
Andreas Gampefcccbaf2016-08-02 17:20:03 -070050#include "base/unix_file/fd_file.h"
David Sehr8f4b0562018-03-02 12:01:51 -080051#include "base/utils.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070052#include "oat_quick_method_header.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070053#include "thread-current-inl.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070054
55#endif
56
57namespace art {
58
59#if defined(__linux__)
60
Andreas Gampe46ee31b2016-12-14 10:11:49 -080061using android::base::StringPrintf;
62
Andreas Gampe5dd44d02016-08-02 17:20:03 -070063static constexpr bool kUseAddr2line = !kIsTargetBuild;
64
65ALWAYS_INLINE
Andreas Gampefcccbaf2016-08-02 17:20:03 -070066static inline void WritePrefix(std::ostream& os, const char* prefix, bool odd) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -070067 if (prefix != nullptr) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -070068 os << prefix;
Andreas Gampe5dd44d02016-08-02 17:20:03 -070069 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -070070 os << " ";
Andreas Gampe5dd44d02016-08-02 17:20:03 -070071 if (!odd) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -070072 os << " ";
Andreas Gampe5dd44d02016-08-02 17:20:03 -070073 }
74}
75
Andreas Gampefcccbaf2016-08-02 17:20:03 -070076// The state of an open pipe to addr2line. In "server" mode, addr2line takes input on stdin
77// and prints the result to stdout. This struct keeps the state of the open connection.
78struct Addr2linePipe {
79 Addr2linePipe(int in_fd, int out_fd, const std::string& file_name, pid_t pid)
80 : in(in_fd, false), out(out_fd, false), file(file_name), child_pid(pid), odd(true) {}
81
82 ~Addr2linePipe() {
83 kill(child_pid, SIGKILL);
84 }
85
86 File in; // The file descriptor that is connected to the output of addr2line.
87 File out; // The file descriptor that is connected to the input of addr2line.
88
89 const std::string file; // The file addr2line is working on, so that we know when to close
90 // and restart.
91 const pid_t child_pid; // The pid of the child, which we should kill when we're done.
92 bool odd; // Print state for indentation of lines.
93};
94
95static std::unique_ptr<Addr2linePipe> Connect(const std::string& name, const char* args[]) {
96 int caller_to_addr2line[2];
97 int addr2line_to_caller[2];
98
99 if (pipe(caller_to_addr2line) == -1) {
100 return nullptr;
101 }
102 if (pipe(addr2line_to_caller) == -1) {
103 close(caller_to_addr2line[0]);
104 close(caller_to_addr2line[1]);
105 return nullptr;
106 }
107
108 pid_t pid = fork();
109 if (pid == -1) {
110 close(caller_to_addr2line[0]);
111 close(caller_to_addr2line[1]);
Calin Juravle0ed6c802017-03-27 18:12:05 -0700112 close(addr2line_to_caller[0]);
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700113 close(addr2line_to_caller[1]);
114 return nullptr;
115 }
116
117 if (pid == 0) {
118 dup2(caller_to_addr2line[0], STDIN_FILENO);
119 dup2(addr2line_to_caller[1], STDOUT_FILENO);
120
121 close(caller_to_addr2line[0]);
122 close(caller_to_addr2line[1]);
123 close(addr2line_to_caller[0]);
124 close(addr2line_to_caller[1]);
125
126 execv(args[0], const_cast<char* const*>(args));
127 exit(1);
128 } else {
129 close(caller_to_addr2line[0]);
130 close(addr2line_to_caller[1]);
131 return std::unique_ptr<Addr2linePipe>(new Addr2linePipe(addr2line_to_caller[0],
132 caller_to_addr2line[1],
133 name,
134 pid));
135 }
136}
137
138static void Drain(size_t expected,
139 const char* prefix,
140 std::unique_ptr<Addr2linePipe>* pipe /* inout */,
141 std::ostream& os) {
142 DCHECK(pipe != nullptr);
143 DCHECK(pipe->get() != nullptr);
144 int in = pipe->get()->in.Fd();
145 DCHECK_GE(in, 0);
146
147 bool prefix_written = false;
148
149 for (;;) {
Christopher Ferris5e074ed2018-03-06 14:02:55 -0800150 constexpr uint32_t kWaitTimeExpectedMilli = 500;
151 constexpr uint32_t kWaitTimeUnexpectedMilli = 50;
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700152
Christopher Ferris5e074ed2018-03-06 14:02:55 -0800153 int timeout = expected > 0 ? kWaitTimeExpectedMilli : kWaitTimeUnexpectedMilli;
154 struct pollfd read_fd{in, POLLIN, 0};
155 int retval = TEMP_FAILURE_RETRY(poll(&read_fd, 1, timeout));
156 if (retval == -1) {
157 // An error occurred.
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700158 pipe->reset();
159 return;
160 }
161
162 if (retval == 0) {
163 // Timeout.
164 return;
165 }
166
Christopher Ferris5e074ed2018-03-06 14:02:55 -0800167 if (!(read_fd.revents & POLLIN)) {
168 // addr2line call exited.
169 pipe->reset();
170 return;
171 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700172
173 constexpr size_t kMaxBuffer = 128; // Relatively small buffer. Should be OK as we're on an
174 // alt stack, but just to be sure...
175 char buffer[kMaxBuffer];
176 memset(buffer, 0, kMaxBuffer);
177 int bytes_read = TEMP_FAILURE_RETRY(read(in, buffer, kMaxBuffer - 1));
Christopher Ferris5e074ed2018-03-06 14:02:55 -0800178 if (bytes_read <= 0) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700179 // This should not really happen...
180 pipe->reset();
181 return;
182 }
Christopher Ferris5e074ed2018-03-06 14:02:55 -0800183 buffer[bytes_read] = '\0';
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700184
185 char* tmp = buffer;
186 while (*tmp != 0) {
187 if (!prefix_written) {
188 WritePrefix(os, prefix, (*pipe)->odd);
189 prefix_written = true;
190 }
191 char* new_line = strchr(tmp, '\n');
192 if (new_line == nullptr) {
193 os << tmp;
194
195 break;
196 } else {
197 char saved = *(new_line + 1);
198 *(new_line + 1) = 0;
199 os << tmp;
200 *(new_line + 1) = saved;
201
202 tmp = new_line + 1;
203 prefix_written = false;
204 (*pipe)->odd = !(*pipe)->odd;
205
206 if (expected > 0) {
207 expected--;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700208 }
209 }
210 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700211 }
212}
213
214static void Addr2line(const std::string& map_src,
215 uintptr_t offset,
216 std::ostream& os,
217 const char* prefix,
218 std::unique_ptr<Addr2linePipe>* pipe /* inout */) {
219 DCHECK(pipe != nullptr);
220
Christopher Ferris0f0cb322018-03-07 14:10:49 -0800221 if (map_src == "[vdso]" || android::base::EndsWith(map_src, ".vdex")) {
222 // addr2line will not work on the vdso.
223 // vdex files are special frames injected for the interpreter
224 // so they don't have any line number information available.
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700225 return;
226 }
227
228 if (*pipe == nullptr || (*pipe)->file != map_src) {
229 if (*pipe != nullptr) {
230 Drain(0, prefix, pipe, os);
231 }
232 pipe->reset(); // Close early.
233
234 const char* args[7] = {
235 "/usr/bin/addr2line",
236 "--functions",
237 "--inlines",
238 "--demangle",
239 "-e",
240 map_src.c_str(),
241 nullptr
242 };
243 *pipe = Connect(map_src, args);
244 }
245
246 Addr2linePipe* pipe_ptr = pipe->get();
247 if (pipe_ptr == nullptr) {
248 // Failed...
249 return;
250 }
251
252 // Send the offset.
253 const std::string hex_offset = StringPrintf("%zx\n", offset);
254
255 if (!pipe_ptr->out.WriteFully(hex_offset.data(), hex_offset.length())) {
256 // Error. :-(
257 pipe->reset();
258 return;
259 }
260
261 // Now drain (expecting two lines).
262 Drain(2U, prefix, pipe, os);
263}
264
Andreas Gampeca620d72016-11-08 08:09:33 -0800265static bool RunCommand(const std::string& cmd) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700266 FILE* stream = popen(cmd.c_str(), "r");
267 if (stream) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700268 pclose(stream);
269 return true;
270 } else {
271 return false;
272 }
273}
274
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700275static bool PcIsWithinQuickCode(ArtMethod* method, uintptr_t pc) NO_THREAD_SAFETY_ANALYSIS {
276 uintptr_t code = reinterpret_cast<uintptr_t>(EntryPointToCodePointer(
277 method->GetEntryPointFromQuickCompiledCode()));
278 if (code == 0) {
279 return pc == 0;
280 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700281 uintptr_t code_size = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].GetCodeSize();
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700282 return code <= pc && pc <= (code + code_size);
283}
284
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700285void DumpNativeStack(std::ostream& os,
286 pid_t tid,
287 BacktraceMap* existing_map,
288 const char* prefix,
289 ArtMethod* current_method,
Christopher Ferris55ecc9f2018-03-23 13:03:45 -0700290 void* ucontext_ptr,
291 bool skip_frames) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700292 // b/18119146
293 if (RUNNING_ON_MEMORY_TOOL != 0) {
294 return;
295 }
296
297 BacktraceMap* map = existing_map;
298 std::unique_ptr<BacktraceMap> tmp_map;
299 if (map == nullptr) {
300 tmp_map.reset(BacktraceMap::Create(getpid()));
301 map = tmp_map.get();
302 }
303 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid, map));
Christopher Ferris55ecc9f2018-03-23 13:03:45 -0700304 backtrace->SetSkipFrames(skip_frames);
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700305 if (!backtrace->Unwind(0, reinterpret_cast<ucontext*>(ucontext_ptr))) {
306 os << prefix << "(backtrace::Unwind failed for thread " << tid
Andreas Gampeef295362016-10-11 20:04:11 -0700307 << ": " << backtrace->GetErrorString(backtrace->GetError()) << ")" << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700308 return;
309 } else if (backtrace->NumFrames() == 0) {
Andreas Gampeef295362016-10-11 20:04:11 -0700310 os << prefix << "(no native stack frames for thread " << tid << ")" << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700311 return;
312 }
313
314 // Check whether we have and should use addr2line.
315 bool use_addr2line;
316 if (kUseAddr2line) {
317 // Try to run it to see whether we have it. Push an argument so that it doesn't assume a.out
318 // and print to stderr.
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700319 use_addr2line = (gAborting > 0) && RunCommand("addr2line -h");
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700320 } else {
321 use_addr2line = false;
322 }
323
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700324 std::unique_ptr<Addr2linePipe> addr2line_state;
325
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700326 for (Backtrace::const_iterator it = backtrace->begin();
327 it != backtrace->end(); ++it) {
328 // We produce output like this:
329 // ] #00 pc 000075bb8 /system/lib/libc.so (unwind_backtrace_thread+536)
330 // In order for parsing tools to continue to function, the stack dump
331 // format must at least adhere to this format:
332 // #XX pc <RELATIVE_ADDR> <FULL_PATH_TO_SHARED_LIBRARY> ...
333 // The parsers require a single space before and after pc, and two spaces
334 // after the <RELATIVE_ADDR>. There can be any prefix data before the
335 // #XX. <RELATIVE_ADDR> has to be a hex number but with no 0x prefix.
336 os << prefix << StringPrintf("#%02zu pc ", it->num);
337 bool try_addr2line = false;
338 if (!BacktraceMap::IsValid(it->map)) {
Christopher Ferris77b38df2018-01-18 16:16:49 -0800339 os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 " ???"
340 : "%08" PRIx64 " ???",
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700341 it->pc);
342 } else {
Christopher Ferris77b38df2018-01-18 16:16:49 -0800343 os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 " "
344 : "%08" PRIx64 " ",
Christopher Ferrisf88b5c02017-07-19 14:18:33 -0700345 it->rel_pc);
Christopher Ferris8bd7d1b2018-01-08 11:12:40 -0800346 if (it->map.name.empty()) {
Christopher Ferris77b38df2018-01-18 16:16:49 -0800347 os << StringPrintf("<anonymous:%" PRIx64 ">", it->map.start);
Christopher Ferris8bd7d1b2018-01-08 11:12:40 -0800348 } else {
349 os << it->map.name;
350 }
Christopher Ferris53ef6a62018-02-09 23:13:27 -0800351 if (it->map.offset != 0) {
352 os << StringPrintf(" (offset %" PRIx64 ")", it->map.offset);
353 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700354 os << " (";
355 if (!it->func_name.empty()) {
356 os << it->func_name;
357 if (it->func_offset != 0) {
358 os << "+" << it->func_offset;
359 }
Christopher Ferris8bd7d1b2018-01-08 11:12:40 -0800360 // Functions found using the gdb jit interface will be in an empty
361 // map that cannot be found using addr2line.
362 if (!it->map.name.empty()) {
363 try_addr2line = true;
364 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700365 } else if (current_method != nullptr &&
366 Locks::mutator_lock_->IsSharedHeld(Thread::Current()) &&
367 PcIsWithinQuickCode(current_method, it->pc)) {
368 const void* start_of_code = current_method->GetEntryPointFromQuickCompiledCode();
David Sehr709b0702016-10-13 09:12:37 -0700369 os << current_method->JniLongName() << "+"
Christopher Ferris77b38df2018-01-18 16:16:49 -0800370 << (it->pc - reinterpret_cast<uint64_t>(start_of_code));
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700371 } else {
372 os << "???";
373 }
374 os << ")";
375 }
Andreas Gampeef295362016-10-11 20:04:11 -0700376 os << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700377 if (try_addr2line && use_addr2line) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700378 Addr2line(it->map.name, it->pc - it->map.start, os, prefix, &addr2line_state);
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700379 }
380 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700381
382 if (addr2line_state != nullptr) {
383 Drain(0, prefix, &addr2line_state, os);
384 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700385}
386
387void DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) {
388 if (tid == GetTid()) {
389 // There's no point showing that we're reading our stack out of /proc!
390 return;
391 }
392
393 std::string kernel_stack_filename(StringPrintf("/proc/self/task/%d/stack", tid));
394 std::string kernel_stack;
395 if (!ReadFileToString(kernel_stack_filename, &kernel_stack)) {
396 os << prefix << "(couldn't read " << kernel_stack_filename << ")\n";
397 return;
398 }
399
400 std::vector<std::string> kernel_stack_frames;
401 Split(kernel_stack, '\n', &kernel_stack_frames);
yuanhao7a8b3f22018-02-01 15:58:53 +0800402 if (kernel_stack_frames.empty()) {
403 os << prefix << "(" << kernel_stack_filename << " is empty)\n";
404 return;
405 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700406 // We skip the last stack frame because it's always equivalent to "[<ffffffff>] 0xffffffff",
407 // which looking at the source appears to be the kernel's way of saying "that's all, folks!".
408 kernel_stack_frames.pop_back();
409 for (size_t i = 0; i < kernel_stack_frames.size(); ++i) {
410 // Turn "[<ffffffff8109156d>] futex_wait_queue_me+0xcd/0x110"
411 // into "futex_wait_queue_me+0xcd/0x110".
412 const char* text = kernel_stack_frames[i].c_str();
413 const char* close_bracket = strchr(text, ']');
414 if (close_bracket != nullptr) {
415 text = close_bracket + 2;
416 }
417 os << prefix;
418 if (include_count) {
419 os << StringPrintf("#%02zd ", i);
420 }
Andreas Gampeef295362016-10-11 20:04:11 -0700421 os << text << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700422 }
423}
424
425#elif defined(__APPLE__)
426
427void DumpNativeStack(std::ostream& os ATTRIBUTE_UNUSED,
428 pid_t tid ATTRIBUTE_UNUSED,
429 BacktraceMap* existing_map ATTRIBUTE_UNUSED,
430 const char* prefix ATTRIBUTE_UNUSED,
431 ArtMethod* current_method ATTRIBUTE_UNUSED,
Christopher Ferrisa89c8322018-03-27 17:04:44 -0700432 void* ucontext_ptr ATTRIBUTE_UNUSED,
433 bool skip_frames ATTRIBUTE_UNUSED) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700434}
435
436void DumpKernelStack(std::ostream& os ATTRIBUTE_UNUSED,
437 pid_t tid ATTRIBUTE_UNUSED,
438 const char* prefix ATTRIBUTE_UNUSED,
439 bool include_count ATTRIBUTE_UNUSED) {
440}
441
442#else
443#error "Unsupported architecture for native stack dumps."
444#endif
445
446} // namespace art