1 /*
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 <memory>
20 #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>
36 #include <poll.h>
37 #include <signal.h>
38 #include <stdlib.h>
39 #include <sys/time.h>
40 #include <sys/types.h>
41 
42 #include "android-base/stringprintf.h"
43 #include "android-base/strings.h"
44 
45 #include "arch/instruction_set.h"
46 #include "base/aborting.h"
47 #include "base/bit_utils.h"
48 #include "base/file_utils.h"
49 #include "base/memory_tool.h"
50 #include "base/mutex.h"
51 #include "base/os.h"
52 #include "base/unix_file/fd_file.h"
53 #include "base/utils.h"
54 #include "class_linker.h"
55 #include "entrypoints/runtime_asm_entrypoints.h"
56 #include "oat_quick_method_header.h"
57 #include "runtime.h"
58 #include "thread-current-inl.h"
59 
60 #endif
61 
62 namespace art {
63 
64 #if defined(__linux__)
65 
66 using android::base::StringPrintf;
67 
68 static constexpr bool kUseAddr2line = !kIsTargetBuild;
69 
FindAddr2line()70 std::string FindAddr2line() {
71   if (!kIsTargetBuild) {
72     constexpr const char* kAddr2linePrebuiltPath =
73       "/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/bin/x86_64-linux-addr2line";
74     const char* env_value = getenv("ANDROID_BUILD_TOP");
75     if (env_value != nullptr) {
76       return std::string(env_value) + kAddr2linePrebuiltPath;
77     }
78   }
79   return std::string("/usr/bin/addr2line");
80 }
81 
82 ALWAYS_INLINE
WritePrefix(std::ostream & os,const char * prefix,bool odd)83 static inline void WritePrefix(std::ostream& os, const char* prefix, bool odd) {
84   if (prefix != nullptr) {
85     os << prefix;
86   }
87   os << "  ";
88   if (!odd) {
89     os << " ";
90   }
91 }
92 
93 // The state of an open pipe to addr2line. In "server" mode, addr2line takes input on stdin
94 // and prints the result to stdout. This struct keeps the state of the open connection.
95 struct Addr2linePipe {
Addr2linePipeart::Addr2linePipe96   Addr2linePipe(int in_fd, int out_fd, const std::string& file_name, pid_t pid)
97       : in(in_fd, false), out(out_fd, false), file(file_name), child_pid(pid), odd(true) {}
98 
~Addr2linePipeart::Addr2linePipe99   ~Addr2linePipe() {
100     kill(child_pid, SIGKILL);
101   }
102 
103   File in;      // The file descriptor that is connected to the output of addr2line.
104   File out;     // The file descriptor that is connected to the input of addr2line.
105 
106   const std::string file;     // The file addr2line is working on, so that we know when to close
107                               // and restart.
108   const pid_t child_pid;      // The pid of the child, which we should kill when we're done.
109   bool odd;                   // Print state for indentation of lines.
110 };
111 
Connect(const std::string & name,const char * args[])112 static std::unique_ptr<Addr2linePipe> Connect(const std::string& name, const char* args[]) {
113   int caller_to_addr2line[2];
114   int addr2line_to_caller[2];
115 
116   if (pipe(caller_to_addr2line) == -1) {
117     return nullptr;
118   }
119   if (pipe(addr2line_to_caller) == -1) {
120     close(caller_to_addr2line[0]);
121     close(caller_to_addr2line[1]);
122     return nullptr;
123   }
124 
125   pid_t pid = fork();
126   if (pid == -1) {
127     close(caller_to_addr2line[0]);
128     close(caller_to_addr2line[1]);
129     close(addr2line_to_caller[0]);
130     close(addr2line_to_caller[1]);
131     return nullptr;
132   }
133 
134   if (pid == 0) {
135     dup2(caller_to_addr2line[0], STDIN_FILENO);
136     dup2(addr2line_to_caller[1], STDOUT_FILENO);
137 
138     close(caller_to_addr2line[0]);
139     close(caller_to_addr2line[1]);
140     close(addr2line_to_caller[0]);
141     close(addr2line_to_caller[1]);
142 
143     execv(args[0], const_cast<char* const*>(args));
144     exit(1);
145   } else {
146     close(caller_to_addr2line[0]);
147     close(addr2line_to_caller[1]);
148     return std::make_unique<Addr2linePipe>(addr2line_to_caller[0],
149                                            caller_to_addr2line[1],
150                                            name,
151                                            pid);
152   }
153 }
154 
Drain(size_t expected,const char * prefix,std::unique_ptr<Addr2linePipe> * pipe,std::ostream & os)155 static void Drain(size_t expected,
156                   const char* prefix,
157                   std::unique_ptr<Addr2linePipe>* pipe /* inout */,
158                   std::ostream& os) {
159   DCHECK(pipe != nullptr);
160   DCHECK(pipe->get() != nullptr);
161   int in = pipe->get()->in.Fd();
162   DCHECK_GE(in, 0);
163 
164   bool prefix_written = false;
165 
166   for (;;) {
167     constexpr uint32_t kWaitTimeExpectedMilli = 500;
168     constexpr uint32_t kWaitTimeUnexpectedMilli = 50;
169 
170     int timeout = expected > 0 ? kWaitTimeExpectedMilli : kWaitTimeUnexpectedMilli;
171     struct pollfd read_fd{in, POLLIN, 0};
172     int retval = TEMP_FAILURE_RETRY(poll(&read_fd, 1, timeout));
173     if (retval == -1) {
174       // An error occurred.
175       pipe->reset();
176       return;
177     }
178 
179     if (retval == 0) {
180       // Timeout.
181       return;
182     }
183 
184     if (!(read_fd.revents & POLLIN)) {
185       // addr2line call exited.
186       pipe->reset();
187       return;
188     }
189 
190     constexpr size_t kMaxBuffer = 128;  // Relatively small buffer. Should be OK as we're on an
191     // alt stack, but just to be sure...
192     char buffer[kMaxBuffer];
193     memset(buffer, 0, kMaxBuffer);
194     int bytes_read = TEMP_FAILURE_RETRY(read(in, buffer, kMaxBuffer - 1));
195     if (bytes_read <= 0) {
196       // This should not really happen...
197       pipe->reset();
198       return;
199     }
200     buffer[bytes_read] = '\0';
201 
202     char* tmp = buffer;
203     while (*tmp != 0) {
204       if (!prefix_written) {
205         WritePrefix(os, prefix, (*pipe)->odd);
206         prefix_written = true;
207       }
208       char* new_line = strchr(tmp, '\n');
209       if (new_line == nullptr) {
210         os << tmp;
211 
212         break;
213       } else {
214         char saved = *(new_line + 1);
215         *(new_line + 1) = 0;
216         os << tmp;
217         *(new_line + 1) = saved;
218 
219         tmp = new_line + 1;
220         prefix_written = false;
221         (*pipe)->odd = !(*pipe)->odd;
222 
223         if (expected > 0) {
224           expected--;
225         }
226       }
227     }
228   }
229 }
230 
Addr2line(const std::string & map_src,uintptr_t offset,std::ostream & os,const char * prefix,std::unique_ptr<Addr2linePipe> * pipe)231 static void Addr2line(const std::string& map_src,
232                       uintptr_t offset,
233                       std::ostream& os,
234                       const char* prefix,
235                       std::unique_ptr<Addr2linePipe>* pipe /* inout */) {
236   DCHECK(pipe != nullptr);
237 
238   if (map_src == "[vdso]" || android::base::EndsWith(map_src, ".vdex")) {
239     // addr2line will not work on the vdso.
240     // vdex files are special frames injected for the interpreter
241     // so they don't have any line number information available.
242     return;
243   }
244 
245   if (*pipe == nullptr || (*pipe)->file != map_src) {
246     if (*pipe != nullptr) {
247       Drain(0, prefix, pipe, os);
248     }
249     pipe->reset();  // Close early.
250 
251     std::string addr2linePath = FindAddr2line();
252     const char* args[7] = {
253         addr2linePath.c_str(),
254         "--functions",
255         "--inlines",
256         "--demangle",
257         "-e",
258         map_src.c_str(),
259         nullptr
260     };
261     *pipe = Connect(map_src, args);
262   }
263 
264   Addr2linePipe* pipe_ptr = pipe->get();
265   if (pipe_ptr == nullptr) {
266     // Failed...
267     return;
268   }
269 
270   // Send the offset.
271   const std::string hex_offset = StringPrintf("%zx\n", offset);
272 
273   if (!pipe_ptr->out.WriteFully(hex_offset.data(), hex_offset.length())) {
274     // Error. :-(
275     pipe->reset();
276     return;
277   }
278 
279   // Now drain (expecting two lines).
280   Drain(2U, prefix, pipe, os);
281 }
282 
RunCommand(const std::string & cmd)283 static bool RunCommand(const std::string& cmd) {
284   FILE* stream = popen(cmd.c_str(), "r");
285   if (stream) {
286     pclose(stream);
287     return true;
288   } else {
289     return false;
290   }
291 }
292 
PcIsWithinQuickCode(ArtMethod * method,uintptr_t pc)293 static bool PcIsWithinQuickCode(ArtMethod* method, uintptr_t pc) NO_THREAD_SAFETY_ANALYSIS {
294   const void* entry_point = method->GetEntryPointFromQuickCompiledCode();
295   if (entry_point == nullptr) {
296     return pc == 0;
297   }
298   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
299   if (class_linker->IsQuickGenericJniStub(entry_point) ||
300       class_linker->IsQuickResolutionStub(entry_point) ||
301       class_linker->IsQuickToInterpreterBridge(entry_point)) {
302     return false;
303   }
304   // The backtrace library might have heuristically subracted instruction
305   // size from the pc, to pretend the pc is at the calling instruction.
306   if (reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) - pc <= 4) {
307     return false;
308   }
309   uintptr_t code = reinterpret_cast<uintptr_t>(EntryPointToCodePointer(entry_point));
310   uintptr_t code_size = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].GetCodeSize();
311   return code <= pc && pc <= (code + code_size);
312 }
313 
DumpNativeStack(std::ostream & os,pid_t tid,BacktraceMap * existing_map,const char * prefix,ArtMethod * current_method,void * ucontext_ptr,bool skip_frames)314 void DumpNativeStack(std::ostream& os,
315                      pid_t tid,
316                      BacktraceMap* existing_map,
317                      const char* prefix,
318                      ArtMethod* current_method,
319                      void* ucontext_ptr,
320                      bool skip_frames) {
321   // Historical note: This was disabled when running under Valgrind (b/18119146).
322 
323   BacktraceMap* map = existing_map;
324   std::unique_ptr<BacktraceMap> tmp_map;
325   if (map == nullptr) {
326     tmp_map.reset(BacktraceMap::Create(getpid()));
327     map = tmp_map.get();
328   }
329   std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid, map));
330   backtrace->SetSkipFrames(skip_frames);
331   if (!backtrace->Unwind(0, reinterpret_cast<ucontext*>(ucontext_ptr))) {
332     os << prefix << "(backtrace::Unwind failed for thread " << tid
333        << ": " <<  backtrace->GetErrorString(backtrace->GetError()) << ")" << std::endl;
334     return;
335   } else if (backtrace->NumFrames() == 0) {
336     os << prefix << "(no native stack frames for thread " << tid << ")" << std::endl;
337     return;
338   }
339 
340   // Check whether we have and should use addr2line.
341   bool use_addr2line;
342   if (kUseAddr2line) {
343     // Try to run it to see whether we have it. Push an argument so that it doesn't assume a.out
344     // and print to stderr.
345     use_addr2line = (gAborting > 0) && RunCommand(FindAddr2line() + " -h");
346   } else {
347     use_addr2line = false;
348   }
349 
350   std::unique_ptr<Addr2linePipe> addr2line_state;
351 
352   for (Backtrace::const_iterator it = backtrace->begin();
353        it != backtrace->end(); ++it) {
354     // We produce output like this:
355     // ]    #00 pc 000075bb8  /system/lib/libc.so (unwind_backtrace_thread+536)
356     // In order for parsing tools to continue to function, the stack dump
357     // format must at least adhere to this format:
358     //  #XX pc <RELATIVE_ADDR>  <FULL_PATH_TO_SHARED_LIBRARY> ...
359     // The parsers require a single space before and after pc, and two spaces
360     // after the <RELATIVE_ADDR>. There can be any prefix data before the
361     // #XX. <RELATIVE_ADDR> has to be a hex number but with no 0x prefix.
362     os << prefix << StringPrintf("#%02zu pc ", it->num);
363     bool try_addr2line = false;
364     if (!BacktraceMap::IsValid(it->map)) {
365       os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 "  ???"
366                                                             : "%08" PRIx64 "  ???",
367                          it->pc);
368     } else {
369       os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 "  "
370                                                             : "%08" PRIx64 "  ",
371                          it->rel_pc);
372       if (it->map.name.empty()) {
373         os << StringPrintf("<anonymous:%" PRIx64 ">", it->map.start);
374       } else {
375         os << it->map.name;
376       }
377       if (it->map.offset != 0) {
378         os << StringPrintf(" (offset %" PRIx64 ")", it->map.offset);
379       }
380       os << " (";
381       if (!it->func_name.empty()) {
382         os << it->func_name;
383         if (it->func_offset != 0) {
384           os << "+" << it->func_offset;
385         }
386         // Functions found using the gdb jit interface will be in an empty
387         // map that cannot be found using addr2line.
388         if (!it->map.name.empty()) {
389           try_addr2line = true;
390         }
391       } else if (current_method != nullptr &&
392           Locks::mutator_lock_->IsSharedHeld(Thread::Current()) &&
393           PcIsWithinQuickCode(current_method, it->pc)) {
394         const void* start_of_code = current_method->GetEntryPointFromQuickCompiledCode();
395         os << current_method->JniLongName() << "+"
396            << (it->pc - reinterpret_cast<uint64_t>(start_of_code));
397       } else {
398         os << "???";
399       }
400       os << ")";
401     }
402     os << std::endl;
403     if (try_addr2line && use_addr2line) {
404       Addr2line(it->map.name, it->rel_pc, os, prefix, &addr2line_state);
405     }
406   }
407 
408   if (addr2line_state != nullptr) {
409     Drain(0, prefix, &addr2line_state, os);
410   }
411 }
412 
DumpKernelStack(std::ostream & os,pid_t tid,const char * prefix,bool include_count)413 void DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) {
414   if (tid == GetTid()) {
415     // There's no point showing that we're reading our stack out of /proc!
416     return;
417   }
418 
419   std::string kernel_stack_filename(StringPrintf("/proc/self/task/%d/stack", tid));
420   std::string kernel_stack;
421   if (!ReadFileToString(kernel_stack_filename, &kernel_stack)) {
422     os << prefix << "(couldn't read " << kernel_stack_filename << ")\n";
423     return;
424   }
425 
426   std::vector<std::string> kernel_stack_frames;
427   Split(kernel_stack, '\n', &kernel_stack_frames);
428   if (kernel_stack_frames.empty()) {
429     os << prefix << "(" << kernel_stack_filename << " is empty)\n";
430     return;
431   }
432   // We skip the last stack frame because it's always equivalent to "[<ffffffff>] 0xffffffff",
433   // which looking at the source appears to be the kernel's way of saying "that's all, folks!".
434   kernel_stack_frames.pop_back();
435   for (size_t i = 0; i < kernel_stack_frames.size(); ++i) {
436     // Turn "[<ffffffff8109156d>] futex_wait_queue_me+0xcd/0x110"
437     // into "futex_wait_queue_me+0xcd/0x110".
438     const char* text = kernel_stack_frames[i].c_str();
439     const char* close_bracket = strchr(text, ']');
440     if (close_bracket != nullptr) {
441       text = close_bracket + 2;
442     }
443     os << prefix;
444     if (include_count) {
445       os << StringPrintf("#%02zd ", i);
446     }
447     os << text << std::endl;
448   }
449 }
450 
451 #elif defined(__APPLE__)
452 
453 void DumpNativeStack(std::ostream& os ATTRIBUTE_UNUSED,
454                      pid_t tid ATTRIBUTE_UNUSED,
455                      BacktraceMap* existing_map ATTRIBUTE_UNUSED,
456                      const char* prefix ATTRIBUTE_UNUSED,
457                      ArtMethod* current_method ATTRIBUTE_UNUSED,
458                      void* ucontext_ptr ATTRIBUTE_UNUSED,
459                      bool skip_frames ATTRIBUTE_UNUSED) {
460 }
461 
462 void DumpKernelStack(std::ostream& os ATTRIBUTE_UNUSED,
463                      pid_t tid ATTRIBUTE_UNUSED,
464                      const char* prefix ATTRIBUTE_UNUSED,
465                      bool include_count ATTRIBUTE_UNUSED) {
466 }
467 
468 #else
469 #error "Unsupported architecture for native stack dumps."
470 #endif
471 
472 }  // namespace art
473