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 #include <string_view>
22 
23 #include <stdio.h>
24 
25 #include "art_method.h"
26 
27 // For DumpNativeStack.
28 #include <unwindstack/AndroidUnwinder.h>
29 
30 #if defined(__linux__)
31 
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/file.h"
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/oat_quick_method_header.h"
57 #include "runtime.h"
58 #include "thread-current-inl.h"
59 
60 #endif
61 
62 namespace art HIDDEN {
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 !defined(ART_TARGET) && !defined(ART_CLANG_PATH)
72   #error "ART_CLANG_PATH must be defined on host build"
73 #endif
74 #if defined(ART_CLANG_PATH)
75   const char* env_value = getenv("ANDROID_BUILD_TOP");
76   std::string_view top(env_value != nullptr ? env_value : ".");
77   return std::string(top) + "/" + ART_CLANG_PATH + "/bin/llvm-addr2line";
78 #else
79   return std::string("llvm-addr2line");
80 #endif
81 }
82 
83 ALWAYS_INLINE
WritePrefix(std::ostream & os,const char * prefix,bool odd)84 static inline void WritePrefix(std::ostream& os, const char* prefix, bool odd) {
85   if (prefix != nullptr) {
86     os << prefix;
87   }
88   os << "  ";
89   if (!odd) {
90     os << " ";
91   }
92 }
93 
94 // The state of an open pipe to addr2line. In "server" mode, addr2line takes input on stdin
95 // and prints the result to stdout. This struct keeps the state of the open connection.
96 struct Addr2linePipe {
Addr2linePipeart::Addr2linePipe97   Addr2linePipe(int in_fd, int out_fd, const std::string& file_name, pid_t pid)
98       : in(in_fd, false), out(out_fd, false), file(file_name), child_pid(pid), odd(true) {}
99 
~Addr2linePipeart::Addr2linePipe100   ~Addr2linePipe() {
101     kill(child_pid, SIGKILL);
102   }
103 
104   File in;      // The file descriptor that is connected to the output of addr2line.
105   File out;     // The file descriptor that is connected to the input of addr2line.
106 
107   const std::string file;     // The file addr2line is working on, so that we know when to close
108                               // and restart.
109   const pid_t child_pid;      // The pid of the child, which we should kill when we're done.
110   bool odd;                   // Print state for indentation of lines.
111 };
112 
Connect(const std::string & name,const char * args[])113 static std::unique_ptr<Addr2linePipe> Connect(const std::string& name, const char* args[]) {
114   int caller_to_addr2line[2];
115   int addr2line_to_caller[2];
116 
117   if (pipe(caller_to_addr2line) == -1) {
118     return nullptr;
119   }
120   if (pipe(addr2line_to_caller) == -1) {
121     close(caller_to_addr2line[0]);
122     close(caller_to_addr2line[1]);
123     return nullptr;
124   }
125 
126   pid_t pid = fork();
127   if (pid == -1) {
128     close(caller_to_addr2line[0]);
129     close(caller_to_addr2line[1]);
130     close(addr2line_to_caller[0]);
131     close(addr2line_to_caller[1]);
132     return nullptr;
133   }
134 
135   if (pid == 0) {
136     dup2(caller_to_addr2line[0], STDIN_FILENO);
137     dup2(addr2line_to_caller[1], STDOUT_FILENO);
138 
139     close(caller_to_addr2line[0]);
140     close(caller_to_addr2line[1]);
141     close(addr2line_to_caller[0]);
142     close(addr2line_to_caller[1]);
143 
144     execv(args[0], const_cast<char* const*>(args));
145     exit(1);
146   } else {
147     close(caller_to_addr2line[0]);
148     close(addr2line_to_caller[1]);
149     return std::make_unique<Addr2linePipe>(addr2line_to_caller[0],
150                                            caller_to_addr2line[1],
151                                            name,
152                                            pid);
153   }
154 }
155 
Drain(size_t expected,const char * prefix,std::unique_ptr<Addr2linePipe> * pipe,std::ostream & os)156 static void Drain(size_t expected,
157                   const char* prefix,
158                   std::unique_ptr<Addr2linePipe>* pipe /* inout */,
159                   std::ostream& os) {
160   DCHECK(pipe != nullptr);
161   DCHECK(pipe->get() != nullptr);
162   int in = pipe->get()->in.Fd();
163   DCHECK_GE(in, 0);
164 
165   bool prefix_written = false;
166 
167   for (;;) {
168     constexpr uint32_t kWaitTimeExpectedMilli = 500;
169     constexpr uint32_t kWaitTimeUnexpectedMilli = 50;
170 
171     int timeout = expected > 0 ? kWaitTimeExpectedMilli : kWaitTimeUnexpectedMilli;
172     struct pollfd read_fd{in, POLLIN, 0};
173     int retval = TEMP_FAILURE_RETRY(poll(&read_fd, 1, timeout));
174     if (retval == -1) {
175       // An error occurred.
176       pipe->reset();
177       return;
178     }
179 
180     if (retval == 0) {
181       // Timeout.
182       return;
183     }
184 
185     if (!(read_fd.revents & POLLIN)) {
186       // addr2line call exited.
187       pipe->reset();
188       return;
189     }
190 
191     constexpr size_t kMaxBuffer = 128;  // Relatively small buffer. Should be OK as we're on an
192     // alt stack, but just to be sure...
193     char buffer[kMaxBuffer];
194     memset(buffer, 0, kMaxBuffer);
195     int bytes_read = TEMP_FAILURE_RETRY(read(in, buffer, kMaxBuffer - 1));
196     if (bytes_read <= 0) {
197       // This should not really happen...
198       pipe->reset();
199       return;
200     }
201     buffer[bytes_read] = '\0';
202 
203     char* tmp = buffer;
204     while (*tmp != 0) {
205       if (!prefix_written) {
206         WritePrefix(os, prefix, (*pipe)->odd);
207         prefix_written = true;
208       }
209       char* new_line = strchr(tmp, '\n');
210       if (new_line == nullptr) {
211         os << tmp;
212 
213         break;
214       } else {
215         char saved = *(new_line + 1);
216         *(new_line + 1) = 0;
217         os << tmp;
218         *(new_line + 1) = saved;
219 
220         tmp = new_line + 1;
221         prefix_written = false;
222         (*pipe)->odd = !(*pipe)->odd;
223 
224         if (expected > 0) {
225           expected--;
226         }
227       }
228     }
229   }
230 }
231 
Addr2line(const std::string & map_src,uintptr_t offset,std::ostream & os,const char * prefix,std::unique_ptr<Addr2linePipe> * pipe)232 static void Addr2line(const std::string& map_src,
233                       uintptr_t offset,
234                       std::ostream& os,
235                       const char* prefix,
236                       std::unique_ptr<Addr2linePipe>* pipe /* inout */) {
237   std::array<const char*, 3> kIgnoreSuffixes{ ".dex", ".jar", ".vdex" };
238   for (const char* ignore_suffix : kIgnoreSuffixes) {
239     if (map_src.ends_with(ignore_suffix)) {
240       // Ignore file names that do not have map information addr2line can consume. e.g. vdex
241       // files are special frames injected for the interpreter so they don't have any line
242       // number information available.
243       return;
244     }
245   }
246   if (map_src == "[vdso]") {
247     // addr2line will not work on the vdso.
248     return;
249   }
250 
251   if (*pipe == nullptr || (*pipe)->file != map_src) {
252     if (*pipe != nullptr) {
253       Drain(0, prefix, pipe, os);
254     }
255     pipe->reset();  // Close early.
256 
257     std::string addr2linePath = FindAddr2line();
258     const char* args[7] = {
259         addr2linePath.c_str(),
260         "--functions",
261         "--inlines",
262         "--demangle",
263         "-e",
264         map_src.c_str(),
265         nullptr
266     };
267     *pipe = Connect(map_src, args);
268   }
269 
270   Addr2linePipe* pipe_ptr = pipe->get();
271   if (pipe_ptr == nullptr) {
272     // Failed...
273     return;
274   }
275 
276   // Send the offset.
277   const std::string hex_offset = StringPrintf("0x%zx\n", offset);
278 
279   if (!pipe_ptr->out.WriteFully(hex_offset.data(), hex_offset.length())) {
280     // Error. :-(
281     pipe->reset();
282     return;
283   }
284 
285   // Now drain (expecting two lines).
286   Drain(2U, prefix, pipe, os);
287 }
288 
RunCommand(const std::string & cmd)289 static bool RunCommand(const std::string& cmd) {
290   FILE* stream = popen(cmd.c_str(), "r");
291   if (stream) {
292     // Consume the stdout until we encounter EOF when the tool exits.
293     // Otherwise the tool would complain to stderr when the stream is closed.
294     char buffer[64];
295     while (fread(buffer, 1, sizeof(buffer), stream) == sizeof(buffer)) {}
296     pclose(stream);
297     return true;
298   } else {
299     return false;
300   }
301 }
302 
303 // Remove method parameters by finding matching top-level parenthesis and removing them.
304 // Since functions can be defined inside functions, this can remove multiple substrings.
StripParameters(std::string name)305 std::string StripParameters(std::string name) {
306   size_t end = name.size();
307   int nesting = 0;
308   for (ssize_t i = name.size() - 1; i > 0; i--) {
309     if (name[i] == ')' && nesting++ == 0) {
310       end = i + 1;
311     }
312     if (name[i] == '(' && --nesting == 0) {
313       name = name.erase(i, end - i);
314     }
315   }
316   return name;
317 }
318 
DumpNativeStack(std::ostream & os,pid_t tid,const char * prefix,ArtMethod * current_method,void * ucontext_ptr,bool skip_frames)319 void DumpNativeStack(std::ostream& os,
320                      pid_t tid,
321                      const char* prefix,
322                      ArtMethod* current_method,
323                      void* ucontext_ptr,
324                      bool skip_frames) {
325   unwindstack::AndroidLocalUnwinder unwinder;
326   DumpNativeStack(os, unwinder, tid, prefix, current_method, ucontext_ptr, skip_frames);
327 }
328 
DumpNativeStack(std::ostream & os,unwindstack::AndroidLocalUnwinder & unwinder,pid_t tid,const char * prefix,ArtMethod * current_method,void * ucontext_ptr,bool skip_frames)329 void DumpNativeStack(std::ostream& os,
330                      unwindstack::AndroidLocalUnwinder& unwinder,
331                      pid_t tid,
332                      const char* prefix,
333                      ArtMethod* current_method,
334                      void* ucontext_ptr,
335                      bool skip_frames) {
336   // Historical note: This was disabled when running under Valgrind (b/18119146).
337 
338   unwindstack::AndroidUnwinderData data(!skip_frames /*show_all_frames*/);
339   bool unwind_ret;
340   if (ucontext_ptr != nullptr) {
341     unwind_ret = unwinder.Unwind(ucontext_ptr, data);
342   } else {
343     unwind_ret = unwinder.Unwind(tid, data);
344   }
345   if (!unwind_ret) {
346     os << prefix << "(Unwind failed for thread " << tid << ": "
347        <<  data.GetErrorString() << ")" << std::endl;
348     return;
349   }
350 
351   // Check whether we have and should use addr2line.
352   bool use_addr2line;
353   if (kUseAddr2line) {
354     // Try to run it to see whether we have it. Push an argument so that it doesn't assume a.out
355     // and print to stderr.
356     use_addr2line = (gAborting > 0) && RunCommand(FindAddr2line() + " -h");
357   } else {
358     use_addr2line = false;
359   }
360 
361   std::unique_ptr<Addr2linePipe> addr2line_state;
362   data.DemangleFunctionNames();
363   bool holds_mutator_lock =  Locks::mutator_lock_->IsSharedHeld(Thread::Current());
364   for (const unwindstack::FrameData& frame : data.frames) {
365     // We produce output like this:
366     // ]    #00 pc 000075bb8  /system/lib/libc.so (unwind_backtrace_thread+536)
367     // In order for parsing tools to continue to function, the stack dump
368     // format must at least adhere to this format:
369     //  #XX pc <RELATIVE_ADDR>  <FULL_PATH_TO_SHARED_LIBRARY> ...
370     // The parsers require a single space before and after pc, and two spaces
371     // after the <RELATIVE_ADDR>. There can be any prefix data before the
372     // #XX. <RELATIVE_ADDR> has to be a hex number but with no 0x prefix.
373     os << prefix << StringPrintf("#%02zu pc ", frame.num);
374     bool try_addr2line = false;
375     if (frame.map_info == nullptr) {
376       os << StringPrintf("%08" PRIx64 "  ???", frame.pc);
377     } else {
378       os << StringPrintf("%08" PRIx64 "  ", frame.rel_pc);
379       const std::shared_ptr<unwindstack::MapInfo>& map_info = frame.map_info;
380       if (map_info->name().empty()) {
381         os << StringPrintf("<anonymous:%" PRIx64 ">", map_info->start());
382       } else {
383         os << map_info->name().c_str();
384       }
385       if (map_info->elf_start_offset() != 0) {
386         os << StringPrintf(" (offset %" PRIx64 ")", map_info->elf_start_offset());
387       }
388       os << " (";
389       if (!frame.function_name.empty()) {
390         // Remove parameters from the printed function name to improve signal/noise in the logs.
391         // Also, ANRs are often trimmed, so printing less means we get more useful data out.
392         // We can still symbolize the function based on the PC and build-id (including inlining).
393         os << StripParameters(frame.function_name.c_str());
394         if (frame.function_offset != 0) {
395           os << "+" << frame.function_offset;
396         }
397         // Functions found using the gdb jit interface will be in an empty
398         // map that cannot be found using addr2line.
399         if (!map_info->name().empty()) {
400           try_addr2line = true;
401         }
402       } else if (current_method != nullptr && holds_mutator_lock) {
403         const OatQuickMethodHeader* header = current_method->GetOatQuickMethodHeader(frame.pc);
404         if (header != nullptr) {
405           const void* start_of_code = header->GetCode();
406           os << current_method->JniLongName() << "+"
407              << (frame.pc - reinterpret_cast<uint64_t>(start_of_code));
408         } else {
409           os << "???";
410         }
411       } else {
412         os << "???";
413       }
414       os << ")";
415       std::string build_id = map_info->GetPrintableBuildID();
416       if (!build_id.empty()) {
417         os << " (BuildId: " << build_id << ")";
418       }
419     }
420     os << std::endl;
421     if (try_addr2line && use_addr2line) {
422       // Guaranteed that map_info is not nullptr and name is non-empty.
423       Addr2line(frame.map_info->name(), frame.rel_pc, os, prefix, &addr2line_state);
424     }
425   }
426 
427   if (addr2line_state != nullptr) {
428     Drain(0, prefix, &addr2line_state, os);
429   }
430 }
431 
432 #elif defined(__APPLE__)
433 
434 void DumpNativeStack([[maybe_unused]] std::ostream& os,
435                      [[maybe_unused]] pid_t tid,
436                      [[maybe_unused]] const char* prefix,
437                      [[maybe_unused]] ArtMethod* current_method,
438                      [[maybe_unused]] void* ucontext_ptr,
439                      [[maybe_unused]] bool skip_frames) {}
440 
441 void DumpNativeStack([[maybe_unused]] std::ostream& os,
442                      [[maybe_unused]] unwindstack::AndroidLocalUnwinder& existing_map,
443                      [[maybe_unused]] pid_t tid,
444                      [[maybe_unused]] const char* prefix,
445                      [[maybe_unused]] ArtMethod* current_method,
446                      [[maybe_unused]] void* ucontext_ptr,
447                      [[maybe_unused]] bool skip_frames) {}
448 
449 #else
450 #error "Unsupported architecture for native stack dumps."
451 #endif
452 
453 }  // namespace art
454