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