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 <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 <poll.h>
36 #include <signal.h>
37 #include <stdlib.h>
38 #include <sys/time.h>
39 #include <sys/types.h>
40 
41 #include "android-base/stringprintf.h"
42 #include "android-base/strings.h"
43 
44 #include "arch/instruction_set.h"
45 #include "base/aborting.h"
46 #include "base/file_utils.h"
47 #include "base/memory_tool.h"
48 #include "base/mutex.h"
49 #include "base/os.h"
50 #include "base/unix_file/fd_file.h"
51 #include "base/utils.h"
52 #include "oat_quick_method_header.h"
53 #include "thread-current-inl.h"
54 
55 #endif
56 
57 namespace art {
58 
59 #if defined(__linux__)
60 
61 using android::base::StringPrintf;
62 
63 static constexpr bool kUseAddr2line = !kIsTargetBuild;
64 
65 ALWAYS_INLINE
WritePrefix(std::ostream & os,const char * prefix,bool odd)66 static inline void WritePrefix(std::ostream& os, const char* prefix, bool odd) {
67   if (prefix != nullptr) {
68     os << prefix;
69   }
70   os << "  ";
71   if (!odd) {
72     os << " ";
73   }
74 }
75 
76 // 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.
78 struct Addr2linePipe {
Addr2linePipeart::Addr2linePipe79   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 
~Addr2linePipeart::Addr2linePipe82   ~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 
Connect(const std::string & name,const char * args[])95 static 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]);
112     close(addr2line_to_caller[0]);
113     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 
Drain(size_t expected,const char * prefix,std::unique_ptr<Addr2linePipe> * pipe,std::ostream & os)138 static 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 (;;) {
150     constexpr uint32_t kWaitTimeExpectedMilli = 500;
151     constexpr uint32_t kWaitTimeUnexpectedMilli = 50;
152 
153     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.
158       pipe->reset();
159       return;
160     }
161 
162     if (retval == 0) {
163       // Timeout.
164       return;
165     }
166 
167     if (!(read_fd.revents & POLLIN)) {
168       // addr2line call exited.
169       pipe->reset();
170       return;
171     }
172 
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));
178     if (bytes_read <= 0) {
179       // This should not really happen...
180       pipe->reset();
181       return;
182     }
183     buffer[bytes_read] = '\0';
184 
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--;
208         }
209       }
210     }
211   }
212 }
213 
Addr2line(const std::string & map_src,uintptr_t offset,std::ostream & os,const char * prefix,std::unique_ptr<Addr2linePipe> * pipe)214 static 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 
221   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.
225     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 
RunCommand(const std::string & cmd)265 static bool RunCommand(const std::string& cmd) {
266   FILE* stream = popen(cmd.c_str(), "r");
267   if (stream) {
268     pclose(stream);
269     return true;
270   } else {
271     return false;
272   }
273 }
274 
PcIsWithinQuickCode(ArtMethod * method,uintptr_t pc)275 static 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   }
281   uintptr_t code_size = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].GetCodeSize();
282   return code <= pc && pc <= (code + code_size);
283 }
284 
DumpNativeStack(std::ostream & os,pid_t tid,BacktraceMap * existing_map,const char * prefix,ArtMethod * current_method,void * ucontext_ptr,bool skip_frames)285 void DumpNativeStack(std::ostream& os,
286                      pid_t tid,
287                      BacktraceMap* existing_map,
288                      const char* prefix,
289                      ArtMethod* current_method,
290                      void* ucontext_ptr,
291                      bool skip_frames) {
292   // 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));
304   backtrace->SetSkipFrames(skip_frames);
305   if (!backtrace->Unwind(0, reinterpret_cast<ucontext*>(ucontext_ptr))) {
306     os << prefix << "(backtrace::Unwind failed for thread " << tid
307        << ": " <<  backtrace->GetErrorString(backtrace->GetError()) << ")" << std::endl;
308     return;
309   } else if (backtrace->NumFrames() == 0) {
310     os << prefix << "(no native stack frames for thread " << tid << ")" << std::endl;
311     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.
319     use_addr2line = (gAborting > 0) && RunCommand("addr2line -h");
320   } else {
321     use_addr2line = false;
322   }
323 
324   std::unique_ptr<Addr2linePipe> addr2line_state;
325 
326   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)) {
339       os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 "  ???"
340                                                             : "%08" PRIx64 "  ???",
341                          it->pc);
342     } else {
343       os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 "  "
344                                                             : "%08" PRIx64 "  ",
345                          it->rel_pc);
346       if (it->map.name.empty()) {
347         os << StringPrintf("<anonymous:%" PRIx64 ">", it->map.start);
348       } else {
349         os << it->map.name;
350       }
351       if (it->map.offset != 0) {
352         os << StringPrintf(" (offset %" PRIx64 ")", it->map.offset);
353       }
354       os << " (";
355       if (!it->func_name.empty()) {
356         os << it->func_name;
357         if (it->func_offset != 0) {
358           os << "+" << it->func_offset;
359         }
360         // 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         }
365       } 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();
369         os << current_method->JniLongName() << "+"
370            << (it->pc - reinterpret_cast<uint64_t>(start_of_code));
371       } else {
372         os << "???";
373       }
374       os << ")";
375     }
376     os << std::endl;
377     if (try_addr2line && use_addr2line) {
378       Addr2line(it->map.name, it->pc - it->map.start, os, prefix, &addr2line_state);
379     }
380   }
381 
382   if (addr2line_state != nullptr) {
383     Drain(0, prefix, &addr2line_state, os);
384   }
385 }
386 
DumpKernelStack(std::ostream & os,pid_t tid,const char * prefix,bool include_count)387 void 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);
402   if (kernel_stack_frames.empty()) {
403     os << prefix << "(" << kernel_stack_filename << " is empty)\n";
404     return;
405   }
406   // 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     }
421     os << text << std::endl;
422   }
423 }
424 
425 #elif defined(__APPLE__)
426 
427 void 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,
432                      void* ucontext_ptr ATTRIBUTE_UNUSED,
433                      bool skip_frames ATTRIBUTE_UNUSED) {
434 }
435 
436 void 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
447