blob: 5565565c7b32de57262cdff5942317b0587da2fb [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
40#include "arch/instruction_set.h"
41#include "base/memory_tool.h"
42#include "base/mutex.h"
43#include "base/stringprintf.h"
Andreas Gampefcccbaf2016-08-02 17:20:03 -070044#include "base/unix_file/fd_file.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070045#include "oat_quick_method_header.h"
Andreas Gampefcccbaf2016-08-02 17:20:03 -070046#include "os.h"
Andreas Gampe5dd44d02016-08-02 17:20:03 -070047#include "thread-inl.h"
48#include "utils.h"
49
50#endif
51
52namespace art {
53
54#if defined(__linux__)
55
56static constexpr bool kUseAddr2line = !kIsTargetBuild;
57
58ALWAYS_INLINE
Andreas Gampefcccbaf2016-08-02 17:20:03 -070059static inline void WritePrefix(std::ostream& os, const char* prefix, bool odd) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -070060 if (prefix != nullptr) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -070061 os << prefix;
Andreas Gampe5dd44d02016-08-02 17:20:03 -070062 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -070063 os << " ";
Andreas Gampe5dd44d02016-08-02 17:20:03 -070064 if (!odd) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -070065 os << " ";
Andreas Gampe5dd44d02016-08-02 17:20:03 -070066 }
67}
68
Andreas Gampefcccbaf2016-08-02 17:20:03 -070069// The state of an open pipe to addr2line. In "server" mode, addr2line takes input on stdin
70// and prints the result to stdout. This struct keeps the state of the open connection.
71struct Addr2linePipe {
72 Addr2linePipe(int in_fd, int out_fd, const std::string& file_name, pid_t pid)
73 : in(in_fd, false), out(out_fd, false), file(file_name), child_pid(pid), odd(true) {}
74
75 ~Addr2linePipe() {
76 kill(child_pid, SIGKILL);
77 }
78
79 File in; // The file descriptor that is connected to the output of addr2line.
80 File out; // The file descriptor that is connected to the input of addr2line.
81
82 const std::string file; // The file addr2line is working on, so that we know when to close
83 // and restart.
84 const pid_t child_pid; // The pid of the child, which we should kill when we're done.
85 bool odd; // Print state for indentation of lines.
86};
87
88static std::unique_ptr<Addr2linePipe> Connect(const std::string& name, const char* args[]) {
89 int caller_to_addr2line[2];
90 int addr2line_to_caller[2];
91
92 if (pipe(caller_to_addr2line) == -1) {
93 return nullptr;
94 }
95 if (pipe(addr2line_to_caller) == -1) {
96 close(caller_to_addr2line[0]);
97 close(caller_to_addr2line[1]);
98 return nullptr;
99 }
100
101 pid_t pid = fork();
102 if (pid == -1) {
103 close(caller_to_addr2line[0]);
104 close(caller_to_addr2line[1]);
105 close(addr2line_to_caller[1]);
106 close(addr2line_to_caller[1]);
107 return nullptr;
108 }
109
110 if (pid == 0) {
111 dup2(caller_to_addr2line[0], STDIN_FILENO);
112 dup2(addr2line_to_caller[1], STDOUT_FILENO);
113
114 close(caller_to_addr2line[0]);
115 close(caller_to_addr2line[1]);
116 close(addr2line_to_caller[0]);
117 close(addr2line_to_caller[1]);
118
119 execv(args[0], const_cast<char* const*>(args));
120 exit(1);
121 } else {
122 close(caller_to_addr2line[0]);
123 close(addr2line_to_caller[1]);
124 return std::unique_ptr<Addr2linePipe>(new Addr2linePipe(addr2line_to_caller[0],
125 caller_to_addr2line[1],
126 name,
127 pid));
128 }
129}
130
131static void Drain(size_t expected,
132 const char* prefix,
133 std::unique_ptr<Addr2linePipe>* pipe /* inout */,
134 std::ostream& os) {
135 DCHECK(pipe != nullptr);
136 DCHECK(pipe->get() != nullptr);
137 int in = pipe->get()->in.Fd();
138 DCHECK_GE(in, 0);
139
140 bool prefix_written = false;
141
142 for (;;) {
143 constexpr uint32_t kWaitTimeExpectedMicros = 500 * 1000;
144 constexpr uint32_t kWaitTimeUnexpectedMicros = 50 * 1000;
145
146 struct timeval tv;
147 tv.tv_sec = 0;
148 tv.tv_usec = expected > 0 ? kWaitTimeExpectedMicros : kWaitTimeUnexpectedMicros;
149
150 fd_set rfds;
151 FD_ZERO(&rfds);
152 FD_SET(in, &rfds);
153
154 int retval = TEMP_FAILURE_RETRY(select(in + 1, &rfds, nullptr, nullptr, &tv));
155
156 if (retval < 0) {
157 // Other side may have crashed or other errors.
158 pipe->reset();
159 return;
160 }
161
162 if (retval == 0) {
163 // Timeout.
164 return;
165 }
166
167 DCHECK_EQ(retval, 1);
168
169 constexpr size_t kMaxBuffer = 128; // Relatively small buffer. Should be OK as we're on an
170 // alt stack, but just to be sure...
171 char buffer[kMaxBuffer];
172 memset(buffer, 0, kMaxBuffer);
173 int bytes_read = TEMP_FAILURE_RETRY(read(in, buffer, kMaxBuffer - 1));
174
175 if (bytes_read < 0) {
176 // This should not really happen...
177 pipe->reset();
178 return;
179 }
180
181 char* tmp = buffer;
182 while (*tmp != 0) {
183 if (!prefix_written) {
184 WritePrefix(os, prefix, (*pipe)->odd);
185 prefix_written = true;
186 }
187 char* new_line = strchr(tmp, '\n');
188 if (new_line == nullptr) {
189 os << tmp;
190
191 break;
192 } else {
193 char saved = *(new_line + 1);
194 *(new_line + 1) = 0;
195 os << tmp;
196 *(new_line + 1) = saved;
197
198 tmp = new_line + 1;
199 prefix_written = false;
200 (*pipe)->odd = !(*pipe)->odd;
201
202 if (expected > 0) {
203 expected--;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700204 }
205 }
206 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700207 }
208}
209
210static void Addr2line(const std::string& map_src,
211 uintptr_t offset,
212 std::ostream& os,
213 const char* prefix,
214 std::unique_ptr<Addr2linePipe>* pipe /* inout */) {
215 DCHECK(pipe != nullptr);
216
217 if (map_src == "[vdso]") {
218 // Special-case this, our setup has problems with this.
219 return;
220 }
221
222 if (*pipe == nullptr || (*pipe)->file != map_src) {
223 if (*pipe != nullptr) {
224 Drain(0, prefix, pipe, os);
225 }
226 pipe->reset(); // Close early.
227
228 const char* args[7] = {
229 "/usr/bin/addr2line",
230 "--functions",
231 "--inlines",
232 "--demangle",
233 "-e",
234 map_src.c_str(),
235 nullptr
236 };
237 *pipe = Connect(map_src, args);
238 }
239
240 Addr2linePipe* pipe_ptr = pipe->get();
241 if (pipe_ptr == nullptr) {
242 // Failed...
243 return;
244 }
245
246 // Send the offset.
247 const std::string hex_offset = StringPrintf("%zx\n", offset);
248
249 if (!pipe_ptr->out.WriteFully(hex_offset.data(), hex_offset.length())) {
250 // Error. :-(
251 pipe->reset();
252 return;
253 }
254
255 // Now drain (expecting two lines).
256 Drain(2U, prefix, pipe, os);
257}
258
Andreas Gampeca620d72016-11-08 08:09:33 -0800259static bool RunCommand(const std::string& cmd) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700260 FILE* stream = popen(cmd.c_str(), "r");
261 if (stream) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700262 pclose(stream);
263 return true;
264 } else {
265 return false;
266 }
267}
268
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700269static bool PcIsWithinQuickCode(ArtMethod* method, uintptr_t pc) NO_THREAD_SAFETY_ANALYSIS {
270 uintptr_t code = reinterpret_cast<uintptr_t>(EntryPointToCodePointer(
271 method->GetEntryPointFromQuickCompiledCode()));
272 if (code == 0) {
273 return pc == 0;
274 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700275 uintptr_t code_size = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].GetCodeSize();
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700276 return code <= pc && pc <= (code + code_size);
277}
278
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700279void DumpNativeStack(std::ostream& os,
280 pid_t tid,
281 BacktraceMap* existing_map,
282 const char* prefix,
283 ArtMethod* current_method,
284 void* ucontext_ptr) {
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700285 // b/18119146
286 if (RUNNING_ON_MEMORY_TOOL != 0) {
287 return;
288 }
289
290 BacktraceMap* map = existing_map;
291 std::unique_ptr<BacktraceMap> tmp_map;
292 if (map == nullptr) {
293 tmp_map.reset(BacktraceMap::Create(getpid()));
294 map = tmp_map.get();
295 }
296 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid, map));
297 if (!backtrace->Unwind(0, reinterpret_cast<ucontext*>(ucontext_ptr))) {
298 os << prefix << "(backtrace::Unwind failed for thread " << tid
Andreas Gampeef295362016-10-11 20:04:11 -0700299 << ": " << backtrace->GetErrorString(backtrace->GetError()) << ")" << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700300 return;
301 } else if (backtrace->NumFrames() == 0) {
Andreas Gampeef295362016-10-11 20:04:11 -0700302 os << prefix << "(no native stack frames for thread " << tid << ")" << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700303 return;
304 }
305
306 // Check whether we have and should use addr2line.
307 bool use_addr2line;
308 if (kUseAddr2line) {
309 // Try to run it to see whether we have it. Push an argument so that it doesn't assume a.out
310 // and print to stderr.
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700311 use_addr2line = (gAborting > 0) && RunCommand("addr2line -h");
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700312 } else {
313 use_addr2line = false;
314 }
315
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700316 std::unique_ptr<Addr2linePipe> addr2line_state;
317
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700318 for (Backtrace::const_iterator it = backtrace->begin();
319 it != backtrace->end(); ++it) {
320 // We produce output like this:
321 // ] #00 pc 000075bb8 /system/lib/libc.so (unwind_backtrace_thread+536)
322 // In order for parsing tools to continue to function, the stack dump
323 // format must at least adhere to this format:
324 // #XX pc <RELATIVE_ADDR> <FULL_PATH_TO_SHARED_LIBRARY> ...
325 // The parsers require a single space before and after pc, and two spaces
326 // after the <RELATIVE_ADDR>. There can be any prefix data before the
327 // #XX. <RELATIVE_ADDR> has to be a hex number but with no 0x prefix.
328 os << prefix << StringPrintf("#%02zu pc ", it->num);
329 bool try_addr2line = false;
330 if (!BacktraceMap::IsValid(it->map)) {
331 os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIxPTR " ???"
332 : "%08" PRIxPTR " ???",
333 it->pc);
334 } else {
335 os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIxPTR " "
336 : "%08" PRIxPTR " ",
337 BacktraceMap::GetRelativePc(it->map, it->pc));
338 os << it->map.name;
339 os << " (";
340 if (!it->func_name.empty()) {
341 os << it->func_name;
342 if (it->func_offset != 0) {
343 os << "+" << it->func_offset;
344 }
345 try_addr2line = true;
346 } else if (current_method != nullptr &&
347 Locks::mutator_lock_->IsSharedHeld(Thread::Current()) &&
348 PcIsWithinQuickCode(current_method, it->pc)) {
349 const void* start_of_code = current_method->GetEntryPointFromQuickCompiledCode();
David Sehr709b0702016-10-13 09:12:37 -0700350 os << current_method->JniLongName() << "+"
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700351 << (it->pc - reinterpret_cast<uintptr_t>(start_of_code));
352 } else {
353 os << "???";
354 }
355 os << ")";
356 }
Andreas Gampeef295362016-10-11 20:04:11 -0700357 os << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700358 if (try_addr2line && use_addr2line) {
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700359 Addr2line(it->map.name, it->pc - it->map.start, os, prefix, &addr2line_state);
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700360 }
361 }
Andreas Gampefcccbaf2016-08-02 17:20:03 -0700362
363 if (addr2line_state != nullptr) {
364 Drain(0, prefix, &addr2line_state, os);
365 }
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700366}
367
368void DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) {
369 if (tid == GetTid()) {
370 // There's no point showing that we're reading our stack out of /proc!
371 return;
372 }
373
374 std::string kernel_stack_filename(StringPrintf("/proc/self/task/%d/stack", tid));
375 std::string kernel_stack;
376 if (!ReadFileToString(kernel_stack_filename, &kernel_stack)) {
377 os << prefix << "(couldn't read " << kernel_stack_filename << ")\n";
378 return;
379 }
380
381 std::vector<std::string> kernel_stack_frames;
382 Split(kernel_stack, '\n', &kernel_stack_frames);
383 // We skip the last stack frame because it's always equivalent to "[<ffffffff>] 0xffffffff",
384 // which looking at the source appears to be the kernel's way of saying "that's all, folks!".
385 kernel_stack_frames.pop_back();
386 for (size_t i = 0; i < kernel_stack_frames.size(); ++i) {
387 // Turn "[<ffffffff8109156d>] futex_wait_queue_me+0xcd/0x110"
388 // into "futex_wait_queue_me+0xcd/0x110".
389 const char* text = kernel_stack_frames[i].c_str();
390 const char* close_bracket = strchr(text, ']');
391 if (close_bracket != nullptr) {
392 text = close_bracket + 2;
393 }
394 os << prefix;
395 if (include_count) {
396 os << StringPrintf("#%02zd ", i);
397 }
Andreas Gampeef295362016-10-11 20:04:11 -0700398 os << text << std::endl;
Andreas Gampe5dd44d02016-08-02 17:20:03 -0700399 }
400}
401
402#elif defined(__APPLE__)
403
404void DumpNativeStack(std::ostream& os ATTRIBUTE_UNUSED,
405 pid_t tid ATTRIBUTE_UNUSED,
406 BacktraceMap* existing_map ATTRIBUTE_UNUSED,
407 const char* prefix ATTRIBUTE_UNUSED,
408 ArtMethod* current_method ATTRIBUTE_UNUSED,
409 void* ucontext_ptr ATTRIBUTE_UNUSED) {
410}
411
412void DumpKernelStack(std::ostream& os ATTRIBUTE_UNUSED,
413 pid_t tid ATTRIBUTE_UNUSED,
414 const char* prefix ATTRIBUTE_UNUSED,
415 bool include_count ATTRIBUTE_UNUSED) {
416}
417
418#else
419#error "Unsupported architecture for native stack dumps."
420#endif
421
422} // namespace art