blob: 4d4bab764ce3347b9525d798000d75647c8dfcc9 [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>
35#include <signal.h>
36#include <stdlib.h>
37#include <sys/time.h>
38#include <sys/types.h>
39
Andreas Gampe46ee31b2016-12-14 10:11:49 -080040#include "android-base/stringprintf.h"
41
Andreas Gampe5dd44d02016-08-02 17:20:03 -070042#include "arch/instruction_set.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080043#include "base/aborting.h"
David Sehr891a50e2017-10-27 17:01:07 -070044#include "base/file_utils.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070045#include "base/memory_tool.h"
46#include "base/mutex.h"
Andreas Gampefcccbaf2016-08-02 17:20:03 -070047#include "base/unix_file/fd_file.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070048#include "oat_quick_method_header.h"
Andreas Gampefcccbaf2016-08-02 17:20:03 -070049#include "os.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070050#include "thread-current-inl.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070051#include "utils.h"
52
53#endif
54
55namespace art {
56
57#if defined(__linux__)
58
Andreas Gampe46ee31b2016-12-14 10:11:49 -080059using android::base::StringPrintf;
60
Andreas Gampe5dd44d02016-08-02 17:20:03 -070061static constexpr bool kUseAddr2line = !kIsTargetBuild;
62
63ALWAYS_INLINE
Andreas Gampefcccbaf2016-08-02 17:20:03 -070064static inline void WritePrefix(std::ostream& os, const char* prefix, bool odd) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -070065 if (prefix != nullptr) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -070066 os << prefix;
Andreas Gampe5dd44d02016-08-02 17:20:03 -070067 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -070068 os << " ";
Andreas Gampe5dd44d02016-08-02 17:20:03 -070069 if (!odd) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -070070 os << " ";
Andreas Gampe5dd44d02016-08-02 17:20:03 -070071 }
72}
73
Andreas Gampefcccbaf2016-08-02 17:20:03 -070074// The state of an open pipe to addr2line. In "server" mode, addr2line takes input on stdin
75// and prints the result to stdout. This struct keeps the state of the open connection.
76struct Addr2linePipe {
77 Addr2linePipe(int in_fd, int out_fd, const std::string& file_name, pid_t pid)
78 : in(in_fd, false), out(out_fd, false), file(file_name), child_pid(pid), odd(true) {}
79
80 ~Addr2linePipe() {
81 kill(child_pid, SIGKILL);
82 }
83
84 File in; // The file descriptor that is connected to the output of addr2line.
85 File out; // The file descriptor that is connected to the input of addr2line.
86
87 const std::string file; // The file addr2line is working on, so that we know when to close
88 // and restart.
89 const pid_t child_pid; // The pid of the child, which we should kill when we're done.
90 bool odd; // Print state for indentation of lines.
91};
92
93static std::unique_ptr<Addr2linePipe> Connect(const std::string& name, const char* args[]) {
94 int caller_to_addr2line[2];
95 int addr2line_to_caller[2];
96
97 if (pipe(caller_to_addr2line) == -1) {
98 return nullptr;
99 }
100 if (pipe(addr2line_to_caller) == -1) {
101 close(caller_to_addr2line[0]);
102 close(caller_to_addr2line[1]);
103 return nullptr;
104 }
105
106 pid_t pid = fork();
107 if (pid == -1) {
108 close(caller_to_addr2line[0]);
109 close(caller_to_addr2line[1]);
Calin Juravle0ed6c802017-03-27 18:12:05 -0700110 close(addr2line_to_caller[0]);
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700111 close(addr2line_to_caller[1]);
112 return nullptr;
113 }
114
115 if (pid == 0) {
116 dup2(caller_to_addr2line[0], STDIN_FILENO);
117 dup2(addr2line_to_caller[1], STDOUT_FILENO);
118
119 close(caller_to_addr2line[0]);
120 close(caller_to_addr2line[1]);
121 close(addr2line_to_caller[0]);
122 close(addr2line_to_caller[1]);
123
124 execv(args[0], const_cast<char* const*>(args));
125 exit(1);
126 } else {
127 close(caller_to_addr2line[0]);
128 close(addr2line_to_caller[1]);
129 return std::unique_ptr<Addr2linePipe>(new Addr2linePipe(addr2line_to_caller[0],
130 caller_to_addr2line[1],
131 name,
132 pid));
133 }
134}
135
136static void Drain(size_t expected,
137 const char* prefix,
138 std::unique_ptr<Addr2linePipe>* pipe /* inout */,
139 std::ostream& os) {
140 DCHECK(pipe != nullptr);
141 DCHECK(pipe->get() != nullptr);
142 int in = pipe->get()->in.Fd();
143 DCHECK_GE(in, 0);
144
145 bool prefix_written = false;
146
147 for (;;) {
148 constexpr uint32_t kWaitTimeExpectedMicros = 500 * 1000;
149 constexpr uint32_t kWaitTimeUnexpectedMicros = 50 * 1000;
150
151 struct timeval tv;
152 tv.tv_sec = 0;
153 tv.tv_usec = expected > 0 ? kWaitTimeExpectedMicros : kWaitTimeUnexpectedMicros;
154
155 fd_set rfds;
156 FD_ZERO(&rfds);
157 FD_SET(in, &rfds);
158
159 int retval = TEMP_FAILURE_RETRY(select(in + 1, &rfds, nullptr, nullptr, &tv));
160
161 if (retval < 0) {
162 // Other side may have crashed or other errors.
163 pipe->reset();
164 return;
165 }
166
167 if (retval == 0) {
168 // Timeout.
169 return;
170 }
171
172 DCHECK_EQ(retval, 1);
173
174 constexpr size_t kMaxBuffer = 128; // Relatively small buffer. Should be OK as we're on an
175 // alt stack, but just to be sure...
176 char buffer[kMaxBuffer];
177 memset(buffer, 0, kMaxBuffer);
178 int bytes_read = TEMP_FAILURE_RETRY(read(in, buffer, kMaxBuffer - 1));
179
180 if (bytes_read < 0) {
181 // This should not really happen...
182 pipe->reset();
183 return;
184 }
185
186 char* tmp = buffer;
187 while (*tmp != 0) {
188 if (!prefix_written) {
189 WritePrefix(os, prefix, (*pipe)->odd);
190 prefix_written = true;
191 }
192 char* new_line = strchr(tmp, '\n');
193 if (new_line == nullptr) {
194 os << tmp;
195
196 break;
197 } else {
198 char saved = *(new_line + 1);
199 *(new_line + 1) = 0;
200 os << tmp;
201 *(new_line + 1) = saved;
202
203 tmp = new_line + 1;
204 prefix_written = false;
205 (*pipe)->odd = !(*pipe)->odd;
206
207 if (expected > 0) {
208 expected--;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700209 }
210 }
211 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700212 }
213}
214
215static void Addr2line(const std::string& map_src,
216 uintptr_t offset,
217 std::ostream& os,
218 const char* prefix,
219 std::unique_ptr<Addr2linePipe>* pipe /* inout */) {
220 DCHECK(pipe != nullptr);
221
222 if (map_src == "[vdso]") {
223 // Special-case this, our setup has problems with this.
224 return;
225 }
226
227 if (*pipe == nullptr || (*pipe)->file != map_src) {
228 if (*pipe != nullptr) {
229 Drain(0, prefix, pipe, os);
230 }
231 pipe->reset(); // Close early.
232
233 const char* args[7] = {
234 "/usr/bin/addr2line",
235 "--functions",
236 "--inlines",
237 "--demangle",
238 "-e",
239 map_src.c_str(),
240 nullptr
241 };
242 *pipe = Connect(map_src, args);
243 }
244
245 Addr2linePipe* pipe_ptr = pipe->get();
246 if (pipe_ptr == nullptr) {
247 // Failed...
248 return;
249 }
250
251 // Send the offset.
252 const std::string hex_offset = StringPrintf("%zx\n", offset);
253
254 if (!pipe_ptr->out.WriteFully(hex_offset.data(), hex_offset.length())) {
255 // Error. :-(
256 pipe->reset();
257 return;
258 }
259
260 // Now drain (expecting two lines).
261 Drain(2U, prefix, pipe, os);
262}
263
Andreas Gampeca620d72016-11-08 08:09:33 -0800264static bool RunCommand(const std::string& cmd) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700265 FILE* stream = popen(cmd.c_str(), "r");
266 if (stream) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700267 pclose(stream);
268 return true;
269 } else {
270 return false;
271 }
272}
273
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700274static bool PcIsWithinQuickCode(ArtMethod* method, uintptr_t pc) NO_THREAD_SAFETY_ANALYSIS {
275 uintptr_t code = reinterpret_cast<uintptr_t>(EntryPointToCodePointer(
276 method->GetEntryPointFromQuickCompiledCode()));
277 if (code == 0) {
278 return pc == 0;
279 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700280 uintptr_t code_size = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].GetCodeSize();
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700281 return code <= pc && pc <= (code + code_size);
282}
283
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700284void DumpNativeStack(std::ostream& os,
285 pid_t tid,
286 BacktraceMap* existing_map,
287 const char* prefix,
288 ArtMethod* current_method,
289 void* ucontext_ptr) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700290 // b/18119146
291 if (RUNNING_ON_MEMORY_TOOL != 0) {
292 return;
293 }
294
295 BacktraceMap* map = existing_map;
296 std::unique_ptr<BacktraceMap> tmp_map;
297 if (map == nullptr) {
298 tmp_map.reset(BacktraceMap::Create(getpid()));
299 map = tmp_map.get();
300 }
301 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid, map));
302 if (!backtrace->Unwind(0, reinterpret_cast<ucontext*>(ucontext_ptr))) {
303 os << prefix << "(backtrace::Unwind failed for thread " << tid
Andreas Gampeef295362016-10-11 20:04:11 -0700304 << ": " << backtrace->GetErrorString(backtrace->GetError()) << ")" << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700305 return;
306 } else if (backtrace->NumFrames() == 0) {
Andreas Gampeef295362016-10-11 20:04:11 -0700307 os << prefix << "(no native stack frames for thread " << tid << ")" << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700308 return;
309 }
310
311 // Check whether we have and should use addr2line.
312 bool use_addr2line;
313 if (kUseAddr2line) {
314 // Try to run it to see whether we have it. Push an argument so that it doesn't assume a.out
315 // and print to stderr.
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700316 use_addr2line = (gAborting > 0) && RunCommand("addr2line -h");
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700317 } else {
318 use_addr2line = false;
319 }
320
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700321 std::unique_ptr<Addr2linePipe> addr2line_state;
322
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700323 for (Backtrace::const_iterator it = backtrace->begin();
324 it != backtrace->end(); ++it) {
325 // We produce output like this:
326 // ] #00 pc 000075bb8 /system/lib/libc.so (unwind_backtrace_thread+536)
327 // In order for parsing tools to continue to function, the stack dump
328 // format must at least adhere to this format:
329 // #XX pc <RELATIVE_ADDR> <FULL_PATH_TO_SHARED_LIBRARY> ...
330 // The parsers require a single space before and after pc, and two spaces
331 // after the <RELATIVE_ADDR>. There can be any prefix data before the
332 // #XX. <RELATIVE_ADDR> has to be a hex number but with no 0x prefix.
333 os << prefix << StringPrintf("#%02zu pc ", it->num);
334 bool try_addr2line = false;
335 if (!BacktraceMap::IsValid(it->map)) {
Christopher Ferris77b38df2018-01-18 16:16:49 -0800336 os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 " ???"
337 : "%08" PRIx64 " ???",
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700338 it->pc);
339 } else {
Christopher Ferris77b38df2018-01-18 16:16:49 -0800340 os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 " "
341 : "%08" PRIx64 " ",
Christopher Ferrisf88b5c02017-07-19 14:18:33 -0700342 it->rel_pc);
Christopher Ferris8bd7d1b2018-01-08 11:12:40 -0800343 if (it->map.name.empty()) {
Christopher Ferris77b38df2018-01-18 16:16:49 -0800344 os << StringPrintf("<anonymous:%" PRIx64 ">", it->map.start);
Christopher Ferris8bd7d1b2018-01-08 11:12:40 -0800345 } else {
346 os << it->map.name;
347 }
Christopher Ferris53ef6a62018-02-09 23:13:27 -0800348 if (it->map.offset != 0) {
349 os << StringPrintf(" (offset %" PRIx64 ")", it->map.offset);
350 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700351 os << " (";
352 if (!it->func_name.empty()) {
353 os << it->func_name;
354 if (it->func_offset != 0) {
355 os << "+" << it->func_offset;
356 }
Christopher Ferris8bd7d1b2018-01-08 11:12:40 -0800357 // Functions found using the gdb jit interface will be in an empty
358 // map that cannot be found using addr2line.
359 if (!it->map.name.empty()) {
360 try_addr2line = true;
361 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700362 } else if (current_method != nullptr &&
363 Locks::mutator_lock_->IsSharedHeld(Thread::Current()) &&
364 PcIsWithinQuickCode(current_method, it->pc)) {
365 const void* start_of_code = current_method->GetEntryPointFromQuickCompiledCode();
David Sehr709b0702016-10-13 09:12:37 -0700366 os << current_method->JniLongName() << "+"
Christopher Ferris77b38df2018-01-18 16:16:49 -0800367 << (it->pc - reinterpret_cast<uint64_t>(start_of_code));
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700368 } else {
369 os << "???";
370 }
371 os << ")";
372 }
Andreas Gampeef295362016-10-11 20:04:11 -0700373 os << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700374 if (try_addr2line && use_addr2line) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700375 Addr2line(it->map.name, it->pc - it->map.start, os, prefix, &addr2line_state);
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700376 }
377 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700378
379 if (addr2line_state != nullptr) {
380 Drain(0, prefix, &addr2line_state, os);
381 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700382}
383
384void DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) {
385 if (tid == GetTid()) {
386 // There's no point showing that we're reading our stack out of /proc!
387 return;
388 }
389
390 std::string kernel_stack_filename(StringPrintf("/proc/self/task/%d/stack", tid));
391 std::string kernel_stack;
392 if (!ReadFileToString(kernel_stack_filename, &kernel_stack)) {
393 os << prefix << "(couldn't read " << kernel_stack_filename << ")\n";
394 return;
395 }
396
397 std::vector<std::string> kernel_stack_frames;
398 Split(kernel_stack, '\n', &kernel_stack_frames);
yuanhao7a8b3f22018-02-01 15:58:53 +0800399 if (kernel_stack_frames.empty()) {
400 os << prefix << "(" << kernel_stack_filename << " is empty)\n";
401 return;
402 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700403 // We skip the last stack frame because it's always equivalent to "[<ffffffff>] 0xffffffff",
404 // which looking at the source appears to be the kernel's way of saying "that's all, folks!".
405 kernel_stack_frames.pop_back();
406 for (size_t i = 0; i < kernel_stack_frames.size(); ++i) {
407 // Turn "[<ffffffff8109156d>] futex_wait_queue_me+0xcd/0x110"
408 // into "futex_wait_queue_me+0xcd/0x110".
409 const char* text = kernel_stack_frames[i].c_str();
410 const char* close_bracket = strchr(text, ']');
411 if (close_bracket != nullptr) {
412 text = close_bracket + 2;
413 }
414 os << prefix;
415 if (include_count) {
416 os << StringPrintf("#%02zd ", i);
417 }
Andreas Gampeef295362016-10-11 20:04:11 -0700418 os << text << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700419 }
420}
421
422#elif defined(__APPLE__)
423
424void DumpNativeStack(std::ostream& os ATTRIBUTE_UNUSED,
425 pid_t tid ATTRIBUTE_UNUSED,
426 BacktraceMap* existing_map ATTRIBUTE_UNUSED,
427 const char* prefix ATTRIBUTE_UNUSED,
428 ArtMethod* current_method ATTRIBUTE_UNUSED,
429 void* ucontext_ptr ATTRIBUTE_UNUSED) {
430}
431
432void DumpKernelStack(std::ostream& os ATTRIBUTE_UNUSED,
433 pid_t tid ATTRIBUTE_UNUSED,
434 const char* prefix ATTRIBUTE_UNUSED,
435 bool include_count ATTRIBUTE_UNUSED) {
436}
437
438#else
439#error "Unsupported architecture for native stack dumps."
440#endif
441
442} // namespace art