Blame src/symbolize.h

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
//
Packit Service 9def5d
// This library provides Symbolize() function that symbolizes program
Packit Service 9def5d
// counters to their corresponding symbol names on linux platforms.
Packit Service 9def5d
// This library has a minimal implementation of an ELF symbol table
Packit Service 9def5d
// reader (i.e. it doesn't depend on libelf, etc.).
Packit Service 9def5d
//
Packit Service 9def5d
// The algorithm used in Symbolize() is as follows.
Packit Service 9def5d
//
Packit Service 9def5d
//   1. Go through a list of maps in /proc/self/maps and find the map
Packit Service 9def5d
//   containing the program counter.
Packit Service 9def5d
//
Packit Service 9def5d
//   2. Open the mapped file and find a regular symbol table inside.
Packit Service 9def5d
//   Iterate over symbols in the symbol table and look for the symbol
Packit Service 9def5d
//   containing the program counter.  If such a symbol is found,
Packit Service 9def5d
//   obtain the symbol name, and demangle the symbol if possible.
Packit Service 9def5d
//   If the symbol isn't found in the regular symbol table (binary is
Packit Service 9def5d
//   stripped), try the same thing with a dynamic symbol table.
Packit Service 9def5d
//
Packit Service 9def5d
// Note that Symbolize() is originally implemented to be used in
Packit Service 9def5d
// FailureSignalHandler() in base/google.cc.  Hence it doesn't use
Packit Service 9def5d
// malloc() and other unsafe operations.  It should be both
Packit Service 9def5d
// thread-safe and async-signal-safe.
Packit Service 9def5d
Packit Service 9def5d
#ifndef BASE_SYMBOLIZE_H_
Packit Service 9def5d
#define BASE_SYMBOLIZE_H_
Packit Service 9def5d
Packit Service 9def5d
#include "utilities.h"
Packit Service 9def5d
#include "config.h"
Packit Service 9def5d
#include "glog/logging.h"
Packit Service 9def5d
Packit Service 9def5d
#ifdef HAVE_SYMBOLIZE
Packit Service 9def5d
Packit Service 9def5d
#if defined(__ELF__)  // defined by gcc
Packit Service 9def5d
#if defined(__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
Packit Service 9def5d
#if !defined(ANDROID)
Packit Service 9def5d
#include <link.h>  // For ElfW() macro.
Packit Service 9def5d
#endif
Packit Service 9def5d
Packit Service 9def5d
// For systems where SIZEOF_VOID_P is not defined, determine it
Packit Service 9def5d
// based on __LP64__ (defined by gcc on 64-bit systems)
Packit Service 9def5d
#if !defined(SIZEOF_VOID_P)
Packit Service 9def5d
# if defined(__LP64__)
Packit Service 9def5d
#  define SIZEOF_VOID_P 8
Packit Service 9def5d
# else
Packit Service 9def5d
#  define SIZEOF_VOID_P 4
Packit Service 9def5d
# endif
Packit Service 9def5d
#endif
Packit Service 9def5d
Packit Service 9def5d
// If there is no ElfW macro, let's define it by ourself.
Packit Service 9def5d
#ifndef ElfW
Packit Service 9def5d
# if SIZEOF_VOID_P == 4
Packit Service 9def5d
#  define ElfW(type) Elf32_##type
Packit Service 9def5d
# elif SIZEOF_VOID_P == 8
Packit Service 9def5d
#  define ElfW(type) Elf64_##type
Packit Service 9def5d
# else
Packit Service 9def5d
#  error "Unknown sizeof(void *)"
Packit Service 9def5d
# endif
Packit Service 9def5d
#endif
Packit Service 9def5d
Packit Service 9def5d
_START_GOOGLE_NAMESPACE_
Packit Service 9def5d
Packit Service 9def5d
// Gets the section header for the given name, if it exists. Returns true on
Packit Service 9def5d
// success. Otherwise, returns false.
Packit Service 9def5d
bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
Packit Service 9def5d
                            ElfW(Shdr) *out);
Packit Service 9def5d
Packit Service 9def5d
_END_GOOGLE_NAMESPACE_
Packit Service 9def5d
Packit Service 9def5d
#endif  /* __ELF__ */
Packit Service 9def5d
Packit Service 9def5d
_START_GOOGLE_NAMESPACE_
Packit Service 9def5d
Packit Service 9def5d
// Restrictions on the callbacks that follow:
Packit Service 9def5d
//  - The callbacks must not use heaps but only use stacks.
Packit Service 9def5d
//  - The callbacks must be async-signal-safe.
Packit Service 9def5d
Packit Service 9def5d
// Installs a callback function, which will be called right before a symbol name
Packit Service 9def5d
// is printed. The callback is intended to be used for showing a file name and a
Packit Service 9def5d
// line number preceding a symbol name.
Packit Service 9def5d
// "fd" is a file descriptor of the object file containing the program
Packit Service 9def5d
// counter "pc". The callback function should write output to "out"
Packit Service 9def5d
// and return the size of the output written. On error, the callback
Packit Service 9def5d
// function should return -1.
Packit Service 9def5d
typedef int (*SymbolizeCallback)(int fd, void *pc, char *out, size_t out_size,
Packit Service 9def5d
                                 uint64 relocation);
Packit Service 9def5d
void InstallSymbolizeCallback(SymbolizeCallback callback);
Packit Service 9def5d
Packit Service 9def5d
// Installs a callback function, which will be called instead of
Packit Service 9def5d
// OpenObjectFileContainingPcAndGetStartAddress.  The callback is expected
Packit Service 9def5d
// to 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
typedef int (*SymbolizeOpenObjectFileCallback)(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
void InstallSymbolizeOpenObjectFileCallback(
Packit Service 9def5d
    SymbolizeOpenObjectFileCallback callback);
Packit Service 9def5d
Packit Service 9def5d
_END_GOOGLE_NAMESPACE_
Packit Service 9def5d
Packit Service 9def5d
#endif
Packit Service 9def5d
Packit Service 9def5d
_START_GOOGLE_NAMESPACE_
Packit Service 9def5d
Packit Service 9def5d
// Symbolizes a program counter.  On success, returns true and write the
Packit Service 9def5d
// symbol name to "out".  The symbol name is demangled if possible
Packit Service 9def5d
// (supports symbols generated by GCC 3.x or newer).  Otherwise,
Packit Service 9def5d
// returns false.
Packit Service 9def5d
bool Symbolize(void *pc, char *out, int out_size);
Packit Service 9def5d
Packit Service 9def5d
_END_GOOGLE_NAMESPACE_
Packit Service 9def5d
Packit Service 9def5d
#endif  // BASE_SYMBOLIZE_H_