Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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> |
Christopher Ferris | 5e074ed | 2018-03-06 14:02:55 -0800 | [diff] [blame] | 35 | #include <poll.h> |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 36 | #include <signal.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <sys/time.h> |
| 39 | #include <sys/types.h> |
| 40 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 41 | #include "android-base/stringprintf.h" |
Christopher Ferris | 0f0cb32 | 2018-03-07 14:10:49 -0800 | [diff] [blame] | 42 | #include "android-base/strings.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 43 | |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 44 | #include "arch/instruction_set.h" |
Andreas Gampe | 39b378c | 2017-12-07 15:44:13 -0800 | [diff] [blame] | 45 | #include "base/aborting.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 46 | #include "base/file_utils.h" |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 47 | #include "base/memory_tool.h" |
| 48 | #include "base/mutex.h" |
David Sehr | 8f4b056 | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 49 | #include "base/os.h" |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 50 | #include "base/unix_file/fd_file.h" |
David Sehr | 8f4b056 | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 51 | #include "base/utils.h" |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 52 | #include "oat_quick_method_header.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 53 | #include "thread-current-inl.h" |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 54 | |
| 55 | #endif |
| 56 | |
| 57 | namespace art { |
| 58 | |
| 59 | #if defined(__linux__) |
| 60 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 61 | using android::base::StringPrintf; |
| 62 | |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 63 | static constexpr bool kUseAddr2line = !kIsTargetBuild; |
| 64 | |
| 65 | ALWAYS_INLINE |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 66 | static inline void WritePrefix(std::ostream& os, const char* prefix, bool odd) { |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 67 | if (prefix != nullptr) { |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 68 | os << prefix; |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 69 | } |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 70 | os << " "; |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 71 | if (!odd) { |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 72 | os << " "; |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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 { |
| 79 | 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 | |
| 82 | ~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 | |
| 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]); |
Calin Juravle | 0ed6c80 | 2017-03-27 18:12:05 -0700 | [diff] [blame] | 112 | close(addr2line_to_caller[0]); |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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 | |
| 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 (;;) { |
Christopher Ferris | 5e074ed | 2018-03-06 14:02:55 -0800 | [diff] [blame] | 150 | constexpr uint32_t kWaitTimeExpectedMilli = 500; |
| 151 | constexpr uint32_t kWaitTimeUnexpectedMilli = 50; |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 152 | |
Christopher Ferris | 5e074ed | 2018-03-06 14:02:55 -0800 | [diff] [blame] | 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. |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 158 | pipe->reset(); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | if (retval == 0) { |
| 163 | // Timeout. |
| 164 | return; |
| 165 | } |
| 166 | |
Christopher Ferris | 5e074ed | 2018-03-06 14:02:55 -0800 | [diff] [blame] | 167 | if (!(read_fd.revents & POLLIN)) { |
| 168 | // addr2line call exited. |
| 169 | pipe->reset(); |
| 170 | return; |
| 171 | } |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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)); |
Christopher Ferris | 5e074ed | 2018-03-06 14:02:55 -0800 | [diff] [blame] | 178 | if (bytes_read <= 0) { |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 179 | // This should not really happen... |
| 180 | pipe->reset(); |
| 181 | return; |
| 182 | } |
Christopher Ferris | 5e074ed | 2018-03-06 14:02:55 -0800 | [diff] [blame] | 183 | buffer[bytes_read] = '\0'; |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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--; |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 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 | |
Christopher Ferris | 0f0cb32 | 2018-03-07 14:10:49 -0800 | [diff] [blame] | 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. |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 265 | static bool RunCommand(const std::string& cmd) { |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 266 | FILE* stream = popen(cmd.c_str(), "r"); |
| 267 | if (stream) { |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 268 | pclose(stream); |
| 269 | return true; |
| 270 | } else { |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 281 | uintptr_t code_size = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].GetCodeSize(); |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 282 | return code <= pc && pc <= (code + code_size); |
| 283 | } |
| 284 | |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 285 | void DumpNativeStack(std::ostream& os, |
| 286 | pid_t tid, |
| 287 | BacktraceMap* existing_map, |
| 288 | const char* prefix, |
| 289 | ArtMethod* current_method, |
Christopher Ferris | 55ecc9f | 2018-03-23 13:03:45 -0700 | [diff] [blame] | 290 | void* ucontext_ptr, |
| 291 | bool skip_frames) { |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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)); |
Christopher Ferris | 55ecc9f | 2018-03-23 13:03:45 -0700 | [diff] [blame] | 304 | backtrace->SetSkipFrames(skip_frames); |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 305 | if (!backtrace->Unwind(0, reinterpret_cast<ucontext*>(ucontext_ptr))) { |
| 306 | os << prefix << "(backtrace::Unwind failed for thread " << tid |
Andreas Gampe | ef29536 | 2016-10-11 20:04:11 -0700 | [diff] [blame] | 307 | << ": " << backtrace->GetErrorString(backtrace->GetError()) << ")" << std::endl; |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 308 | return; |
| 309 | } else if (backtrace->NumFrames() == 0) { |
Andreas Gampe | ef29536 | 2016-10-11 20:04:11 -0700 | [diff] [blame] | 310 | os << prefix << "(no native stack frames for thread " << tid << ")" << std::endl; |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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. |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 319 | use_addr2line = (gAborting > 0) && RunCommand("addr2line -h"); |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 320 | } else { |
| 321 | use_addr2line = false; |
| 322 | } |
| 323 | |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 324 | std::unique_ptr<Addr2linePipe> addr2line_state; |
| 325 | |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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)) { |
Christopher Ferris | 77b38df | 2018-01-18 16:16:49 -0800 | [diff] [blame] | 339 | os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 " ???" |
| 340 | : "%08" PRIx64 " ???", |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 341 | it->pc); |
| 342 | } else { |
Christopher Ferris | 77b38df | 2018-01-18 16:16:49 -0800 | [diff] [blame] | 343 | os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIx64 " " |
| 344 | : "%08" PRIx64 " ", |
Christopher Ferris | f88b5c0 | 2017-07-19 14:18:33 -0700 | [diff] [blame] | 345 | it->rel_pc); |
Christopher Ferris | 8bd7d1b | 2018-01-08 11:12:40 -0800 | [diff] [blame] | 346 | if (it->map.name.empty()) { |
Christopher Ferris | 77b38df | 2018-01-18 16:16:49 -0800 | [diff] [blame] | 347 | os << StringPrintf("<anonymous:%" PRIx64 ">", it->map.start); |
Christopher Ferris | 8bd7d1b | 2018-01-08 11:12:40 -0800 | [diff] [blame] | 348 | } else { |
| 349 | os << it->map.name; |
| 350 | } |
Christopher Ferris | 53ef6a6 | 2018-02-09 23:13:27 -0800 | [diff] [blame] | 351 | if (it->map.offset != 0) { |
| 352 | os << StringPrintf(" (offset %" PRIx64 ")", it->map.offset); |
| 353 | } |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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 | } |
Christopher Ferris | 8bd7d1b | 2018-01-08 11:12:40 -0800 | [diff] [blame] | 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 | } |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 369 | os << current_method->JniLongName() << "+" |
Christopher Ferris | 77b38df | 2018-01-18 16:16:49 -0800 | [diff] [blame] | 370 | << (it->pc - reinterpret_cast<uint64_t>(start_of_code)); |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 371 | } else { |
| 372 | os << "???"; |
| 373 | } |
| 374 | os << ")"; |
| 375 | } |
Andreas Gampe | ef29536 | 2016-10-11 20:04:11 -0700 | [diff] [blame] | 376 | os << std::endl; |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 377 | if (try_addr2line && use_addr2line) { |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 378 | Addr2line(it->map.name, it->pc - it->map.start, os, prefix, &addr2line_state); |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
Andreas Gampe | fcccbaf | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 381 | |
| 382 | if (addr2line_state != nullptr) { |
| 383 | Drain(0, prefix, &addr2line_state, os); |
| 384 | } |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 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); |
yuanhao | 7a8b3f2 | 2018-02-01 15:58:53 +0800 | [diff] [blame] | 402 | if (kernel_stack_frames.empty()) { |
| 403 | os << prefix << "(" << kernel_stack_filename << " is empty)\n"; |
| 404 | return; |
| 405 | } |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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 | } |
Andreas Gampe | ef29536 | 2016-10-11 20:04:11 -0700 | [diff] [blame] | 421 | os << text << std::endl; |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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, |
Christopher Ferris | a89c832 | 2018-03-27 17:04:44 -0700 | [diff] [blame] | 432 | void* ucontext_ptr ATTRIBUTE_UNUSED, |
| 433 | bool skip_frames ATTRIBUTE_UNUSED) { |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 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 |