|
Packit Service |
9def5d |
// Copyright (c) 2006, Google Inc.
|
|
Packit Service |
9def5d |
// All rights reserved.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Redistribution and use in source and binary forms, with or without
|
|
Packit Service |
9def5d |
// modification, are permitted provided that the following conditions are
|
|
Packit Service |
9def5d |
// met:
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// * Redistributions of source code must retain the above copyright
|
|
Packit Service |
9def5d |
// notice, this list of conditions and the following disclaimer.
|
|
Packit Service |
9def5d |
// * Redistributions in binary form must reproduce the above
|
|
Packit Service |
9def5d |
// copyright notice, this list of conditions and the following disclaimer
|
|
Packit Service |
9def5d |
// in the documentation and/or other materials provided with the
|
|
Packit Service |
9def5d |
// distribution.
|
|
Packit Service |
9def5d |
// * Neither the name of Google Inc. nor the names of its
|
|
Packit Service |
9def5d |
// contributors may be used to endorse or promote products derived from
|
|
Packit Service |
9def5d |
// this software without specific prior written permission.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit Service |
9def5d |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit Service |
9def5d |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit Service |
9def5d |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit Service |
9def5d |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit Service |
9def5d |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit Service |
9def5d |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit Service |
9def5d |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit Service |
9def5d |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit Service |
9def5d |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit Service |
9def5d |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Author: Satoru Takabayashi
|
|
Packit Service |
9def5d |
// Stack-footprint reduction work done by Raksit Ashok
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Implementation note:
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// We don't use heaps but only use stacks. We want to reduce the
|
|
Packit Service |
9def5d |
// stack consumption so that the symbolizer can run on small stacks.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Here are some numbers collected with GCC 4.1.0 on x86:
|
|
Packit Service |
9def5d |
// - sizeof(Elf32_Sym) = 16
|
|
Packit Service |
9def5d |
// - sizeof(Elf32_Shdr) = 40
|
|
Packit Service |
9def5d |
// - sizeof(Elf64_Sym) = 24
|
|
Packit Service |
9def5d |
// - sizeof(Elf64_Shdr) = 64
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// This implementation is intended to be async-signal-safe but uses
|
|
Packit Service |
9def5d |
// some functions which are not guaranteed to be so, such as memchr()
|
|
Packit Service |
9def5d |
// and memmove(). We assume they are async-signal-safe.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Additional header can be specified by the GLOG_BUILD_CONFIG_INCLUDE
|
|
Packit Service |
9def5d |
// macro to add platform specific defines (e.g. OS_OPENBSD).
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#ifdef GLOG_BUILD_CONFIG_INCLUDE
|
|
Packit Service |
9def5d |
#include GLOG_BUILD_CONFIG_INCLUDE
|
|
Packit Service |
9def5d |
#endif // GLOG_BUILD_CONFIG_INCLUDE
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include "utilities.h"
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#if defined(HAVE_SYMBOLIZE)
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include <limits>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include "symbolize.h"
|
|
Packit Service |
9def5d |
#include "demangle.h"
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_START_GOOGLE_NAMESPACE_
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// We don't use assert() since it's not guaranteed to be
|
|
Packit Service |
9def5d |
// async-signal-safe. Instead we define a minimal assertion
|
|
Packit Service |
9def5d |
// macro. So far, we don't need pretty printing for __FILE__, etc.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// A wrapper for abort() to make it callable in ? :.
|
|
Packit Service |
9def5d |
static int AssertFail() {
|
|
Packit Service |
9def5d |
abort();
|
|
Packit Service |
9def5d |
return 0; // Should not reach.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#define SAFE_ASSERT(expr) ((expr) ? 0 : AssertFail())
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
static SymbolizeCallback g_symbolize_callback = NULL;
|
|
Packit Service |
9def5d |
void InstallSymbolizeCallback(SymbolizeCallback callback) {
|
|
Packit Service |
9def5d |
g_symbolize_callback = callback;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
static SymbolizeOpenObjectFileCallback g_symbolize_open_object_file_callback =
|
|
Packit Service |
9def5d |
NULL;
|
|
Packit Service |
9def5d |
void InstallSymbolizeOpenObjectFileCallback(
|
|
Packit Service |
9def5d |
SymbolizeOpenObjectFileCallback callback) {
|
|
Packit Service |
9def5d |
g_symbolize_open_object_file_callback = callback;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// This function wraps the Demangle function to provide an interface
|
|
Packit Service |
9def5d |
// where the input symbol is demangled in-place.
|
|
Packit Service |
9def5d |
// To keep stack consumption low, we would like this function to not
|
|
Packit Service |
9def5d |
// get inlined.
|
|
Packit Service |
9def5d |
static ATTRIBUTE_NOINLINE void DemangleInplace(char *out, int out_size) {
|
|
Packit Service |
9def5d |
char demangled[256]; // Big enough for sane demangled symbols.
|
|
Packit Service |
9def5d |
if (Demangle(out, demangled, sizeof(demangled))) {
|
|
Packit Service |
9def5d |
// Demangling succeeded. Copy to out if the space allows.
|
|
Packit Service |
9def5d |
size_t len = strlen(demangled);
|
|
Packit Service |
9def5d |
if (len + 1 <= (size_t)out_size) { // +1 for '\0'.
|
|
Packit Service |
9def5d |
SAFE_ASSERT(len < sizeof(demangled));
|
|
Packit Service |
9def5d |
memmove(out, demangled, len + 1);
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_END_GOOGLE_NAMESPACE_
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#if defined(__ELF__)
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include <dlfcn.h>
|
|
Packit Service |
9def5d |
#if defined(OS_OPENBSD)
|
|
Packit Service |
9def5d |
#include <sys/exec_elf.h>
|
|
Packit Service |
9def5d |
#else
|
|
Packit Service |
9def5d |
#include <elf.h>
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
#include <errno.h>
|
|
Packit Service |
9def5d |
#include <fcntl.h>
|
|
Packit Service |
9def5d |
#include <limits.h>
|
|
Packit Service |
9def5d |
#include <stdint.h>
|
|
Packit Service |
9def5d |
#include <stdio.h>
|
|
Packit Service |
9def5d |
#include <stdlib.h>
|
|
Packit Service |
9def5d |
#include <stddef.h>
|
|
Packit Service |
9def5d |
#include <string.h>
|
|
Packit Service |
9def5d |
#include <sys/stat.h>
|
|
Packit Service |
9def5d |
#include <sys/types.h>
|
|
Packit Service |
9def5d |
#include <unistd.h>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include "symbolize.h"
|
|
Packit Service |
9def5d |
#include "config.h"
|
|
Packit Service |
9def5d |
#include "glog/raw_logging.h"
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Re-runs fn until it doesn't cause EINTR.
|
|
Packit Service |
9def5d |
#define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR)
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_START_GOOGLE_NAMESPACE_
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Read up to "count" bytes from file descriptor "fd" into the buffer
|
|
Packit Service |
9def5d |
// starting at "buf" while handling short reads and EINTR. On
|
|
Packit Service |
9def5d |
// success, return the number of bytes read. Otherwise, return -1.
|
|
Packit Service |
9def5d |
static ssize_t ReadPersistent(const int fd, void *buf, const size_t count) {
|
|
Packit Service |
9def5d |
SAFE_ASSERT(fd >= 0);
|
|
Packit Service |
9def5d |
SAFE_ASSERT(count <= std::numeric_limits<ssize_t>::max());
|
|
Packit Service |
9def5d |
char *buf0 = reinterpret_cast<char *>(buf);
|
|
Packit Service |
9def5d |
ssize_t num_bytes = 0;
|
|
Packit Service |
9def5d |
while (num_bytes < count) {
|
|
Packit Service |
9def5d |
ssize_t len;
|
|
Packit Service |
9def5d |
NO_INTR(len = read(fd, buf0 + num_bytes, count - num_bytes));
|
|
Packit Service |
9def5d |
if (len < 0) { // There was an error other than EINTR.
|
|
Packit Service |
9def5d |
return -1;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
if (len == 0) { // Reached EOF.
|
|
Packit Service |
9def5d |
break;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
num_bytes += len;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
SAFE_ASSERT(num_bytes <= count);
|
|
Packit Service |
9def5d |
return num_bytes;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Read up to "count" bytes from "offset" in the file pointed by file
|
|
Packit Service |
9def5d |
// descriptor "fd" into the buffer starting at "buf". On success,
|
|
Packit Service |
9def5d |
// return the number of bytes read. Otherwise, return -1.
|
|
Packit Service |
9def5d |
static ssize_t ReadFromOffset(const int fd, void *buf,
|
|
Packit Service |
9def5d |
const size_t count, const off_t offset) {
|
|
Packit Service |
9def5d |
off_t off = lseek(fd, offset, SEEK_SET);
|
|
Packit Service |
9def5d |
if (off == (off_t)-1) {
|
|
Packit Service |
9def5d |
return -1;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
return ReadPersistent(fd, buf, count);
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Try reading exactly "count" bytes from "offset" bytes in a file
|
|
Packit Service |
9def5d |
// pointed by "fd" into the buffer starting at "buf" while handling
|
|
Packit Service |
9def5d |
// short reads and EINTR. On success, return true. Otherwise, return
|
|
Packit Service |
9def5d |
// false.
|
|
Packit Service |
9def5d |
static bool ReadFromOffsetExact(const int fd, void *buf,
|
|
Packit Service |
9def5d |
const size_t count, const off_t offset) {
|
|
Packit Service |
9def5d |
ssize_t len = ReadFromOffset(fd, buf, count, offset);
|
|
Packit Service |
9def5d |
return len == count;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Returns elf_header.e_type if the file pointed by fd is an ELF binary.
|
|
Packit Service |
9def5d |
static int FileGetElfType(const int fd) {
|
|
Packit Service |
9def5d |
ElfW(Ehdr) elf_header;
|
|
Packit Service |
9def5d |
if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
|
|
Packit Service |
9def5d |
return -1;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
if (memcmp(elf_header.e_ident, ELFMAG, SELFMAG) != 0) {
|
|
Packit Service |
9def5d |
return -1;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
return elf_header.e_type;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Read the section headers in the given ELF binary, and if a section
|
|
Packit Service |
9def5d |
// of the specified type is found, set the output to this section header
|
|
Packit Service |
9def5d |
// and return true. Otherwise, return false.
|
|
Packit Service |
9def5d |
// To keep stack consumption low, we would like this function to not get
|
|
Packit Service |
9def5d |
// inlined.
|
|
Packit Service |
9def5d |
static ATTRIBUTE_NOINLINE bool
|
|
Packit Service |
9def5d |
GetSectionHeaderByType(const int fd, ElfW(Half) sh_num, const off_t sh_offset,
|
|
Packit Service |
9def5d |
ElfW(Word) type, ElfW(Shdr) *out) {
|
|
Packit Service |
9def5d |
// Read at most 16 section headers at a time to save read calls.
|
|
Packit Service |
9def5d |
ElfW(Shdr) buf[16];
|
|
Packit Service |
9def5d |
for (int i = 0; i < sh_num;) {
|
|
Packit Service |
9def5d |
const ssize_t num_bytes_left = (sh_num - i) * sizeof(buf[0]);
|
|
Packit Service |
9def5d |
const ssize_t num_bytes_to_read =
|
|
Packit Service |
9def5d |
(sizeof(buf) > num_bytes_left) ? num_bytes_left : sizeof(buf);
|
|
Packit Service |
9def5d |
const ssize_t len = ReadFromOffset(fd, buf, num_bytes_to_read,
|
|
Packit Service |
9def5d |
sh_offset + i * sizeof(buf[0]));
|
|
Packit Service |
9def5d |
SAFE_ASSERT(len % sizeof(buf[0]) == 0);
|
|
Packit Service |
9def5d |
const ssize_t num_headers_in_buf = len / sizeof(buf[0]);
|
|
Packit Service |
9def5d |
SAFE_ASSERT(num_headers_in_buf <= sizeof(buf) / sizeof(buf[0]));
|
|
Packit Service |
9def5d |
for (int j = 0; j < num_headers_in_buf; ++j) {
|
|
Packit Service |
9def5d |
if (buf[j].sh_type == type) {
|
|
Packit Service |
9def5d |
*out = buf[j];
|
|
Packit Service |
9def5d |
return true;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
i += num_headers_in_buf;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// There is no particular reason to limit section name to 63 characters,
|
|
Packit Service |
9def5d |
// but there has (as yet) been no need for anything longer either.
|
|
Packit Service |
9def5d |
const int kMaxSectionNameLen = 64;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// name_len should include terminating '\0'.
|
|
Packit Service |
9def5d |
bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
|
|
Packit Service |
9def5d |
ElfW(Shdr) *out) {
|
|
Packit Service |
9def5d |
ElfW(Ehdr) elf_header;
|
|
Packit Service |
9def5d |
if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
ElfW(Shdr) shstrtab;
|
|
Packit Service |
9def5d |
off_t shstrtab_offset = (elf_header.e_shoff +
|
|
Packit Service |
9def5d |
elf_header.e_shentsize * elf_header.e_shstrndx);
|
|
Packit Service |
9def5d |
if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
for (int i = 0; i < elf_header.e_shnum; ++i) {
|
|
Packit Service |
9def5d |
off_t section_header_offset = (elf_header.e_shoff +
|
|
Packit Service |
9def5d |
elf_header.e_shentsize * i);
|
|
Packit Service |
9def5d |
if (!ReadFromOffsetExact(fd, out, sizeof(*out), section_header_offset)) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
char header_name[kMaxSectionNameLen];
|
|
Packit Service |
9def5d |
if (sizeof(header_name) < name_len) {
|
|
Packit Service |
9def5d |
RAW_LOG(WARNING, "Section name '%s' is too long (%" PRIuS "); "
|
|
Packit Service |
9def5d |
"section will not be found (even if present).", name, name_len);
|
|
Packit Service |
9def5d |
// No point in even trying.
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
off_t name_offset = shstrtab.sh_offset + out->sh_name;
|
|
Packit Service |
9def5d |
ssize_t n_read = ReadFromOffset(fd, &header_name, name_len, name_offset);
|
|
Packit Service |
9def5d |
if (n_read == -1) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
} else if (n_read != name_len) {
|
|
Packit Service |
9def5d |
// Short read -- name could be at end of file.
|
|
Packit Service |
9def5d |
continue;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
if (memcmp(header_name, name, name_len) == 0) {
|
|
Packit Service |
9def5d |
return true;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Read a symbol table and look for the symbol containing the
|
|
Packit Service |
9def5d |
// pc. Iterate over symbols in a symbol table and look for the symbol
|
|
Packit Service |
9def5d |
// containing "pc". On success, return true and write the symbol name
|
|
Packit Service |
9def5d |
// to out. Otherwise, return false.
|
|
Packit Service |
9def5d |
// To keep stack consumption low, we would like this function to not get
|
|
Packit Service |
9def5d |
// inlined.
|
|
Packit Service |
9def5d |
static ATTRIBUTE_NOINLINE bool
|
|
Packit Service |
9def5d |
FindSymbol(uint64_t pc, const int fd, char *out, int out_size,
|
|
Packit Service |
9def5d |
uint64_t symbol_offset, const ElfW(Shdr) *strtab,
|
|
Packit Service |
9def5d |
const ElfW(Shdr) *symtab) {
|
|
Packit Service |
9def5d |
if (symtab == NULL) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
const int num_symbols = symtab->sh_size / symtab->sh_entsize;
|
|
Packit Service |
9def5d |
for (int i = 0; i < num_symbols;) {
|
|
Packit Service |
9def5d |
off_t offset = symtab->sh_offset + i * symtab->sh_entsize;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// If we are reading Elf64_Sym's, we want to limit this array to
|
|
Packit Service |
9def5d |
// 32 elements (to keep stack consumption low), otherwise we can
|
|
Packit Service |
9def5d |
// have a 64 element Elf32_Sym array.
|
|
Packit Service |
9def5d |
#if __WORDSIZE == 64
|
|
Packit Service |
9def5d |
#define NUM_SYMBOLS 32
|
|
Packit Service |
9def5d |
#else
|
|
Packit Service |
9def5d |
#define NUM_SYMBOLS 64
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Read at most NUM_SYMBOLS symbols at once to save read() calls.
|
|
Packit Service |
9def5d |
ElfW(Sym) buf[NUM_SYMBOLS];
|
|
Packit Service |
9def5d |
const ssize_t len = ReadFromOffset(fd, &buf, sizeof(buf), offset);
|
|
Packit Service |
9def5d |
SAFE_ASSERT(len % sizeof(buf[0]) == 0);
|
|
Packit Service |
9def5d |
const ssize_t num_symbols_in_buf = len / sizeof(buf[0]);
|
|
Packit Service |
9def5d |
SAFE_ASSERT(num_symbols_in_buf <= sizeof(buf)/sizeof(buf[0]));
|
|
Packit Service |
9def5d |
for (int j = 0; j < num_symbols_in_buf; ++j) {
|
|
Packit Service |
9def5d |
const ElfW(Sym)& symbol = buf[j];
|
|
Packit Service |
9def5d |
uint64_t start_address = symbol.st_value;
|
|
Packit Service |
9def5d |
start_address += symbol_offset;
|
|
Packit Service |
9def5d |
uint64_t end_address = start_address + symbol.st_size;
|
|
Packit Service |
9def5d |
if (symbol.st_value != 0 && // Skip null value symbols.
|
|
Packit Service |
9def5d |
symbol.st_shndx != 0 && // Skip undefined symbols.
|
|
Packit Service |
9def5d |
start_address <= pc && pc < end_address) {
|
|
Packit Service |
9def5d |
ssize_t len1 = ReadFromOffset(fd, out, out_size,
|
|
Packit Service |
9def5d |
strtab->sh_offset + symbol.st_name);
|
|
Packit Service |
9def5d |
if (len1 <= 0 || memchr(out, '\0', out_size) == NULL) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
return true; // Obtained the symbol name.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
i += num_symbols_in_buf;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Get the symbol name of "pc" from the file pointed by "fd". Process
|
|
Packit Service |
9def5d |
// both regular and dynamic symbol tables if necessary. On success,
|
|
Packit Service |
9def5d |
// write the symbol name to "out" and return true. Otherwise, return
|
|
Packit Service |
9def5d |
// false.
|
|
Packit Service |
9def5d |
static bool GetSymbolFromObjectFile(const int fd, uint64_t pc,
|
|
Packit Service |
9def5d |
char *out, int out_size,
|
|
Packit Service |
9def5d |
uint64_t map_base_address) {
|
|
Packit Service |
9def5d |
// Read the ELF header.
|
|
Packit Service |
9def5d |
ElfW(Ehdr) elf_header;
|
|
Packit Service |
9def5d |
if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
uint64_t symbol_offset = 0;
|
|
Packit Service |
9def5d |
if (elf_header.e_type == ET_DYN) { // DSO needs offset adjustment.
|
|
Packit Service |
9def5d |
ElfW(Phdr) phdr;
|
|
Packit Service |
9def5d |
// We need to find the PT_LOAD segment corresponding to the read-execute
|
|
Packit Service |
9def5d |
// file mapping in order to correctly perform the offset adjustment.
|
|
Packit Service |
9def5d |
for (unsigned i = 0; i != elf_header.e_phnum; ++i) {
|
|
Packit Service |
9def5d |
if (!ReadFromOffsetExact(fd, &phdr, sizeof(phdr),
|
|
Packit Service |
9def5d |
elf_header.e_phoff + i * sizeof(phdr)))
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
if (phdr.p_type == PT_LOAD &&
|
|
Packit Service |
9def5d |
(phdr.p_flags & (PF_R | PF_X)) == (PF_R | PF_X)) {
|
|
Packit Service |
9def5d |
// Find the mapped address corresponding to virtual address zero. We do
|
|
Packit Service |
9def5d |
// this by first adding p_offset. This gives us the mapped address of
|
|
Packit Service |
9def5d |
// the start of the segment, or in other words the mapped address
|
|
Packit Service |
9def5d |
// corresponding to the virtual address of the segment. (Note that this
|
|
Packit Service |
9def5d |
// is distinct from the start address, as p_offset is not guaranteed to
|
|
Packit Service |
9def5d |
// be page aligned.) We then subtract p_vaddr, which takes us to virtual
|
|
Packit Service |
9def5d |
// address zero.
|
|
Packit Service |
9def5d |
symbol_offset = map_base_address + phdr.p_offset - phdr.p_vaddr;
|
|
Packit Service |
9def5d |
break;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
if (symbol_offset == 0)
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
ElfW(Shdr) symtab, strtab;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Consult a regular symbol table first.
|
|
Packit Service |
9def5d |
if (GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
|
|
Packit Service |
9def5d |
SHT_SYMTAB, &symtab)) {
|
|
Packit Service |
9def5d |
if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
|
|
Packit Service |
9def5d |
symtab.sh_link * sizeof(symtab))) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
if (FindSymbol(pc, fd, out, out_size, symbol_offset,
|
|
Packit Service |
9def5d |
&strtab, &symtab)) {
|
|
Packit Service |
9def5d |
return true; // Found the symbol in a regular symbol table.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// If the symbol is not found, then consult a dynamic symbol table.
|
|
Packit Service |
9def5d |
if (GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
|
|
Packit Service |
9def5d |
SHT_DYNSYM, &symtab)) {
|
|
Packit Service |
9def5d |
if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
|
|
Packit Service |
9def5d |
symtab.sh_link * sizeof(symtab))) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
if (FindSymbol(pc, fd, out, out_size, symbol_offset,
|
|
Packit Service |
9def5d |
&strtab, &symtab)) {
|
|
Packit Service |
9def5d |
return true; // Found the symbol in a dynamic symbol table.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
namespace {
|
|
Packit Service |
9def5d |
// Thin wrapper around a file descriptor so that the file descriptor
|
|
Packit Service |
9def5d |
// gets closed for sure.
|
|
Packit Service |
9def5d |
struct FileDescriptor {
|
|
Packit Service |
9def5d |
const int fd_;
|
|
Packit Service |
9def5d |
explicit FileDescriptor(int fd) : fd_(fd) {}
|
|
Packit Service |
9def5d |
~FileDescriptor() {
|
|
Packit Service |
9def5d |
if (fd_ >= 0) {
|
|
Packit Service |
9def5d |
NO_INTR(close(fd_));
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
int get() { return fd_; }
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
private:
|
|
Packit Service |
9def5d |
explicit FileDescriptor(const FileDescriptor&);
|
|
Packit Service |
9def5d |
void operator=(const FileDescriptor&);
|
|
Packit Service |
9def5d |
};
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Helper class for reading lines from file.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Note: we don't use ProcMapsIterator since the object is big (it has
|
|
Packit Service |
9def5d |
// a 5k array member) and uses async-unsafe functions such as sscanf()
|
|
Packit Service |
9def5d |
// and snprintf().
|
|
Packit Service |
9def5d |
class LineReader {
|
|
Packit Service |
9def5d |
public:
|
|
Packit Service |
9def5d |
explicit LineReader(int fd, char *buf, int buf_len) : fd_(fd),
|
|
Packit Service |
9def5d |
buf_(buf), buf_len_(buf_len), bol_(buf), eol_(buf), eod_(buf) {
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Read '\n'-terminated line from file. On success, modify "bol"
|
|
Packit Service |
9def5d |
// and "eol", then return true. Otherwise, return false.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Note: if the last line doesn't end with '\n', the line will be
|
|
Packit Service |
9def5d |
// dropped. It's an intentional behavior to make the code simple.
|
|
Packit Service |
9def5d |
bool ReadLine(const char **bol, const char **eol) {
|
|
Packit Service |
9def5d |
if (BufferIsEmpty()) { // First time.
|
|
Packit Service |
9def5d |
const ssize_t num_bytes = ReadPersistent(fd_, buf_, buf_len_);
|
|
Packit Service |
9def5d |
if (num_bytes <= 0) { // EOF or error.
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
eod_ = buf_ + num_bytes;
|
|
Packit Service |
9def5d |
bol_ = buf_;
|
|
Packit Service |
9def5d |
} else {
|
|
Packit Service |
9def5d |
bol_ = eol_ + 1; // Advance to the next line in the buffer.
|
|
Packit Service |
9def5d |
SAFE_ASSERT(bol_ <= eod_); // "bol_" can point to "eod_".
|
|
Packit Service |
9def5d |
if (!HasCompleteLine()) {
|
|
Packit Service |
9def5d |
const int incomplete_line_length = eod_ - bol_;
|
|
Packit Service |
9def5d |
// Move the trailing incomplete line to the beginning.
|
|
Packit Service |
9def5d |
memmove(buf_, bol_, incomplete_line_length);
|
|
Packit Service |
9def5d |
// Read text from file and append it.
|
|
Packit Service |
9def5d |
char * const append_pos = buf_ + incomplete_line_length;
|
|
Packit Service |
9def5d |
const int capacity_left = buf_len_ - incomplete_line_length;
|
|
Packit Service |
9def5d |
const ssize_t num_bytes = ReadPersistent(fd_, append_pos,
|
|
Packit Service |
9def5d |
capacity_left);
|
|
Packit Service |
9def5d |
if (num_bytes <= 0) { // EOF or error.
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
eod_ = append_pos + num_bytes;
|
|
Packit Service |
9def5d |
bol_ = buf_;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
eol_ = FindLineFeed();
|
|
Packit Service |
9def5d |
if (eol_ == NULL) { // '\n' not found. Malformed line.
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
*eol_ = '\0'; // Replace '\n' with '\0'.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
*bol = bol_;
|
|
Packit Service |
9def5d |
*eol = eol_;
|
|
Packit Service |
9def5d |
return true;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Beginning of line.
|
|
Packit Service |
9def5d |
const char *bol() {
|
|
Packit Service |
9def5d |
return bol_;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// End of line.
|
|
Packit Service |
9def5d |
const char *eol() {
|
|
Packit Service |
9def5d |
return eol_;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
private:
|
|
Packit Service |
9def5d |
explicit LineReader(const LineReader&);
|
|
Packit Service |
9def5d |
void operator=(const LineReader&);
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
char *FindLineFeed() {
|
|
Packit Service |
9def5d |
return reinterpret_cast<char *>(memchr(bol_, '\n', eod_ - bol_));
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
bool BufferIsEmpty() {
|
|
Packit Service |
9def5d |
return buf_ == eod_;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
bool HasCompleteLine() {
|
|
Packit Service |
9def5d |
return !BufferIsEmpty() && FindLineFeed() != NULL;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
const int fd_;
|
|
Packit Service |
9def5d |
char * const buf_;
|
|
Packit Service |
9def5d |
const int buf_len_;
|
|
Packit Service |
9def5d |
char *bol_;
|
|
Packit Service |
9def5d |
char *eol_;
|
|
Packit Service |
9def5d |
const char *eod_; // End of data in "buf_".
|
|
Packit Service |
9def5d |
};
|
|
Packit Service |
9def5d |
} // namespace
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Place the hex number read from "start" into "*hex". The pointer to
|
|
Packit Service |
9def5d |
// the first non-hex character or "end" is returned.
|
|
Packit Service |
9def5d |
static char *GetHex(const char *start, const char *end, uint64_t *hex) {
|
|
Packit Service |
9def5d |
*hex = 0;
|
|
Packit Service |
9def5d |
const char *p;
|
|
Packit Service |
9def5d |
for (p = start; p < end; ++p) {
|
|
Packit Service |
9def5d |
int ch = *p;
|
|
Packit Service |
9def5d |
if ((ch >= '0' && ch <= '9') ||
|
|
Packit Service |
9def5d |
(ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) {
|
|
Packit Service |
9def5d |
*hex = (*hex << 4) | (ch < 'A' ? ch - '0' : (ch & 0xF) + 9);
|
|
Packit Service |
9def5d |
} else { // Encountered the first non-hex character.
|
|
Packit Service |
9def5d |
break;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
SAFE_ASSERT(p <= end);
|
|
Packit Service |
9def5d |
return const_cast<char *>(p);
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Searches for the object file (from /proc/self/maps) that contains
|
|
Packit Service |
9def5d |
// the specified pc. If found, sets |start_address| to the start address
|
|
Packit Service |
9def5d |
// of where this object file is mapped in memory, sets the module base
|
|
Packit Service |
9def5d |
// address into |base_address|, copies the object file name into
|
|
Packit Service |
9def5d |
// |out_file_name|, and attempts to open the object file. If the object
|
|
Packit Service |
9def5d |
// file is opened successfully, returns the file descriptor. Otherwise,
|
|
Packit Service |
9def5d |
// returns -1. |out_file_name_size| is the size of the file name buffer
|
|
Packit Service |
9def5d |
// (including the null-terminator).
|
|
Packit Service |
9def5d |
static ATTRIBUTE_NOINLINE int
|
|
Packit Service |
9def5d |
OpenObjectFileContainingPcAndGetStartAddress(uint64_t pc,
|
|
Packit Service |
9def5d |
uint64_t &start_address,
|
|
Packit Service |
9def5d |
uint64_t &base_address,
|
|
Packit Service |
9def5d |
char *out_file_name,
|
|
Packit Service |
9def5d |
int out_file_name_size) {
|
|
Packit Service |
9def5d |
int object_fd;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Open /proc/self/maps.
|
|
Packit Service |
9def5d |
int maps_fd;
|
|
Packit Service |
9def5d |
NO_INTR(maps_fd = open("/proc/self/maps", O_RDONLY));
|
|
Packit Service |
9def5d |
FileDescriptor wrapped_maps_fd(maps_fd);
|
|
Packit Service |
9def5d |
if (wrapped_maps_fd.get() < 0) {
|
|
Packit Service |
9def5d |
return -1;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Iterate over maps and look for the map containing the pc. Then
|
|
Packit Service |
9def5d |
// look into the symbol tables inside.
|
|
Packit Service |
9def5d |
char buf[1024]; // Big enough for line of sane /proc/self/maps
|
|
Packit Service |
9def5d |
int num_maps = 0;
|
|
Packit Service |
9def5d |
LineReader reader(wrapped_maps_fd.get(), buf, sizeof(buf));
|
|
Packit Service |
9def5d |
while (true) {
|
|
Packit Service |
9def5d |
num_maps++;
|
|
Packit Service |
9def5d |
const char *cursor;
|
|
Packit Service |
9def5d |
const char *eol;
|
|
Packit Service |
9def5d |
if (!reader.ReadLine(&cursor, &eol)) { // EOF or malformed line.
|
|
Packit Service |
9def5d |
return -1;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Start parsing line in /proc/self/maps. Here is an example:
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// 08048000-0804c000 r-xp 00000000 08:01 2142121 /bin/cat
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// We want start address (08048000), end address (0804c000), flags
|
|
Packit Service |
9def5d |
// (r-xp) and file name (/bin/cat).
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Read start address.
|
|
Packit Service |
9def5d |
cursor = GetHex(cursor, eol, &start_address);
|
|
Packit Service |
9def5d |
if (cursor == eol || *cursor != '-') {
|
|
Packit Service |
9def5d |
return -1; // Malformed line.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
++cursor; // Skip '-'.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Read end address.
|
|
Packit Service |
9def5d |
uint64_t end_address;
|
|
Packit Service |
9def5d |
cursor = GetHex(cursor, eol, &end_address);
|
|
Packit Service |
9def5d |
if (cursor == eol || *cursor != ' ') {
|
|
Packit Service |
9def5d |
return -1; // Malformed line.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
++cursor; // Skip ' '.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Check start and end addresses.
|
|
Packit Service |
9def5d |
if (!(start_address <= pc && pc < end_address)) {
|
|
Packit Service |
9def5d |
continue; // We skip this map. PC isn't in this map.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Read flags. Skip flags until we encounter a space or eol.
|
|
Packit Service |
9def5d |
const char * const flags_start = cursor;
|
|
Packit Service |
9def5d |
while (cursor < eol && *cursor != ' ') {
|
|
Packit Service |
9def5d |
++cursor;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
// We expect at least four letters for flags (ex. "r-xp").
|
|
Packit Service |
9def5d |
if (cursor == eol || cursor < flags_start + 4) {
|
|
Packit Service |
9def5d |
return -1; // Malformed line.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Check flags. We are only interested in "r*x" maps.
|
|
Packit Service |
9def5d |
if (flags_start[0] != 'r' || flags_start[2] != 'x') {
|
|
Packit Service |
9def5d |
continue; // We skip this map.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
++cursor; // Skip ' '.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Read file offset.
|
|
Packit Service |
9def5d |
uint64_t file_offset;
|
|
Packit Service |
9def5d |
cursor = GetHex(cursor, eol, &file_offset);
|
|
Packit Service |
9def5d |
if (cursor == eol || *cursor != ' ') {
|
|
Packit Service |
9def5d |
return -1; // Malformed line.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
++cursor; // Skip ' '.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Don't subtract 'start_address' from the first entry:
|
|
Packit Service |
9def5d |
// * If a binary is compiled w/o -pie, then the first entry in
|
|
Packit Service |
9def5d |
// process maps is likely the binary itself (all dynamic libs
|
|
Packit Service |
9def5d |
// are mapped higher in address space). For such a binary,
|
|
Packit Service |
9def5d |
// instruction offset in binary coincides with the actual
|
|
Packit Service |
9def5d |
// instruction address in virtual memory (as code section
|
|
Packit Service |
9def5d |
// is mapped to a fixed memory range).
|
|
Packit Service |
9def5d |
// * If a binary is compiled with -pie, all the modules are
|
|
Packit Service |
9def5d |
// mapped high at address space (in particular, higher than
|
|
Packit Service |
9def5d |
// shadow memory of the tool), so the module can't be the
|
|
Packit Service |
9def5d |
// first entry.
|
|
Packit Service |
9def5d |
base_address = ((num_maps == 1) ? 0U : start_address) - file_offset;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Skip to file name. "cursor" now points to dev. We need to
|
|
Packit Service |
9def5d |
// skip at least two spaces for dev and inode.
|
|
Packit Service |
9def5d |
int num_spaces = 0;
|
|
Packit Service |
9def5d |
while (cursor < eol) {
|
|
Packit Service |
9def5d |
if (*cursor == ' ') {
|
|
Packit Service |
9def5d |
++num_spaces;
|
|
Packit Service |
9def5d |
} else if (num_spaces >= 2) {
|
|
Packit Service |
9def5d |
// The first non-space character after skipping two spaces
|
|
Packit Service |
9def5d |
// is the beginning of the file name.
|
|
Packit Service |
9def5d |
break;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
++cursor;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
if (cursor == eol) {
|
|
Packit Service |
9def5d |
return -1; // Malformed line.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Finally, "cursor" now points to file name of our interest.
|
|
Packit Service |
9def5d |
NO_INTR(object_fd = open(cursor, O_RDONLY));
|
|
Packit Service |
9def5d |
if (object_fd < 0) {
|
|
Packit Service |
9def5d |
// Failed to open object file. Copy the object file name to
|
|
Packit Service |
9def5d |
// |out_file_name|.
|
|
Packit Service |
9def5d |
strncpy(out_file_name, cursor, out_file_name_size);
|
|
Packit Service |
9def5d |
// Making sure |out_file_name| is always null-terminated.
|
|
Packit Service |
9def5d |
out_file_name[out_file_name_size - 1] = '\0';
|
|
Packit Service |
9def5d |
return -1;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
return object_fd;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// POSIX doesn't define any async-signal safe function for converting
|
|
Packit Service |
9def5d |
// an integer to ASCII. We'll have to define our own version.
|
|
Packit Service |
9def5d |
// itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the
|
|
Packit Service |
9def5d |
// conversion was successful or NULL otherwise. It never writes more than "sz"
|
|
Packit Service |
9def5d |
// bytes. Output will be truncated as needed, and a NUL character is always
|
|
Packit Service |
9def5d |
// appended.
|
|
Packit Service |
9def5d |
// NOTE: code from sandbox/linux/seccomp-bpf/demo.cc.
|
|
Packit Service |
9def5d |
char *itoa_r(intptr_t i, char *buf, size_t sz, int base, size_t padding) {
|
|
Packit Service |
9def5d |
// Make sure we can write at least one NUL byte.
|
|
Packit Service |
9def5d |
size_t n = 1;
|
|
Packit Service |
9def5d |
if (n > sz)
|
|
Packit Service |
9def5d |
return NULL;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
if (base < 2 || base > 16) {
|
|
Packit Service |
9def5d |
buf[0] = '\000';
|
|
Packit Service |
9def5d |
return NULL;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
char *start = buf;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
uintptr_t j = i;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Handle negative numbers (only for base 10).
|
|
Packit Service |
9def5d |
if (i < 0 && base == 10) {
|
|
Packit Service |
9def5d |
j = -i;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Make sure we can write the '-' character.
|
|
Packit Service |
9def5d |
if (++n > sz) {
|
|
Packit Service |
9def5d |
buf[0] = '\000';
|
|
Packit Service |
9def5d |
return NULL;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
*start++ = '-';
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Loop until we have converted the entire number. Output at least one
|
|
Packit Service |
9def5d |
// character (i.e. '0').
|
|
Packit Service |
9def5d |
char *ptr = start;
|
|
Packit Service |
9def5d |
do {
|
|
Packit Service |
9def5d |
// Make sure there is still enough space left in our output buffer.
|
|
Packit Service |
9def5d |
if (++n > sz) {
|
|
Packit Service |
9def5d |
buf[0] = '\000';
|
|
Packit Service |
9def5d |
return NULL;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Output the next digit.
|
|
Packit Service |
9def5d |
*ptr++ = "0123456789abcdef"[j % base];
|
|
Packit Service |
9def5d |
j /= base;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
if (padding > 0)
|
|
Packit Service |
9def5d |
padding--;
|
|
Packit Service |
9def5d |
} while (j > 0 || padding > 0);
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Terminate the output with a NUL character.
|
|
Packit Service |
9def5d |
*ptr = '\000';
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Conversion to ASCII actually resulted in the digits being in reverse
|
|
Packit Service |
9def5d |
// order. We can't easily generate them in forward order, as we can't tell
|
|
Packit Service |
9def5d |
// the number of characters needed until we are done converting.
|
|
Packit Service |
9def5d |
// So, now, we reverse the string (except for the possible "-" sign).
|
|
Packit Service |
9def5d |
while (--ptr > start) {
|
|
Packit Service |
9def5d |
char ch = *ptr;
|
|
Packit Service |
9def5d |
*ptr = *start;
|
|
Packit Service |
9def5d |
*start++ = ch;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
return buf;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Safely appends string |source| to string |dest|. Never writes past the
|
|
Packit Service |
9def5d |
// buffer size |dest_size| and guarantees that |dest| is null-terminated.
|
|
Packit Service |
9def5d |
void SafeAppendString(const char* source, char* dest, int dest_size) {
|
|
Packit Service |
9def5d |
int dest_string_length = strlen(dest);
|
|
Packit Service |
9def5d |
SAFE_ASSERT(dest_string_length < dest_size);
|
|
Packit Service |
9def5d |
dest += dest_string_length;
|
|
Packit Service |
9def5d |
dest_size -= dest_string_length;
|
|
Packit Service |
9def5d |
strncpy(dest, source, dest_size);
|
|
Packit Service |
9def5d |
// Making sure |dest| is always null-terminated.
|
|
Packit Service |
9def5d |
dest[dest_size - 1] = '\0';
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Converts a 64-bit value into a hex string, and safely appends it to |dest|.
|
|
Packit Service |
9def5d |
// Never writes past the buffer size |dest_size| and guarantees that |dest| is
|
|
Packit Service |
9def5d |
// null-terminated.
|
|
Packit Service |
9def5d |
void SafeAppendHexNumber(uint64_t value, char* dest, int dest_size) {
|
|
Packit Service |
9def5d |
// 64-bit numbers in hex can have up to 16 digits.
|
|
Packit Service |
9def5d |
char buf[17] = {'\0'};
|
|
Packit Service |
9def5d |
SafeAppendString(itoa_r(value, buf, sizeof(buf), 16, 0), dest, dest_size);
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// The implementation of our symbolization routine. If it
|
|
Packit Service |
9def5d |
// successfully finds the symbol containing "pc" and obtains the
|
|
Packit Service |
9def5d |
// symbol name, returns true and write the symbol name to "out".
|
|
Packit Service |
9def5d |
// Otherwise, returns false. If Callback function is installed via
|
|
Packit Service |
9def5d |
// InstallSymbolizeCallback(), the function is also called in this function,
|
|
Packit Service |
9def5d |
// and "out" is used as its output.
|
|
Packit Service |
9def5d |
// To keep stack consumption low, we would like this function to not
|
|
Packit Service |
9def5d |
// get inlined.
|
|
Packit Service |
9def5d |
static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
|
|
Packit Service |
9def5d |
int out_size) {
|
|
Packit Service |
9def5d |
uint64_t pc0 = reinterpret_cast<uintptr_t>(pc);
|
|
Packit Service |
9def5d |
uint64_t start_address = 0;
|
|
Packit Service |
9def5d |
uint64_t base_address = 0;
|
|
Packit Service |
9def5d |
int object_fd = -1;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
if (out_size < 1) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
out[0] = '\0';
|
|
Packit Service |
9def5d |
SafeAppendString("(", out, out_size);
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
if (g_symbolize_open_object_file_callback) {
|
|
Packit Service |
9def5d |
object_fd = g_symbolize_open_object_file_callback(pc0, start_address,
|
|
Packit Service |
9def5d |
base_address, out + 1,
|
|
Packit Service |
9def5d |
out_size - 1);
|
|
Packit Service |
9def5d |
} else {
|
|
Packit Service |
9def5d |
object_fd = OpenObjectFileContainingPcAndGetStartAddress(pc0, start_address,
|
|
Packit Service |
9def5d |
base_address,
|
|
Packit Service |
9def5d |
out + 1,
|
|
Packit Service |
9def5d |
out_size - 1);
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Check whether a file name was returned.
|
|
Packit Service |
9def5d |
if (object_fd < 0) {
|
|
Packit Service |
9def5d |
if (out[1]) {
|
|
Packit Service |
9def5d |
// The object file containing PC was determined successfully however the
|
|
Packit Service |
9def5d |
// object file was not opened successfully. This is still considered
|
|
Packit Service |
9def5d |
// success because the object file name and offset are known and tools
|
|
Packit Service |
9def5d |
// like asan_symbolize.py can be used for the symbolization.
|
|
Packit Service |
9def5d |
out[out_size - 1] = '\0'; // Making sure |out| is always null-terminated.
|
|
Packit Service |
9def5d |
SafeAppendString("+0x", out, out_size);
|
|
Packit Service |
9def5d |
SafeAppendHexNumber(pc0 - base_address, out, out_size);
|
|
Packit Service |
9def5d |
SafeAppendString(")", out, out_size);
|
|
Packit Service |
9def5d |
return true;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
// Failed to determine the object file containing PC. Bail out.
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
FileDescriptor wrapped_object_fd(object_fd);
|
|
Packit Service |
9def5d |
int elf_type = FileGetElfType(wrapped_object_fd.get());
|
|
Packit Service |
9def5d |
if (elf_type == -1) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
if (g_symbolize_callback) {
|
|
Packit Service |
9def5d |
// Run the call back if it's installed.
|
|
Packit Service |
9def5d |
// Note: relocation (and much of the rest of this code) will be
|
|
Packit Service |
9def5d |
// wrong for prelinked shared libraries and PIE executables.
|
|
Packit Service |
9def5d |
uint64 relocation = (elf_type == ET_DYN) ? start_address : 0;
|
|
Packit Service |
9def5d |
int num_bytes_written = g_symbolize_callback(wrapped_object_fd.get(),
|
|
Packit Service |
9def5d |
pc, out, out_size,
|
|
Packit Service |
9def5d |
relocation);
|
|
Packit Service |
9def5d |
if (num_bytes_written > 0) {
|
|
Packit Service |
9def5d |
out += num_bytes_written;
|
|
Packit Service |
9def5d |
out_size -= num_bytes_written;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
if (!GetSymbolFromObjectFile(wrapped_object_fd.get(), pc0,
|
|
Packit Service |
9def5d |
out, out_size, base_address)) {
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Symbolization succeeded. Now we try to demangle the symbol.
|
|
Packit Service |
9def5d |
DemangleInplace(out, out_size);
|
|
Packit Service |
9def5d |
return true;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_END_GOOGLE_NAMESPACE_
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#elif defined(OS_MACOSX) && defined(HAVE_DLADDR)
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include <dlfcn.h>
|
|
Packit Service |
9def5d |
#include <string.h>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_START_GOOGLE_NAMESPACE_
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
|
|
Packit Service |
9def5d |
int out_size) {
|
|
Packit Service |
9def5d |
Dl_info info;
|
|
Packit Service |
9def5d |
if (dladdr(pc, &info)) {
|
|
Packit Service |
9def5d |
if ((int)strlen(info.dli_sname) < out_size) {
|
|
Packit Service |
9def5d |
strcpy(out, info.dli_sname);
|
|
Packit Service |
9def5d |
// Symbolization succeeded. Now we try to demangle the symbol.
|
|
Packit Service |
9def5d |
DemangleInplace(out, out_size);
|
|
Packit Service |
9def5d |
return true;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_END_GOOGLE_NAMESPACE_
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#else
|
|
Packit Service |
9def5d |
# error BUG: HAVE_SYMBOLIZE was wrongly set
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_START_GOOGLE_NAMESPACE_
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
bool Symbolize(void *pc, char *out, int out_size) {
|
|
Packit Service |
9def5d |
SAFE_ASSERT(out_size >= 0);
|
|
Packit Service |
9def5d |
return SymbolizeAndDemangle(pc, out, out_size);
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_END_GOOGLE_NAMESPACE_
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#else /* HAVE_SYMBOLIZE */
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include <assert.h>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include "config.h"
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_START_GOOGLE_NAMESPACE_
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// TODO: Support other environments.
|
|
Packit Service |
9def5d |
bool Symbolize(void *pc, char *out, int out_size) {
|
|
Packit Service |
9def5d |
assert(0);
|
|
Packit Service |
9def5d |
return false;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_END_GOOGLE_NAMESPACE_
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#endif
|