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