From: Antoine Busque Date: Thu, 21 Apr 2016 05:55:10 +0000 (-0400) Subject: Rename so-info to bin-info X-Git-Tag: v1.4.0-rc1~53 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=dae52e7673333cf41828c8686158948ce93ef104 Rename so-info to bin-info Signed-off-by: Antoine Busque Signed-off-by: Jérémie Galarneau --- diff --git a/.gitignore b/.gitignore index 420f2440..177307d1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,10 +5,10 @@ /tests/lib/test_bitfield /tests/lib/test_seek /tests/lib/test_ctf_writer -/tests/lib/test_durin -/tests/lib/test_so_info /tests/lib/test_seek_big_trace /tests/lib/test_seek_empty_packet +/tests/lib/test_dwarf +/tests/lib/test_bin_info *.o *.a *.la diff --git a/include/Makefile.am b/include/Makefile.am index ba6d9127..eb58604a 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -36,7 +36,7 @@ noinst_HEADERS = \ babeltrace/debuginfo.h \ babeltrace/trace-debuginfo.h \ babeltrace/dwarf.h \ - babeltrace/so-info.h \ + babeltrace/bin-info.h \ babeltrace/utils.h \ babeltrace/ctf-ir/metadata.h \ babeltrace/ctf/events-internal.h \ diff --git a/include/babeltrace/bin-info.h b/include/babeltrace/bin-info.h new file mode 100644 index 00000000..e2af8952 --- /dev/null +++ b/include/babeltrace/bin-info.h @@ -0,0 +1,226 @@ +#ifndef _BABELTRACE_BIN_INFO_H +#define _BABELTRACE_BIN_INFO_H + +/* + * Babeltrace - Executable and Shared Object Debug Info Reader + * + * Copyright 2015 Antoine Busque + * + * Author: Antoine Busque + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#define DEFAULT_DEBUG_DIR "/usr/lib/debug" +#define DEBUG_SUBDIR ".debug/" +#define BUILD_ID_SUBDIR ".build-id/" +#define BUILD_ID_SUFFIX ".debug" + +struct bin_info { + /* Base virtual memory address. */ + uint64_t low_addr; + /* Upper bound of exec address space. */ + uint64_t high_addr; + /* Size of exec address space. */ + uint64_t memsz; + /* Paths to ELF and DWARF files. */ + char *elf_path; + char *dwarf_path; + /* libelf and libdw objects representing the files. */ + Elf *elf_file; + Dwarf *dwarf_info; + /* Optional build ID info. */ + uint8_t *build_id; + size_t build_id_len; + /* Optional debug link info. */ + char *dbg_link_filename; + uint32_t dbg_link_crc; + /* FDs to ELF and DWARF files. */ + int elf_fd; + int dwarf_fd; + /* Denotes whether the executable is position independent code. */ + bool is_pic:1; + /* + * Denotes whether the executable only has ELF symbols and no + * DWARF info. + */ + bool is_elf_only:1; +}; + +struct source_location { + uint64_t line_no; + char *filename; +}; + +/** + * Initializes the bin_info framework. Call this before calling + * anything else. + * + * @returns 0 on success, -1 on failure + */ +BT_HIDDEN +int bin_info_init(void); + +/** + * Instantiate a structure representing an ELF executable, possibly + * with DWARF info, located at the given path. + * + * @param path Path to the ELF file + * @param low_addr Base address of the executable + * @param memsz In-memory size of the executable + * @param is_pic Whether the executable is position independent + * code (PIC) + * @returns Pointer to the new bin_info on success, + * NULL on failure. + */ +BT_HIDDEN +struct bin_info *bin_info_create(const char *path, uint64_t low_addr, + uint64_t memsz, bool is_pic); + +/** + * Destroy the given bin_info instance + * + * @param bin bin_info instance to destroy + */ +BT_HIDDEN +void bin_info_destroy(struct bin_info *bin); + +/** + * Sets the build ID information for a given bin_info instance. + * + * @param bin The bin_info instance for which to set + * the build ID + * @param build_id Array of bytes containing the actual ID + * @param build_id_len Length in bytes of the build_id + * @returns 0 on success, -1 on failure + */ +BT_HIDDEN +int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id, + size_t build_id_len); + +/** + * Sets the debug link information for a given bin_info instance. + * + * @param bin The bin_info instance for which to set + * the debug link + * @param filename Name of the separate debug info file + * @param crc Checksum for the debug info file + * @returns 0 on success, -1 on failure + */ +BT_HIDDEN +int bin_info_set_debug_link(struct bin_info *bin, char *filename, uint32_t crc); + +/** + * Returns whether or not the given bin info \p bin contains the + * address \p addr. + * + * @param bin bin_info instance + * @param addr Address to lookup + * @returns 1 if \p bin contains \p addr, 0 if it does not, + * -1 on failure + */ +static inline +int bin_info_has_address(struct bin_info *bin, uint64_t addr) +{ + if (!bin) { + return -1; + } + + return addr >= bin->low_addr && addr < bin->high_addr; +} + +/** + * Get the name of the function containing a given address within an + * executable. + * + * If no DWARF info is available, the function falls back to ELF + * symbols and the "function name" is in fact the name of the closest + * symbol, followed by the offset between the symbol and the address. + * + * On success, if found, the out parameter `func_name` is set. The ownership + * of `func_name` is passed to the caller. On failure, `func_name` remains + * unchanged. + * + * @param bin bin_info instance for the executable containing + * the address + * @param addr Virtual memory address for which to find the + * function name + * @param func_name Out parameter, the function name. + * @returns 0 on success, -1 on failure + */ +BT_HIDDEN +int bin_info_lookup_function_name(struct bin_info *bin, uint64_t addr, + char **func_name); + +/** + * Get the source location (file name and line number) for a given + * address within an executable. + * + * If no DWARF info is available, the source location cannot be found + * and the function will return unsuccesfully. + * + * On success, if found, the out parameter `src_loc` is set. The ownership + * of `src_loc` is passed to the caller. On failure, `src_loc` remains + * unchanged. + * + * @param bin bin_info instance for the executable containing + * the address + * @param addr Virtual memory address for which to find the + * source location + * @param src_loc Out parameter, the source location + * @returns 0 on success, -1 on failure + */ +BT_HIDDEN +int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr, + struct source_location **src_loc); +/** + * Get a string representing the location within the binary of a given + * address. + * + * In the case of a PIC binary, the location is relative (+0x1234). + * For a non-PIC binary, the location is absolute (@0x1234) + * + * On success, the out parameter `bin_loc` is set. The ownership is + * passed to the caller. On failure, `bin_loc` remains unchanged. + * + * @param bin bin_info instance for the executable containing + * the address + * @param addr Virtual memory address for which to find the + * binary location + * @param bin_loc Out parameter, the binary location + * @returns 0 on success, -1 on failure + */ +BT_HIDDEN +int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc); + +/** + * Destroy the given source_location instance + * + * @param src_loc source_location instance to destroy + */ +BT_HIDDEN +void source_location_destroy(struct source_location *src_loc); + +#endif /* _BABELTRACE_BIN_INFO_H */ diff --git a/include/babeltrace/so-info.h b/include/babeltrace/so-info.h deleted file mode 100644 index 17d0f59c..00000000 --- a/include/babeltrace/so-info.h +++ /dev/null @@ -1,223 +0,0 @@ -#ifndef _BABELTRACE_SO_INFO_H -#define _BABELTRACE_SO_INFO_H - -/* - * Babeltrace - Executable and Shared Object Debug Info Reader - * - * Copyright 2015 Antoine Busque - * - * Author: Antoine Busque - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include -#include -#include -#include -#include - -#define DEFAULT_DEBUG_DIR "/usr/lib/debug" -#define DEBUG_SUBDIR ".debug/" -#define BUILD_ID_SUBDIR ".build-id/" -#define BUILD_ID_SUFFIX ".debug" - -struct so_info { - /* Base virtual memory address. */ - uint64_t low_addr; - /* Upper bound of exec address space. */ - uint64_t high_addr; - /* Size of exec address space. */ - uint64_t memsz; - /* Paths to ELF and DWARF files. */ - char *elf_path; - char *dwarf_path; - /* libelf and libdw objects representing the files. */ - Elf *elf_file; - Dwarf *dwarf_info; - /* Optional build ID info. */ - uint8_t *build_id; - size_t build_id_len; - /* Optional debug link info. */ - char *dbg_link_filename; - uint32_t dbg_link_crc; - /* FDs to ELF and DWARF files. */ - int elf_fd; - int dwarf_fd; - /* Denotes whether the executable is position independent code. */ - bool is_pic:1; - /* Denotes whether the SO only has ELF symbols and no DWARF info. */ - bool is_elf_only:1; -}; - -struct source_location { - uint64_t line_no; - char *filename; -}; - -/** - * Initializes the so_info framework. Call this before calling - * anything else. - * - * @returns 0 on success, -1 on failure - */ -BT_HIDDEN -int so_info_init(void); - -/** - * Instantiate a structure representing an ELF executable, possibly - * with DWARF info, located at the given path. - * - * @param path Path to the ELF file - * @param low_addr Base address of the executable - * @param memsz In-memory size of the executable - * @param is_pic Whether the executable is position independent - * code (PIC) - * @returns Pointer to the new so_info on success, - * NULL on failure. - */ -BT_HIDDEN -struct so_info *so_info_create(const char *path, uint64_t low_addr, - uint64_t memsz, bool is_pic); - -/** - * Destroy the given so_info instance - * - * @param so so_info instance to destroy - */ -BT_HIDDEN -void so_info_destroy(struct so_info *so); - -/** - * Sets the build ID information for a given so_info instance. - * - * @param so The so_info instance for which to set - * the build ID - * @param build_id Array of bytes containing the actual ID - * @param build_id_len Length in bytes of the build_id - * @returns 0 on success, -1 on failure - */ -BT_HIDDEN -int so_info_set_build_id(struct so_info *so, uint8_t *build_id, - size_t build_id_len); - -/** - * Sets the debug link information for a given so_info instance. - * - * @param so The so_info instance for which to set - * the debug link - * @param filename Name of the separate debug info file - * @param crc Checksum for the debug info file - * @returns 0 on success, -1 on failure - */ -BT_HIDDEN -int so_info_set_debug_link(struct so_info *so, char *filename, uint32_t crc); - -/** - * Returns whether or not the given SO info \p so contains the address - * \p addr. - * - * @param so so_info instance - * @param addr Address to lookup - * @returns 1 if \p so contains \p addr, 0 if it does not, - * -1 on failure - */ -static inline -int so_info_has_address(struct so_info *so, uint64_t addr) -{ - if (!so) { - return -1; - } - - return addr >= so->low_addr && addr < so->high_addr; -} - -/** - * Get the name of the function containing a given address within an - * executable. - * - * If no DWARF info is available, the function falls back to ELF - * symbols and the "function name" is in fact the name of the closest - * symbol, followed by the offset between the symbol and the address. - * - * On success, if found, the out parameter `func_name` is set. The ownership - * of `func_name` is passed to the caller. On failure, `func_name` remains - * unchanged. - * - * @param so so_info instance for the executable containing - * the address - * @param addr Virtual memory address for which to find the - * function name - * @param func_name Out parameter, the function name. - * @returns 0 on success, -1 on failure - */ -BT_HIDDEN -int so_info_lookup_function_name(struct so_info *so, uint64_t addr, - char **func_name); - -/** - * Get the source location (file name and line number) for a given - * address within an executable. - * - * If no DWARF info is available, the source location cannot be found - * and the function will return unsuccesfully. - * - * On success, if found, the out parameter `src_loc` is set. The ownership - * of `src_loc` is passed to the caller. On failure, `src_loc` remains - * unchanged. - * - * @param so so_info instance for the executable containing - * the address - * @param addr Virtual memory address for which to find the - * source location - * @param src_loc Out parameter, the source location - * @returns 0 on success, -1 on failure - */ -BT_HIDDEN -int so_info_lookup_source_location(struct so_info *so, uint64_t addr, - struct source_location **src_loc); -/** - * Get a string representing the location within the binary of a given - * address. - * - * In the case of a PIC binary, the location is relative (+0x1234). - * For a non-PIC binary, the location is absolute (@0x1234) - * - * On success, the out parameter `bin_loc` is set. The ownership is - * passed to the caller. On failure, `bin_loc` remains unchanged. - * - * @param so so_info instance for the executable containing - * the address - * @param addr Virtual memory address for which to find the - * binary location - * @param bin_loc Out parameter, the binary location - * @returns 0 on success, -1 on failure - */ -BT_HIDDEN -int so_info_get_bin_loc(struct so_info *so, uint64_t addr, char **bin_loc); - -/** - * Destroy the given source_location instance - * - * @param src_loc source_location instance to destroy - */ -BT_HIDDEN -void source_location_destroy(struct source_location *src_loc); - -#endif /* _BABELTRACE_SO_INFO_H */ diff --git a/lib/Makefile.am b/lib/Makefile.am index 1587af89..17a92f71 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -17,7 +17,7 @@ if ENABLE_DEBUGINFO noinst_LTLIBRARIES = libdebuginfo.la libdebuginfo_la_SOURCES = debuginfo.c \ - so-info.c \ + bin-info.c \ dwarf.c \ crc32.c \ utils.c diff --git a/lib/bin-info.c b/lib/bin-info.c new file mode 100644 index 00000000..023515ef --- /dev/null +++ b/lib/bin-info.c @@ -0,0 +1,1325 @@ +/* + * bin-info.c + * + * Babeltrace - Executable and Shared Object Debug Info Reader + * + * Copyright 2015 Antoine Busque + * + * Author: Antoine Busque + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * An address printed in hex is at most 20 bytes (16 for 64-bits + + * leading 0x + optional leading '+' if addr is an offset + null + * character). + */ +#define ADDR_STR_LEN 20 + +BT_HIDDEN +int bin_info_init(void) +{ + int ret = 0; + + if (elf_version(EV_CURRENT) == EV_NONE) { + printf_debug("ELF library initialization failed: %s\n", + elf_errmsg(-1)); + ret = -1; + } + + return ret; +} + +BT_HIDDEN +struct bin_info *bin_info_create(const char *path, uint64_t low_addr, + uint64_t memsz, bool is_pic) +{ + struct bin_info *bin = NULL; + + if (!path) { + goto error; + } + + bin = g_new0(struct bin_info, 1); + if (!bin) { + goto error; + } + + if (opt_debug_info_target_prefix) { + bin->elf_path = g_build_path("/", opt_debug_info_target_prefix, + path, NULL); + } else { + bin->elf_path = strdup(path); + } + + if (!bin->elf_path) { + goto error; + } + + bin->is_pic = is_pic; + bin->memsz = memsz; + bin->low_addr = low_addr; + bin->high_addr = bin->low_addr + bin->memsz; + + return bin; + +error: + bin_info_destroy(bin); + return NULL; +} + +BT_HIDDEN +void bin_info_destroy(struct bin_info *bin) +{ + if (!bin) { + return; + } + + dwarf_end(bin->dwarf_info); + + free(bin->elf_path); + free(bin->dwarf_path); + free(bin->build_id); + free(bin->dbg_link_filename); + + elf_end(bin->elf_file); + + close(bin->elf_fd); + close(bin->dwarf_fd); + + g_free(bin); +} + + +BT_HIDDEN +int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id, + size_t build_id_len) +{ + if (!bin || !build_id) { + goto error; + } + + bin->build_id = malloc(build_id_len); + if (!bin->build_id) { + goto error; + } + + memcpy(bin->build_id, build_id, build_id_len); + bin->build_id_len = build_id_len; + + /* + * Reset the is_elf_only flag in case it had been set + * previously, because we might find separate debug info using + * the new build id information. + */ + bin->is_elf_only = false; + + return 0; + +error: + + return -1; +} + +BT_HIDDEN +int bin_info_set_debug_link(struct bin_info *bin, char *filename, uint32_t crc) +{ + if (!bin || !filename) { + goto error; + } + + bin->dbg_link_filename = strdup(filename); + if (!bin->dbg_link_filename) { + goto error; + } + + bin->dbg_link_crc = crc; + + /* + * Reset the is_elf_only flag in case it had been set + * previously, because we might find separate debug info using + * the new build id information. + */ + bin->is_elf_only = false; + + return 0; + +error: + + return -1; +} + +/** + * Tries to read DWARF info from the location given by path, and + * attach it to the given bin_info instance if it exists. + * + * @param bin bin_info instance for which to set DWARF info + * @param path Presumed location of the DWARF info + * @returns 0 on success, -1 on failure + */ +static +int bin_info_set_dwarf_info_from_path(struct bin_info *bin, char *path) +{ + int fd = -1, ret = 0; + struct bt_dwarf_cu *cu = NULL; + Dwarf *dwarf_info = NULL; + + if (!bin || !path) { + goto error; + } + + fd = open(path, O_RDONLY); + if (fd < 0) { + goto error; + } + + dwarf_info = dwarf_begin(fd, DWARF_C_READ); + if (!dwarf_info) { + goto error; + } + + /* + * Check if the dwarf info has any CU. If not, the + * executable's object file contains no DWARF info. + */ + cu = bt_dwarf_cu_create(dwarf_info); + if (!cu) { + goto error; + } + + ret = bt_dwarf_cu_next(cu); + if (ret) { + goto error; + } + + bin->dwarf_fd = fd; + bin->dwarf_path = strdup(path); + if (!bin->dwarf_path) { + goto error; + } + bin->dwarf_info = dwarf_info; + free(cu); + + return 0; + +error: + close(fd); + dwarf_end(dwarf_info); + g_free(dwarf_info); + free(cu); + + return -1; +} + +/** + * Try to set the dwarf_info for a given bin_info instance via the + * build ID method. + * + * @param bin bin_info instance for which to retrieve the + * DWARF info via build ID + * @returns 0 on success (i.e. dwarf_info set), -1 on failure + */ +static +int bin_info_set_dwarf_info_build_id(struct bin_info *bin) +{ + int i = 0, ret = 0; + char *path = NULL, *build_id_file = NULL; + const char *dbg_dir = NULL; + size_t build_id_file_len; + + if (!bin || !bin->build_id) { + goto error; + } + + dbg_dir = opt_debug_info_dir ? : DEFAULT_DEBUG_DIR; + + /* 2 characters per byte printed in hex, +1 for '/' and +1 for + * '\0' */ + build_id_file_len = (2 * bin->build_id_len) + 1 + + strlen(BUILD_ID_SUFFIX) + 1; + build_id_file = malloc(build_id_file_len); + if (!build_id_file) { + goto error; + } + + snprintf(build_id_file, 4, "%02x/", bin->build_id[0]); + for (i = 1; i < bin->build_id_len; ++i) { + int path_idx = 3 + 2 * (i - 1); + + snprintf(&build_id_file[path_idx], 3, "%02x", bin->build_id[i]); + } + strcat(build_id_file, BUILD_ID_SUFFIX); + + path = g_build_path("/", dbg_dir, BUILD_ID_SUBDIR, build_id_file, NULL); + if (!path) { + goto error; + } + + ret = bin_info_set_dwarf_info_from_path(bin, path); + if (ret) { + goto error; + } + + goto end; + +error: + ret = -1; +end: + free(build_id_file); + free(path); + + return ret; +} + +/** + * Tests whether the file located at path exists and has the expected + * checksum. + * + * This predicate is used when looking up separate debug info via the + * GNU debuglink method. The expected crc can be found .gnu_debuglink + * section in the original ELF file, along with the filename for the + * file containing the debug info. + * + * @param path Full path at which to look for the debug file + * @param crc Expected checksum for the debug file + * @returns 1 if the file exists and has the correct checksum, + * 0 otherwise + */ +static +int is_valid_debug_file(char *path, uint32_t crc) +{ + int ret = 0, fd = -1; + uint32_t _crc = 0; + + if (!path) { + goto end; + } + + fd = open(path, O_RDONLY); + if (fd < 0) { + goto end; + } + + ret = crc32(fd, &_crc); + if (ret) { + ret = 0; + goto end; + } + + ret = (crc == _crc); + +end: + close(fd); + return ret; +} + +/** + * Try to set the dwarf_info for a given bin_info instance via the + * build ID method. + * + * @param bin bin_info instance for which to retrieve the + * DWARF info via debug link + * @returns 0 on success (i.e. dwarf_info set), -1 on failure + */ +static +int bin_info_set_dwarf_info_debug_link(struct bin_info *bin) +{ + int ret = 0; + const char *dbg_dir = NULL; + char *dir_name = NULL, *bin_dir = NULL, *path = NULL; + size_t max_path_len = 0; + + if (!bin || !bin->dbg_link_filename) { + goto error; + } + + dbg_dir = opt_debug_info_dir ? : DEFAULT_DEBUG_DIR; + + dir_name = dirname(bin->elf_path); + if (!dir_name) { + goto error; + } + + /* bin_dir is just dir_name with a trailing slash */ + bin_dir = malloc(strlen(dir_name) + 2); + if (!bin_dir) { + goto error; + } + + strcpy(bin_dir, dir_name); + strcat(bin_dir, "/"); + + max_path_len = strlen(dbg_dir) + strlen(bin_dir) + + strlen(DEBUG_SUBDIR) + strlen(bin->dbg_link_filename) + + 1; + path = malloc(max_path_len); + if (!path) { + goto error; + } + + /* First look in the executable's dir */ + strcpy(path, bin_dir); + strcat(path, bin->dbg_link_filename); + + if (is_valid_debug_file(path, bin->dbg_link_crc)) { + goto found; + } + + /* If not found, look in .debug subdir */ + strcpy(path, bin_dir); + strcat(path, DEBUG_SUBDIR); + strcat(path, bin->dbg_link_filename); + + if (is_valid_debug_file(path, bin->dbg_link_crc)) { + goto found; + } + + /* Lastly, look under the global debug directory */ + strcpy(path, dbg_dir); + strcat(path, bin_dir); + strcat(path, bin->dbg_link_filename); + + if (is_valid_debug_file(path, bin->dbg_link_crc)) { + goto found; + } + +error: + ret = -1; +end: + free(path); + free(bin_dir); + + return ret; + +found: + ret = bin_info_set_dwarf_info_from_path(bin, path); + if (ret) { + goto error; + } + + goto end; +} + +/** + * Initialize the DWARF info for a given executable. + * + * @param bin bin_info instance + * @returns 0 on success, -1 on failure + */ +static +int bin_info_set_dwarf_info(struct bin_info *bin) +{ + int ret = 0; + + if (!bin) { + goto error; + } + + /* First try to set the DWARF info from the ELF file */ + ret = bin_info_set_dwarf_info_from_path(bin, bin->elf_path); + if (!ret) { + goto end; + } + + /* + * If that fails, try to find separate debug info via build ID + * and debug link. + */ + ret = bin_info_set_dwarf_info_build_id(bin); + if (!ret) { + goto end; + } + + ret = bin_info_set_dwarf_info_debug_link(bin); + if (!ret) { + goto end; + } + +error: + ret = -1; +end: + return ret; +} + +/** + * Initialize the ELF file for a given executable. + * + * @param bin bin_info instance + * @returns 0 on success, -1 on failure + */ +static +int bin_info_set_elf_file(struct bin_info *bin) +{ + int elf_fd; + Elf *elf_file = NULL; + + if (!bin) { + goto error; + } + + elf_fd = open(bin->elf_path, O_RDONLY); + if (elf_fd < 0) { + printf_verbose("Failed to open %s\n", bin->elf_path); + goto error; + } + + elf_file = elf_begin(elf_fd, ELF_C_READ, NULL); + if (!elf_file) { + printf_debug("elf_begin failed: %s\n", elf_errmsg(-1)); + goto error; + } + + if (elf_kind(elf_file) != ELF_K_ELF) { + printf_verbose("Error: %s is not an ELF object\n", + bin->elf_path); + goto error; + } + + bin->elf_fd = elf_fd; + bin->elf_file = elf_file; + return 0; + +error: + close(elf_fd); + elf_end(elf_file); + return -1; +} + + +BT_HIDDEN +void source_location_destroy(struct source_location *src_loc) +{ + if (!src_loc) { + return; + } + + free(src_loc->filename); + g_free(src_loc); +} +/** + * Append a string representation of an address offset to an existing + * string. + * + * On success, the out parameter `result` will contain the base string + * followed by the offset string of the form "+0x1234". On failure, + * `result` remains unchanged. + * + * @param base_str The string to which to append an offset string + * @param low_addr The lower virtual memory address, the base from + * which the offset is computed + * @param high_addr The higher virtual memory address + * @param result Out parameter, the base string followed by the + * offset string + * @returns 0 on success, -1 on failure + */ +static +int bin_info_append_offset_str(const char *base_str, uint64_t low_addr, + uint64_t high_addr, char **result) +{ + int ret; + uint64_t offset; + char *_result = NULL; + char offset_str[ADDR_STR_LEN]; + + if (!base_str || !result) { + goto error; + } + + offset = high_addr - low_addr; + + _result = malloc(strlen(base_str) + ADDR_STR_LEN); + if (!_result) { + goto error; + } + + ret = snprintf(offset_str, ADDR_STR_LEN, "+%#0" PRIx64, offset); + if (ret < 0) { + goto error; + } + strcpy(_result, base_str); + strcat(_result, offset_str); + *result = _result; + + return 0; + +error: + free(_result); + return -1; +} + +/** + * Try to find the symbol closest to an address within a given ELF + * section. + * + * Only function symbols are taken into account. The symbol's address + * must precede `addr`. A symbol with a closer address might exist + * after `addr` but is irrelevant because it cannot encompass `addr`. + * + * On success, if found, the out parameters `sym` and `shdr` are + * set. On failure or if none are found, they remain unchanged. + * + * @param scn ELF section in which to look for the address + * @param addr Virtual memory address for which to find the + * nearest function symbol + * @param sym Out parameter, the nearest function symbol + * @param shdr Out parameter, the section header for scn + * @returns 0 on success, -1 on failure + */ +static +int bin_info_get_nearest_symbol_from_section(Elf_Scn *scn, uint64_t addr, + GElf_Sym **sym, GElf_Shdr **shdr) +{ + int i; + size_t symbol_count; + Elf_Data *data = NULL; + GElf_Shdr *_shdr = NULL; + GElf_Sym *nearest_sym = NULL; + + if (!scn || !sym || !shdr) { + goto error; + } + + _shdr = g_new0(GElf_Shdr, 1); + if (!_shdr) { + goto error; + } + + _shdr = gelf_getshdr(scn, _shdr); + if (!_shdr) { + goto error; + } + + if (_shdr->sh_type != SHT_SYMTAB) { + /* + * We are only interested in symbol table (symtab) + * sections, skip this one. + */ + goto end; + } + + data = elf_getdata(scn, NULL); + if (!data) { + goto error; + } + + symbol_count = _shdr->sh_size / _shdr->sh_entsize; + + for (i = 0; i < symbol_count; ++i) { + GElf_Sym *cur_sym = NULL; + + cur_sym = g_new0(GElf_Sym, 1); + if (!cur_sym) { + goto error; + } + cur_sym = gelf_getsym(data, i, cur_sym); + if (!cur_sym) { + goto error; + } + if (GELF_ST_TYPE(cur_sym->st_info) != STT_FUNC) { + /* We're only interested in the functions. */ + g_free(cur_sym); + continue; + } + + if (cur_sym->st_value <= addr && + (!nearest_sym || + cur_sym->st_value > nearest_sym->st_value)) { + g_free(nearest_sym); + nearest_sym = cur_sym; + } else { + g_free(cur_sym); + } + } + +end: + if (nearest_sym) { + *sym = nearest_sym; + *shdr = _shdr; + } else { + g_free(_shdr); + } + + return 0; + +error: + g_free(nearest_sym); + g_free(_shdr); + return -1; +} + +/** + * Get the name of the function containing a given address within an + * executable using ELF symbols. + * + * The function name is in fact the name of the nearest ELF symbol, + * followed by the offset in bytes between the address and the symbol + * (in hex), separated by a '+' character. + * + * If found, the out parameter `func_name` is set on success. On failure, + * it remains unchanged. + * + * @param bin bin_info instance for the executable containing + * the address + * @param addr Virtual memory address for which to find the + * function name + * @param func_name Out parameter, the function name + * @returns 0 on success, -1 on failure + */ +static +int bin_info_lookup_elf_function_name(struct bin_info *bin, uint64_t addr, + char **func_name) +{ + /* + * TODO (possible optimisation): if an ELF has no symtab + * section, it has been stripped. Therefore, it would be wise + * to store a flag indicating the stripped status after the + * first iteration to prevent subsequent ones. + */ + int ret = 0; + Elf_Scn *scn = NULL; + GElf_Sym *sym = NULL; + GElf_Shdr *shdr = NULL; + char *sym_name = NULL; + + /* Set ELF file if it hasn't been accessed yet. */ + if (!bin->elf_file) { + ret = bin_info_set_elf_file(bin); + if (ret) { + /* Failed to set ELF file. */ + goto error; + } + } + + scn = elf_nextscn(bin->elf_file, scn); + if (!scn) { + goto error; + } + + while (scn && !sym) { + ret = bin_info_get_nearest_symbol_from_section( + scn, addr, &sym, &shdr); + if (ret) { + goto error; + } + + scn = elf_nextscn(bin->elf_file, scn); + } + + if (sym) { + sym_name = elf_strptr(bin->elf_file, shdr->sh_link, + sym->st_name); + if (!sym_name) { + goto error; + } + + ret = bin_info_append_offset_str(sym_name, sym->st_value, addr, + func_name); + if (ret) { + goto error; + } + } + + g_free(shdr); + g_free(sym); + return 0; + +error: + g_free(shdr); + g_free(sym); + return -1; +} + +/** + * Get the name of the function containing a given address within a + * given compile unit (CU). + * + * If found, the out parameter `func_name` is set on success. On + * failure, it remains unchanged. + * + * @param cu bt_dwarf_cu instance which may contain the address + * @param addr Virtual memory address for which to find the + * function name + * @param func_name Out parameter, the function name + * @returns 0 on success, -1 on failure + */ +static +int bin_info_lookup_cu_function_name(struct bt_dwarf_cu *cu, uint64_t addr, + char **func_name) +{ + int ret = 0, found = 0; + struct bt_dwarf_die *die = NULL; + + if (!cu || !func_name) { + goto error; + } + + die = bt_dwarf_die_create(cu); + if (!die) { + goto error; + } + + while (bt_dwarf_die_next(die) == 0) { + int tag; + + ret = bt_dwarf_die_get_tag(die, &tag); + if (ret) { + goto error; + } + + if (tag == DW_TAG_subprogram) { + ret = bt_dwarf_die_contains_addr(die, addr, &found); + if (ret) { + goto error; + } + + if (found) { + break; + } + } + } + + if (found) { + uint64_t low_addr = 0; + char *die_name = NULL; + + ret = bt_dwarf_die_get_name(die, &die_name); + if (ret) { + goto error; + } + + ret = dwarf_lowpc(die->dwarf_die, &low_addr); + if (ret) { + goto error; + } + + ret = bin_info_append_offset_str(die_name, low_addr, addr, + func_name); + if (ret) { + goto error; + } + } + + bt_dwarf_die_destroy(die); + return 0; + +error: + bt_dwarf_die_destroy(die); + return -1; +} + +/** + * Get the name of the function containing a given address within an + * executable using DWARF debug info. + * + * If found, the out parameter `func_name` is set on success. On + * failure, it remains unchanged. + * + * @param bin bin_info instance for the executable containing + * the address + * @param addr Virtual memory address for which to find the + * function name + * @param func_name Out parameter, the function name + * @returns 0 on success, -1 on failure + */ +static +int bin_info_lookup_dwarf_function_name(struct bin_info *bin, uint64_t addr, + char **func_name) +{ + int ret = 0; + char *_func_name = NULL; + struct bt_dwarf_cu *cu = NULL; + + if (!bin || !func_name) { + goto error; + } + + cu = bt_dwarf_cu_create(bin->dwarf_info); + if (!cu) { + goto error; + } + + while (bt_dwarf_cu_next(cu) == 0) { + ret = bin_info_lookup_cu_function_name(cu, addr, &_func_name); + if (ret) { + goto error; + } + + if (_func_name) { + break; + } + } + + if (_func_name) { + *func_name = _func_name; + } + + bt_dwarf_cu_destroy(cu); + return 0; + +error: + bt_dwarf_cu_destroy(cu); + return -1; +} + +BT_HIDDEN +int bin_info_lookup_function_name(struct bin_info *bin, uint64_t addr, + char **func_name) +{ + int ret = 0; + char *_func_name = NULL; + + if (!bin || !func_name) { + goto error; + } + + /* Set DWARF info if it hasn't been accessed yet. */ + if (!bin->dwarf_info && !bin->is_elf_only) { + ret = bin_info_set_dwarf_info(bin); + if (ret) { + /* Failed to set DWARF info, fallback to ELF. */ + bin->is_elf_only = true; + } + } + + if (!bin_info_has_address(bin, addr)) { + goto error; + } + + /* + * Addresses in ELF and DWARF are relative to base address for + * PIC, so make the address argument relative too if needed. + */ + if (bin->is_pic) { + addr -= bin->low_addr; + } + + if (bin->is_elf_only) { + ret = bin_info_lookup_elf_function_name(bin, addr, &_func_name); + } else { + ret = bin_info_lookup_dwarf_function_name(bin, addr, &_func_name); + } + + if (ret || !_func_name) { + goto error; + } + + *func_name = _func_name; + return 0; + +error: + return -1; +} + +BT_HIDDEN +int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc) +{ + int ret = 0; + char *_bin_loc = NULL; + + if (!bin || !bin_loc) { + goto error; + } + + if (bin->is_pic) { + addr -= bin->low_addr; + ret = asprintf(&_bin_loc, "+%#0" PRIx64, addr); + } else { + ret = asprintf(&_bin_loc, "@%#0" PRIx64, addr); + } + + if (ret == -1 || !_bin_loc) { + goto error; + } + + *bin_loc = _bin_loc; + return 0; + +error: + return -1; +} + +/** + * Predicate used to determine whether the children of a given DIE + * contain a specific address. + * + * More specifically, the parameter `die` is expected to be a + * subprogram (function) DIE, and this predicate tells whether any + * subroutines are inlined within this function and would contain + * `addr`. + * + * Do note that this function advances the position of `die`. If the + * address is found within one of its children, `die` will be pointing + * to that child upon returning from the function, allowing to extract + * the information deemed necessary. + * + * @param die The parent DIE in whose children the address will be + * looked for + * @param addr The address for which to look for in the DIEs + * @returns Returns 1 if the address was found, 0 if not + */ +static +int bin_info_child_die_has_address(struct bt_dwarf_die *die, uint64_t addr) +{ + int ret = 0, contains = 0; + + if (!die) { + goto error; + } + + ret = bt_dwarf_die_child(die); + if (ret) { + goto error; + } + + do { + int tag; + + ret = bt_dwarf_die_get_tag(die, &tag); + if (ret) { + goto error; + } + + if (tag == DW_TAG_inlined_subroutine) { + ret = bt_dwarf_die_contains_addr(die, addr, &contains); + if (ret) { + goto error; + } + + if (contains) { + ret = 1; + goto end; + } + } + } while (bt_dwarf_die_next(die) == 0); + +end: + return ret; + +error: + ret = 0; + goto end; +} + +/** + * Lookup the source location for a given address within a CU, making + * the assumption that it is contained within an inline routine in a + * function. + * + * @param cu bt_dwarf_cu instance in which to look for the address + * @param addr The address for which to look for + * @param src_loc Out parameter, the source location (filename and + * line number) for the address + * @returns 0 on success, -1 on failure + */ +static +int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu *cu, uint64_t addr, + struct source_location **src_loc) +{ + int ret = 0, found = 0; + struct bt_dwarf_die *die = NULL; + struct source_location *_src_loc = NULL; + + if (!cu || !src_loc) { + goto error; + } + + die = bt_dwarf_die_create(cu); + if (!die) { + goto error; + } + + while (bt_dwarf_die_next(die) == 0) { + int tag; + + ret = bt_dwarf_die_get_tag(die, &tag); + if (ret) { + goto error; + } + + if (tag == DW_TAG_subprogram) { + int contains = 0; + + ret = bt_dwarf_die_contains_addr(die, addr, &contains); + if (ret) { + goto error; + } + + if (contains) { + /* + * Try to find an inlined subroutine + * child of this DIE containing addr. + */ + found = bin_info_child_die_has_address( + die, addr); + goto end; + } + } + } + +end: + if (found) { + char *filename = NULL; + uint64_t line_no; + + _src_loc = g_new0(struct source_location, 1); + if (!_src_loc) { + goto error; + } + + ret = bt_dwarf_die_get_call_file(die, &filename); + if (ret) { + goto error; + } + ret = bt_dwarf_die_get_call_line(die, &line_no); + if (ret) { + free(filename); + goto error; + } + + _src_loc->filename = filename; + _src_loc->line_no = line_no; + *src_loc = _src_loc; + } + + bt_dwarf_die_destroy(die); + return 0; + +error: + source_location_destroy(_src_loc); + bt_dwarf_die_destroy(die); + return -1; +} + +/** + * Lookup the source location for a given address within a CU, + * assuming that it is contained within an inlined function. + * + * A source location can be found regardless of inlining status for + * this method, but in the case of an inlined function, the returned + * source location will point not to the callsite but rather to the + * definition site of the inline function. + * + * @param cu bt_dwarf_cu instance in which to look for the address + * @param addr The address for which to look for + * @param src_loc Out parameter, the source location (filename and + * line number) for the address + * @returns 0 on success, -1 on failure + */ +static +int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu *cu, uint64_t addr, + struct source_location **src_loc) +{ + struct source_location *_src_loc = NULL; + struct bt_dwarf_die *die = NULL; + const char *filename = NULL; + Dwarf_Line *line = NULL; + Dwarf_Addr line_addr; + int ret, line_no; + + if (!cu || !src_loc) { + goto error; + } + + die = bt_dwarf_die_create(cu); + if (!die) { + goto error; + } + + line = dwarf_getsrc_die(die->dwarf_die, addr); + if (!line) { + goto error; + } + + ret = dwarf_lineaddr(line, &line_addr); + if (ret) { + goto error; + } + + filename = dwarf_linesrc(line, NULL, NULL); + if (!filename) { + goto error; + } + + if (addr == line_addr) { + _src_loc = g_new0(struct source_location, 1); + if (!_src_loc) { + goto error; + } + + ret = dwarf_lineno(line, &line_no); + if (ret) { + goto error; + } + + _src_loc->line_no = line_no; + _src_loc->filename = strdup(filename); + } + + bt_dwarf_die_destroy(die); + + if (_src_loc) { + *src_loc = _src_loc; + } + + return 0; + +error: + source_location_destroy(_src_loc); + bt_dwarf_die_destroy(die); + return -1; +} + +/** + * Get the source location (file name and line number) for a given + * address within a compile unit (CU). + * + * On success, the out parameter `src_loc` is set if found. On + * failure, it remains unchanged. + * + * @param cu bt_dwarf_cu instance for the compile unit which + * may contain the address + * @param addr Virtual memory address for which to find the + * source location + * @param src_loc Out parameter, the source location + * @returns 0 on success, -1 on failure + */ +static +int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu *cu, uint64_t addr, + struct source_location **src_loc) +{ + int ret = 0; + struct source_location *_src_loc = NULL; + + if (!cu || !src_loc) { + goto error; + } + + ret = bin_info_lookup_cu_src_loc_inl(cu, addr, &_src_loc); + if (ret) { + goto error; + } + + if (_src_loc) { + goto end; + } + + ret = bin_info_lookup_cu_src_loc_no_inl(cu, addr, &_src_loc); + if (ret) { + goto error; + } + + if (_src_loc) { + goto end; + } + +end: + if (_src_loc) { + *src_loc = _src_loc; + } + + return 0; + +error: + source_location_destroy(_src_loc); + return -1; +} + +BT_HIDDEN +int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr, + struct source_location **src_loc) +{ + struct bt_dwarf_cu *cu = NULL; + struct source_location *_src_loc = NULL; + + if (!bin || !src_loc) { + goto error; + } + + /* Set DWARF info if it hasn't been accessed yet. */ + if (!bin->dwarf_info && !bin->is_elf_only) { + if (bin_info_set_dwarf_info(bin)) { + /* Failed to set DWARF info. */ + bin->is_elf_only = true; + } + } + + if (bin->is_elf_only) { + /* We cannot lookup source location without DWARF info. */ + goto error; + } + + if (!bin_info_has_address(bin, addr)) { + goto error; + } + + /* + * Addresses in ELF and DWARF are relative to base address for + * PIC, so make the address argument relative too if needed. + */ + if (bin->is_pic) { + addr -= bin->low_addr; + } + + cu = bt_dwarf_cu_create(bin->dwarf_info); + if (!cu) { + goto error; + } + + while (bt_dwarf_cu_next(cu) == 0) { + int ret; + + ret = bin_info_lookup_cu_src_loc(cu, addr, &_src_loc); + if (ret) { + goto error; + } + + if (_src_loc) { + break; + } + } + + bt_dwarf_cu_destroy(cu); + if (_src_loc) { + *src_loc = _src_loc; + } + + return 0; + +error: + source_location_destroy(_src_loc); + bt_dwarf_cu_destroy(cu); + return -1; +} diff --git a/lib/debuginfo.c b/lib/debuginfo.c index 40514f25..22930791 100644 --- a/lib/debuginfo.c +++ b/lib/debuginfo.c @@ -30,16 +30,16 @@ #include #include #include -#include +#include #include #include struct proc_debug_info_sources { /* - * Hash table: base address (pointer to uint64_t) to so info; owned by + * Hash table: base address (pointer to uint64_t) to bin info; owned by * proc_debug_info_sources. */ - GHashTable *baddr_to_so_info; + GHashTable *baddr_to_bin_info; /* * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *); @@ -74,7 +74,7 @@ int debug_info_init(struct debug_info *info) "lttng_ust_statedump:start"); info->q_dl_open = g_quark_from_string("lttng_ust_dl:dlopen"); - return so_info_init(); + return bin_info_init(); } static @@ -92,7 +92,7 @@ void debug_info_source_destroy(struct debug_info_source *debug_info_src) } static -struct debug_info_source *debug_info_source_create_from_so(struct so_info *so, +struct debug_info_source *debug_info_source_create_from_bin(struct bin_info *bin, uint64_t ip) { int ret; @@ -106,15 +106,15 @@ struct debug_info_source *debug_info_source_create_from_so(struct so_info *so, } /* Lookup function name */ - ret = so_info_lookup_function_name(so, ip, &debug_info_src->func); + ret = bin_info_lookup_function_name(bin, ip, &debug_info_src->func); if (ret) { goto error; } /* Can't retrieve src_loc from ELF only, skip it */ - if (!so->is_elf_only) { + if (!bin->is_elf_only) { /* Lookup source location */ - ret = so_info_lookup_source_location(so, ip, &src_loc); + ret = bin_info_lookup_source_location(bin, ip, &src_loc); if (ret) { goto error; } @@ -136,8 +136,8 @@ struct debug_info_source *debug_info_source_create_from_so(struct so_info *so, source_location_destroy(src_loc); } - if (so->elf_path) { - debug_info_src->bin_path = strdup(so->elf_path); + if (bin->elf_path) { + debug_info_src->bin_path = strdup(bin->elf_path); if (!debug_info_src->bin_path) { goto error; } @@ -145,7 +145,7 @@ struct debug_info_source *debug_info_source_create_from_so(struct so_info *so, debug_info_src->short_bin_path = get_filename_from_path( debug_info_src->bin_path); - ret = so_info_get_bin_loc(so, ip, &(debug_info_src->bin_loc)); + ret = bin_info_get_bin_loc(bin, ip, &(debug_info_src->bin_loc)); if (ret) { goto error; } @@ -167,8 +167,8 @@ void proc_debug_info_sources_destroy( return; } - if (proc_dbg_info_src->baddr_to_so_info) { - g_hash_table_destroy(proc_dbg_info_src->baddr_to_so_info); + if (proc_dbg_info_src->baddr_to_bin_info) { + g_hash_table_destroy(proc_dbg_info_src->baddr_to_bin_info); } if (proc_dbg_info_src->ip_to_debug_info_src) { @@ -188,10 +188,10 @@ struct proc_debug_info_sources *proc_debug_info_sources_create(void) goto end; } - proc_dbg_info_src->baddr_to_so_info = g_hash_table_new_full( + proc_dbg_info_src->baddr_to_bin_info = g_hash_table_new_full( g_int64_hash, g_int64_equal, (GDestroyNotify) g_free, - (GDestroyNotify) so_info_destroy); - if (!proc_dbg_info_src->baddr_to_so_info) { + (GDestroyNotify) bin_info_destroy); + if (!proc_dbg_info_src->baddr_to_bin_info) { goto error; } @@ -266,14 +266,14 @@ struct debug_info_source *proc_debug_info_sources_get_entry( goto end; } - /* Check in all so_infos. */ - g_hash_table_iter_init(&iter, proc_dbg_info_src->baddr_to_so_info); + /* Check in all bin_infos. */ + g_hash_table_iter_init(&iter, proc_dbg_info_src->baddr_to_bin_info); while (g_hash_table_iter_next(&iter, &baddr, &value)) { - struct so_info *so = value; + struct bin_info *bin = value; - if (!so_info_has_address(value, ip)) { + if (!bin_info_has_address(value, ip)) { continue; } @@ -284,7 +284,7 @@ struct debug_info_source *proc_debug_info_sources_get_entry( * a caching policy), and entries should be prunned when * libraries are unmapped. */ - debug_info_src = debug_info_source_create_from_so(so, ip); + debug_info_src = debug_info_source_create_from_bin(bin, ip); if (debug_info_src) { g_hash_table_insert( proc_dbg_info_src->ip_to_debug_info_src, @@ -380,7 +380,7 @@ void handle_statedump_build_id_event(struct debug_info *debug_info, struct bt_definition *vpid_def = NULL; struct bt_definition *build_id_def = NULL; struct definition_sequence *build_id_seq; - struct so_info *so = NULL; + struct bin_info *bin = NULL; int i; int64_t vpid; uint64_t baddr; @@ -447,17 +447,17 @@ void handle_statedump_build_id_event(struct debug_info *debug_info, goto end; } - so = g_hash_table_lookup(proc_dbg_info_src->baddr_to_so_info, + bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info, (gpointer) &baddr); - if (!so) { + if (!bin) { /* - * The build_id event comes after the so has been + * The build_id event comes after the bin has been * created. If it isn't found, just ignore this event. */ goto end; } - so_info_set_build_id(so, build_id, build_id_len); + bin_info_set_build_id(bin, build_id, build_id_len); end: free(build_id); @@ -475,7 +475,7 @@ void handle_statedump_debug_link_event(struct debug_info *debug_info, struct bt_definition *vpid_def = NULL; struct bt_definition *filename_def = NULL; struct bt_definition *crc32_def = NULL; - struct so_info *so = NULL; + struct bin_info *bin = NULL; int64_t vpid; uint64_t baddr; char *filename = NULL; @@ -534,11 +534,11 @@ void handle_statedump_debug_link_event(struct debug_info *debug_info, goto end; } - so = g_hash_table_lookup(proc_dbg_info_src->baddr_to_so_info, + bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info, (gpointer) &baddr); - if (!so) { + if (!bin) { /* - * The debug_link event comes after the so has been + * The debug_link event comes after the bin has been * created. If it isn't found, just ignore this event. */ goto end; @@ -547,7 +547,7 @@ void handle_statedump_debug_link_event(struct debug_info *debug_info, filename = bt_get_string(filename_def); crc32 = bt_get_unsigned_int(crc32_def); - so_info_set_debug_link(so, filename, crc32); + bin_info_set_debug_link(bin, filename, crc32); end: return; @@ -565,7 +565,7 @@ void handle_bin_info_event(struct debug_info *debug_info, struct bt_definition *event_fields_def = NULL; struct bt_definition *sec_def = NULL; struct proc_debug_info_sources *proc_dbg_info_src; - struct so_info *so; + struct bin_info *bin; uint64_t baddr, memsz; int64_t vpid; const char *path; @@ -662,19 +662,19 @@ void handle_bin_info_event(struct debug_info *debug_info, *((uint64_t *) key) = baddr; - so = g_hash_table_lookup(proc_dbg_info_src->baddr_to_so_info, + bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info, key); - if (so) { + if (bin) { goto end; } - so = so_info_create(path, baddr, memsz, is_pic); - if (!so) { + bin = bin_info_create(path, baddr, memsz, is_pic); + if (!bin) { goto end; } - g_hash_table_insert(proc_dbg_info_src->baddr_to_so_info, - key, so); + g_hash_table_insert(proc_dbg_info_src->baddr_to_bin_info, + key, bin); /* Ownership passed to ht. */ key = NULL; @@ -726,7 +726,7 @@ void handle_statedump_start(struct debug_info *debug_info, goto end; } - g_hash_table_remove_all(proc_dbg_info_src->baddr_to_so_info); + g_hash_table_remove_all(proc_dbg_info_src->baddr_to_bin_info); g_hash_table_remove_all(proc_dbg_info_src->ip_to_debug_info_src); end: diff --git a/lib/so-info.c b/lib/so-info.c deleted file mode 100644 index a5c38604..00000000 --- a/lib/so-info.c +++ /dev/null @@ -1,1325 +0,0 @@ -/* - * so-info.c - * - * Babeltrace - Executable and Shared Object Debug Info Reader - * - * Copyright 2015 Antoine Busque - * - * Author: Antoine Busque - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* - * An address printed in hex is at most 20 bytes (16 for 64-bits + - * leading 0x + optional leading '+' if addr is an offset + null - * character). - */ -#define ADDR_STR_LEN 20 - -BT_HIDDEN -int so_info_init(void) -{ - int ret = 0; - - if (elf_version(EV_CURRENT) == EV_NONE) { - printf_debug("ELF library initialization failed: %s\n", - elf_errmsg(-1)); - ret = -1; - } - - return ret; -} - -BT_HIDDEN -struct so_info *so_info_create(const char *path, uint64_t low_addr, - uint64_t memsz, bool is_pic) -{ - struct so_info *so = NULL; - - if (!path) { - goto error; - } - - so = g_new0(struct so_info, 1); - if (!so) { - goto error; - } - - if (opt_debug_info_target_prefix) { - so->elf_path = g_build_path("/", opt_debug_info_target_prefix, - path, NULL); - } else { - so->elf_path = strdup(path); - } - - if (!so->elf_path) { - goto error; - } - - so->is_pic = is_pic; - so->memsz = memsz; - so->low_addr = low_addr; - so->high_addr = so->low_addr + so->memsz; - - return so; - -error: - so_info_destroy(so); - return NULL; -} - -BT_HIDDEN -void so_info_destroy(struct so_info *so) -{ - if (!so) { - return; - } - - dwarf_end(so->dwarf_info); - - free(so->elf_path); - free(so->dwarf_path); - free(so->build_id); - free(so->dbg_link_filename); - - elf_end(so->elf_file); - - close(so->elf_fd); - close(so->dwarf_fd); - - g_free(so); -} - - -BT_HIDDEN -int so_info_set_build_id(struct so_info *so, uint8_t *build_id, - size_t build_id_len) -{ - if (!so || !build_id) { - goto error; - } - - so->build_id = malloc(build_id_len); - if (!so->build_id) { - goto error; - } - - memcpy(so->build_id, build_id, build_id_len); - so->build_id_len = build_id_len; - - /* - * Reset the is_elf_only flag in case it had been set - * previously, because we might find separate debug info using - * the new build id information. - */ - so->is_elf_only = false; - - return 0; - -error: - - return -1; -} - -BT_HIDDEN -int so_info_set_debug_link(struct so_info *so, char *filename, uint32_t crc) -{ - if (!so || !filename) { - goto error; - } - - so->dbg_link_filename = strdup(filename); - if (!so->dbg_link_filename) { - goto error; - } - - so->dbg_link_crc = crc; - - /* - * Reset the is_elf_only flag in case it had been set - * previously, because we might find separate debug info using - * the new build id information. - */ - so->is_elf_only = false; - - return 0; - -error: - - return -1; -} - -/** - * Tries to read DWARF info from the location given by path, and - * attach it to the given so_info instance if it exists. - * - * @param so so_info instance for which to set DWARF info - * @param path Presumed location of the DWARF info - * @returns 0 on success, -1 on failure - */ -static -int so_info_set_dwarf_info_from_path(struct so_info *so, char *path) -{ - int fd = -1, ret = 0; - struct bt_dwarf_cu *cu = NULL; - Dwarf *dwarf_info = NULL; - - if (!so || !path) { - goto error; - } - - fd = open(path, O_RDONLY); - if (fd < 0) { - goto error; - } - - dwarf_info = dwarf_begin(fd, DWARF_C_READ); - if (!dwarf_info) { - goto error; - } - - /* - * Check if the dwarf info has any CU. If not, the SO's object - * file contains no DWARF info. - */ - cu = bt_dwarf_cu_create(dwarf_info); - if (!cu) { - goto error; - } - - ret = bt_dwarf_cu_next(cu); - if (ret) { - goto error; - } - - so->dwarf_fd = fd; - so->dwarf_path = strdup(path); - if (!so->dwarf_path) { - goto error; - } - so->dwarf_info = dwarf_info; - free(cu); - - return 0; - -error: - close(fd); - dwarf_end(dwarf_info); - g_free(dwarf_info); - free(cu); - - return -1; -} - -/** - * Try to set the dwarf_info for a given so_info instance via the - * build ID method. - * - * @param so so_info instance for which to retrieve the - * DWARF info via build ID - * @returns 0 on success (i.e. dwarf_info set), -1 on failure - */ -static -int so_info_set_dwarf_info_build_id(struct so_info *so) -{ - int i = 0, ret = 0; - char *path = NULL, *build_id_file = NULL; - const char *dbg_dir = NULL; - size_t build_id_file_len; - - if (!so || !so->build_id) { - goto error; - } - - dbg_dir = opt_debug_info_dir ? : DEFAULT_DEBUG_DIR; - - /* 2 characters per byte printed in hex, +1 for '/' and +1 for - * '\0' */ - build_id_file_len = (2 * so->build_id_len) + 1 + - strlen(BUILD_ID_SUFFIX) + 1; - build_id_file = malloc(build_id_file_len); - if (!build_id_file) { - goto error; - } - - snprintf(build_id_file, 4, "%02x/", so->build_id[0]); - for (i = 1; i < so->build_id_len; ++i) { - int path_idx = 3 + 2 * (i - 1); - - snprintf(&build_id_file[path_idx], 3, "%02x", so->build_id[i]); - } - strcat(build_id_file, BUILD_ID_SUFFIX); - - path = g_build_path("/", dbg_dir, BUILD_ID_SUBDIR, build_id_file, NULL); - if (!path) { - goto error; - } - - ret = so_info_set_dwarf_info_from_path(so, path); - if (ret) { - goto error; - } - - goto end; - -error: - ret = -1; -end: - free(build_id_file); - free(path); - - return ret; -} - -/** - * Tests whether the file located at path exists and has the expected - * checksum. - * - * This predicate is used when looking up separate debug info via the - * GNU debuglink method. The expected crc can be found .gnu_debuglink - * section in the original ELF file, along with the filename for the - * file containing the debug info. - * - * @param path Full path at which to look for the debug file - * @param crc Expected checksum for the debug file - * @returns 1 if the file exists and has the correct checksum, - * 0 otherwise - */ -static -int is_valid_debug_file(char *path, uint32_t crc) -{ - int ret = 0, fd = -1; - uint32_t _crc = 0; - - if (!path) { - goto end; - } - - fd = open(path, O_RDONLY); - if (fd < 0) { - goto end; - } - - ret = crc32(fd, &_crc); - if (ret) { - ret = 0; - goto end; - } - - ret = (crc == _crc); - -end: - close(fd); - return ret; -} - -/** - * Try to set the dwarf_info for a given so_info instance via the - * build ID method. - * - * @param so so_info instance for which to retrieve the - * DWARF info via debug link - * @returns 0 on success (i.e. dwarf_info set), -1 on failure - */ -static -int so_info_set_dwarf_info_debug_link(struct so_info *so) -{ - int ret = 0; - const char *dbg_dir = NULL; - char *dir_name = NULL, *so_dir = NULL, *path = NULL; - size_t max_path_len = 0; - - if (!so || !so->dbg_link_filename) { - goto error; - } - - dbg_dir = opt_debug_info_dir ? : DEFAULT_DEBUG_DIR; - - dir_name = dirname(so->elf_path); - if (!dir_name) { - goto error; - } - - /* so_dir is just dir_name with a trailing slash */ - so_dir = malloc(strlen(dir_name) + 2); - if (!so_dir) { - goto error; - } - - strcpy(so_dir, dir_name); - strcat(so_dir, "/"); - - max_path_len = strlen(dbg_dir) + strlen(so_dir) + - strlen(DEBUG_SUBDIR) + strlen(so->dbg_link_filename) - + 1; - path = malloc(max_path_len); - if (!path) { - goto error; - } - - /* First look in the SO's dir */ - strcpy(path, so_dir); - strcat(path, so->dbg_link_filename); - - if (is_valid_debug_file(path, so->dbg_link_crc)) { - goto found; - } - - /* If not found, look in .debug subdir */ - strcpy(path, so_dir); - strcat(path, DEBUG_SUBDIR); - strcat(path, so->dbg_link_filename); - - if (is_valid_debug_file(path, so->dbg_link_crc)) { - goto found; - } - - /* Lastly, look under the global debug directory */ - strcpy(path, dbg_dir); - strcat(path, so_dir); - strcat(path, so->dbg_link_filename); - - if (is_valid_debug_file(path, so->dbg_link_crc)) { - goto found; - } - -error: - ret = -1; -end: - free(path); - free(so_dir); - - return ret; - -found: - ret = so_info_set_dwarf_info_from_path(so, path); - if (ret) { - goto error; - } - - goto end; -} - -/** - * Initialize the DWARF info for a given executable. - * - * @param so so_info instance - * @returns 0 on success, -1 on failure - */ -static -int so_info_set_dwarf_info(struct so_info *so) -{ - int ret = 0; - - if (!so) { - goto error; - } - - /* First try to set the DWARF info from the ELF file */ - ret = so_info_set_dwarf_info_from_path(so, so->elf_path); - if (!ret) { - goto end; - } - - /* - * If that fails, try to find separate debug info via build ID - * and debug link. - */ - ret = so_info_set_dwarf_info_build_id(so); - if (!ret) { - goto end; - } - - ret = so_info_set_dwarf_info_debug_link(so); - if (!ret) { - goto end; - } - -error: - ret = -1; -end: - return ret; -} - -/** - * Initialize the ELF file for a given executable. - * - * @param so so_info instance - * @returns 0 on success, -1 on failure - */ -static -int so_info_set_elf_file(struct so_info *so) -{ - int elf_fd; - Elf *elf_file = NULL; - - if (!so) { - goto error; - } - - elf_fd = open(so->elf_path, O_RDONLY); - if (elf_fd < 0) { - printf_verbose("Failed to open %s\n", so->elf_path); - goto error; - } - - elf_file = elf_begin(elf_fd, ELF_C_READ, NULL); - if (!elf_file) { - printf_debug("elf_begin failed: %s\n", elf_errmsg(-1)); - goto error; - } - - if (elf_kind(elf_file) != ELF_K_ELF) { - printf_verbose("Error: %s is not an ELF object\n", - so->elf_path); - goto error; - } - - so->elf_fd = elf_fd; - so->elf_file = elf_file; - return 0; - -error: - close(elf_fd); - elf_end(elf_file); - return -1; -} - - -BT_HIDDEN -void source_location_destroy(struct source_location *src_loc) -{ - if (!src_loc) { - return; - } - - free(src_loc->filename); - g_free(src_loc); -} -/** - * Append a string representation of an address offset to an existing - * string. - * - * On success, the out parameter `result` will contain the base string - * followed by the offset string of the form "+0x1234". On failure, - * `result` remains unchanged. - * - * @param base_str The string to which to append an offset string - * @param low_addr The lower virtual memory address, the base from - * which the offset is computed - * @param high_addr The higher virtual memory address - * @param result Out parameter, the base string followed by the - * offset string - * @returns 0 on success, -1 on failure - */ -static -int so_info_append_offset_str(const char *base_str, uint64_t low_addr, - uint64_t high_addr, char **result) -{ - int ret; - uint64_t offset; - char *_result = NULL; - char offset_str[ADDR_STR_LEN]; - - if (!base_str || !result) { - goto error; - } - - offset = high_addr - low_addr; - - _result = malloc(strlen(base_str) + ADDR_STR_LEN); - if (!_result) { - goto error; - } - - ret = snprintf(offset_str, ADDR_STR_LEN, "+%#0" PRIx64, offset); - if (ret < 0) { - goto error; - } - strcpy(_result, base_str); - strcat(_result, offset_str); - *result = _result; - - return 0; - -error: - free(_result); - return -1; -} - -/** - * Try to find the symbol closest to an address within a given ELF - * section. - * - * Only function symbols are taken into account. The symbol's address - * must precede `addr`. A symbol with a closer address might exist - * after `addr` but is irrelevant because it cannot encompass `addr`. - * - * On success, if found, the out parameters `sym` and `shdr` are - * set. On failure or if none are found, they remain unchanged. - * - * @param scn ELF section in which to look for the address - * @param addr Virtual memory address for which to find the - * nearest function symbol - * @param sym Out parameter, the nearest function symbol - * @param shdr Out parameter, the section header for scn - * @returns 0 on success, -1 on failure - */ -static -int so_info_get_nearest_symbol_from_section(Elf_Scn *scn, uint64_t addr, - GElf_Sym **sym, GElf_Shdr **shdr) -{ - int i; - size_t symbol_count; - Elf_Data *data = NULL; - GElf_Shdr *_shdr = NULL; - GElf_Sym *nearest_sym = NULL; - - if (!scn || !sym || !shdr) { - goto error; - } - - _shdr = g_new0(GElf_Shdr, 1); - if (!_shdr) { - goto error; - } - - _shdr = gelf_getshdr(scn, _shdr); - if (!_shdr) { - goto error; - } - - if (_shdr->sh_type != SHT_SYMTAB) { - /* - * We are only interested in symbol table (symtab) - * sections, skip this one. - */ - goto end; - } - - data = elf_getdata(scn, NULL); - if (!data) { - goto error; - } - - symbol_count = _shdr->sh_size / _shdr->sh_entsize; - - for (i = 0; i < symbol_count; ++i) { - GElf_Sym *cur_sym = NULL; - - cur_sym = g_new0(GElf_Sym, 1); - if (!cur_sym) { - goto error; - } - cur_sym = gelf_getsym(data, i, cur_sym); - if (!cur_sym) { - goto error; - } - if (GELF_ST_TYPE(cur_sym->st_info) != STT_FUNC) { - /* We're only interested in the functions. */ - g_free(cur_sym); - continue; - } - - if (cur_sym->st_value <= addr && - (!nearest_sym || - cur_sym->st_value > nearest_sym->st_value)) { - g_free(nearest_sym); - nearest_sym = cur_sym; - } else { - g_free(cur_sym); - } - } - -end: - if (nearest_sym) { - *sym = nearest_sym; - *shdr = _shdr; - } else { - g_free(_shdr); - } - - return 0; - -error: - g_free(nearest_sym); - g_free(_shdr); - return -1; -} - -/** - * Get the name of the function containing a given address within an - * executable using ELF symbols. - * - * The function name is in fact the name of the nearest ELF symbol, - * followed by the offset in bytes between the address and the symbol - * (in hex), separated by a '+' character. - * - * If found, the out parameter `func_name` is set on success. On failure, - * it remains unchanged. - * - * @param so so_info instance for the executable containing - * the address - * @param addr Virtual memory address for which to find the - * function name - * @param func_name Out parameter, the function name - * @returns 0 on success, -1 on failure - */ -static -int so_info_lookup_elf_function_name(struct so_info *so, uint64_t addr, - char **func_name) -{ - /* - * TODO (possible optimisation): if an ELF has no symtab - * section, it has been stripped. Therefore, it would be wise - * to store a flag indicating the stripped status after the - * first iteration to prevent subsequent ones. - */ - int ret = 0; - Elf_Scn *scn = NULL; - GElf_Sym *sym = NULL; - GElf_Shdr *shdr = NULL; - char *sym_name = NULL; - - /* Set ELF file if it hasn't been accessed yet. */ - if (!so->elf_file) { - ret = so_info_set_elf_file(so); - if (ret) { - /* Failed to set ELF file. */ - goto error; - } - } - - scn = elf_nextscn(so->elf_file, scn); - if (!scn) { - goto error; - } - - while (scn && !sym) { - ret = so_info_get_nearest_symbol_from_section( - scn, addr, &sym, &shdr); - if (ret) { - goto error; - } - - scn = elf_nextscn(so->elf_file, scn); - } - - if (sym) { - sym_name = elf_strptr(so->elf_file, shdr->sh_link, - sym->st_name); - if (!sym_name) { - goto error; - } - - ret = so_info_append_offset_str(sym_name, sym->st_value, addr, - func_name); - if (ret) { - goto error; - } - } - - g_free(shdr); - g_free(sym); - return 0; - -error: - g_free(shdr); - g_free(sym); - return -1; -} - -/** - * Get the name of the function containing a given address within a - * given compile unit (CU). - * - * If found, the out parameter `func_name` is set on success. On - * failure, it remains unchanged. - * - * @param cu bt_dwarf_cu instance which may contain the address - * @param addr Virtual memory address for which to find the - * function name - * @param func_name Out parameter, the function name - * @returns 0 on success, -1 on failure - */ -static -int so_info_lookup_cu_function_name(struct bt_dwarf_cu *cu, uint64_t addr, - char **func_name) -{ - int ret = 0, found = 0; - struct bt_dwarf_die *die = NULL; - - if (!cu || !func_name) { - goto error; - } - - die = bt_dwarf_die_create(cu); - if (!die) { - goto error; - } - - while (bt_dwarf_die_next(die) == 0) { - int tag; - - ret = bt_dwarf_die_get_tag(die, &tag); - if (ret) { - goto error; - } - - if (tag == DW_TAG_subprogram) { - ret = bt_dwarf_die_contains_addr(die, addr, &found); - if (ret) { - goto error; - } - - if (found) { - break; - } - } - } - - if (found) { - uint64_t low_addr = 0; - char *die_name = NULL; - - ret = bt_dwarf_die_get_name(die, &die_name); - if (ret) { - goto error; - } - - ret = dwarf_lowpc(die->dwarf_die, &low_addr); - if (ret) { - goto error; - } - - ret = so_info_append_offset_str(die_name, low_addr, addr, - func_name); - if (ret) { - goto error; - } - } - - bt_dwarf_die_destroy(die); - return 0; - -error: - bt_dwarf_die_destroy(die); - return -1; -} - -/** - * Get the name of the function containing a given address within an - * executable using DWARF debug info. - * - * If found, the out parameter `func_name` is set on success. On - * failure, it remains unchanged. - * - * @param so so_info instance for the executable containing - * the address - * @param addr Virtual memory address for which to find the - * function name - * @param func_name Out parameter, the function name - * @returns 0 on success, -1 on failure - */ -static -int so_info_lookup_dwarf_function_name(struct so_info *so, uint64_t addr, - char **func_name) -{ - int ret = 0; - char *_func_name = NULL; - struct bt_dwarf_cu *cu = NULL; - - if (!so || !func_name) { - goto error; - } - - cu = bt_dwarf_cu_create(so->dwarf_info); - if (!cu) { - goto error; - } - - while (bt_dwarf_cu_next(cu) == 0) { - ret = so_info_lookup_cu_function_name(cu, addr, &_func_name); - if (ret) { - goto error; - } - - if (_func_name) { - break; - } - } - - if (_func_name) { - *func_name = _func_name; - } - - bt_dwarf_cu_destroy(cu); - return 0; - -error: - bt_dwarf_cu_destroy(cu); - return -1; -} - -BT_HIDDEN -int so_info_lookup_function_name(struct so_info *so, uint64_t addr, - char **func_name) -{ - int ret = 0; - char *_func_name = NULL; - - if (!so || !func_name) { - goto error; - } - - /* Set DWARF info if it hasn't been accessed yet. */ - if (!so->dwarf_info && !so->is_elf_only) { - ret = so_info_set_dwarf_info(so); - if (ret) { - /* Failed to set DWARF info, fallback to ELF. */ - so->is_elf_only = true; - } - } - - if (!so_info_has_address(so, addr)) { - goto error; - } - - /* - * Addresses in ELF and DWARF are relative to base address for - * PIC, so make the address argument relative too if needed. - */ - if (so->is_pic) { - addr -= so->low_addr; - } - - if (so->is_elf_only) { - ret = so_info_lookup_elf_function_name(so, addr, &_func_name); - } else { - ret = so_info_lookup_dwarf_function_name(so, addr, &_func_name); - } - - if (ret || !_func_name) { - goto error; - } - - *func_name = _func_name; - return 0; - -error: - return -1; -} - -BT_HIDDEN -int so_info_get_bin_loc(struct so_info *so, uint64_t addr, char **bin_loc) -{ - int ret = 0; - char *_bin_loc = NULL; - - if (!so || !bin_loc) { - goto error; - } - - if (so->is_pic) { - addr -= so->low_addr; - ret = asprintf(&_bin_loc, "+%#0" PRIx64, addr); - } else { - ret = asprintf(&_bin_loc, "@%#0" PRIx64, addr); - } - - if (ret == -1 || !_bin_loc) { - goto error; - } - - *bin_loc = _bin_loc; - return 0; - -error: - return -1; -} - -/** - * Predicate used to determine whether the children of a given DIE - * contain a specific address. - * - * More specifically, the parameter `die` is expected to be a - * subprogram (function) DIE, and this predicate tells whether any - * subroutines are inlined within this function and would contain - * `addr`. - * - * Do note that this function advances the position of `die`. If the - * address is found within one of its children, `die` will be pointing - * to that child upon returning from the function, allowing to extract - * the information deemed necessary. - * - * @param die The parent DIE in whose children the address will be - * looked for - * @param addr The address for which to look for in the DIEs - * @returns Returns 1 if the address was found, 0 if not - */ -static -int so_info_child_die_has_address(struct bt_dwarf_die *die, uint64_t addr) -{ - int ret = 0, contains = 0; - - if (!die) { - goto error; - } - - ret = bt_dwarf_die_child(die); - if (ret) { - goto error; - } - - do { - int tag; - - ret = bt_dwarf_die_get_tag(die, &tag); - if (ret) { - goto error; - } - - if (tag == DW_TAG_inlined_subroutine) { - ret = bt_dwarf_die_contains_addr(die, addr, &contains); - if (ret) { - goto error; - } - - if (contains) { - ret = 1; - goto end; - } - } - } while (bt_dwarf_die_next(die) == 0); - -end: - return ret; - -error: - ret = 0; - goto end; -} - -/** - * Lookup the source location for a given address within a CU, making - * the assumption that it is contained within an inline routine in a - * function. - * - * @param cu bt_dwarf_cu instance in which to look for the address - * @param addr The address for which to look for - * @param src_loc Out parameter, the source location (filename and - * line number) for the address - * @returns 0 on success, -1 on failure - */ -static -int so_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu *cu, uint64_t addr, - struct source_location **src_loc) -{ - int ret = 0, found = 0; - struct bt_dwarf_die *die = NULL; - struct source_location *_src_loc = NULL; - - if (!cu || !src_loc) { - goto error; - } - - die = bt_dwarf_die_create(cu); - if (!die) { - goto error; - } - - while (bt_dwarf_die_next(die) == 0) { - int tag; - - ret = bt_dwarf_die_get_tag(die, &tag); - if (ret) { - goto error; - } - - if (tag == DW_TAG_subprogram) { - int contains = 0; - - ret = bt_dwarf_die_contains_addr(die, addr, &contains); - if (ret) { - goto error; - } - - if (contains) { - /* - * Try to find an inlined subroutine - * child of this DIE containing addr. - */ - found = so_info_child_die_has_address( - die, addr); - goto end; - } - } - } - -end: - if (found) { - char *filename = NULL; - uint64_t line_no; - - _src_loc = g_new0(struct source_location, 1); - if (!_src_loc) { - goto error; - } - - ret = bt_dwarf_die_get_call_file(die, &filename); - if (ret) { - goto error; - } - ret = bt_dwarf_die_get_call_line(die, &line_no); - if (ret) { - free(filename); - goto error; - } - - _src_loc->filename = filename; - _src_loc->line_no = line_no; - *src_loc = _src_loc; - } - - bt_dwarf_die_destroy(die); - return 0; - -error: - source_location_destroy(_src_loc); - bt_dwarf_die_destroy(die); - return -1; -} - -/** - * Lookup the source location for a given address within a CU, - * assuming that it is contained within an inlined function. - * - * A source location can be found regardless of inlining status for - * this method, but in the case of an inlined function, the returned - * source location will point not to the callsite but rather to the - * definition site of the inline function. - * - * @param cu bt_dwarf_cu instance in which to look for the address - * @param addr The address for which to look for - * @param src_loc Out parameter, the source location (filename and - * line number) for the address - * @returns 0 on success, -1 on failure - */ -static -int so_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu *cu, uint64_t addr, - struct source_location **src_loc) -{ - struct source_location *_src_loc = NULL; - struct bt_dwarf_die *die = NULL; - const char *filename = NULL; - Dwarf_Line *line = NULL; - Dwarf_Addr line_addr; - int ret, line_no; - - if (!cu || !src_loc) { - goto error; - } - - die = bt_dwarf_die_create(cu); - if (!die) { - goto error; - } - - line = dwarf_getsrc_die(die->dwarf_die, addr); - if (!line) { - goto error; - } - - ret = dwarf_lineaddr(line, &line_addr); - if (ret) { - goto error; - } - - filename = dwarf_linesrc(line, NULL, NULL); - if (!filename) { - goto error; - } - - if (addr == line_addr) { - _src_loc = g_new0(struct source_location, 1); - if (!_src_loc) { - goto error; - } - - ret = dwarf_lineno(line, &line_no); - if (ret) { - goto error; - } - - _src_loc->line_no = line_no; - _src_loc->filename = strdup(filename); - } - - bt_dwarf_die_destroy(die); - - if (_src_loc) { - *src_loc = _src_loc; - } - - return 0; - -error: - source_location_destroy(_src_loc); - bt_dwarf_die_destroy(die); - return -1; -} - -/** - * Get the source location (file name and line number) for a given - * address within a compile unit (CU). - * - * On success, the out parameter `src_loc` is set if found. On - * failure, it remains unchanged. - * - * @param so bt_dwarf_cu instance for the compile unit which - * may contain the address - * @param addr Virtual memory address for which to find the - * source location - * @param src_loc Out parameter, the source location - * @returns 0 on success, -1 on failure - */ -static -int so_info_lookup_cu_src_loc(struct bt_dwarf_cu *cu, uint64_t addr, - struct source_location **src_loc) -{ - int ret = 0; - struct source_location *_src_loc = NULL; - - if (!cu || !src_loc) { - goto error; - } - - ret = so_info_lookup_cu_src_loc_inl(cu, addr, &_src_loc); - if (ret) { - goto error; - } - - if (_src_loc) { - goto end; - } - - ret = so_info_lookup_cu_src_loc_no_inl(cu, addr, &_src_loc); - if (ret) { - goto error; - } - - if (_src_loc) { - goto end; - } - -end: - if (_src_loc) { - *src_loc = _src_loc; - } - - return 0; - -error: - source_location_destroy(_src_loc); - return -1; -} - -BT_HIDDEN -int so_info_lookup_source_location(struct so_info *so, uint64_t addr, - struct source_location **src_loc) -{ - struct bt_dwarf_cu *cu = NULL; - struct source_location *_src_loc = NULL; - - if (!so || !src_loc) { - goto error; - } - - /* Set DWARF info if it hasn't been accessed yet. */ - if (!so->dwarf_info && !so->is_elf_only) { - if (so_info_set_dwarf_info(so)) { - /* Failed to set DWARF info. */ - so->is_elf_only = true; - } - } - - if (so->is_elf_only) { - /* We cannot lookup source location without DWARF info. */ - goto error; - } - - if (!so_info_has_address(so, addr)) { - goto error; - } - - /* - * Addresses in ELF and DWARF are relative to base address for - * PIC, so make the address argument relative too if needed. - */ - if (so->is_pic) { - addr -= so->low_addr; - } - - cu = bt_dwarf_cu_create(so->dwarf_info); - if (!cu) { - goto error; - } - - while (bt_dwarf_cu_next(cu) == 0) { - int ret; - - ret = so_info_lookup_cu_src_loc(cu, addr, &_src_loc); - if (ret) { - goto error; - } - - if (_src_loc) { - break; - } - } - - bt_dwarf_cu_destroy(cu); - if (_src_loc) { - *src_loc = _src_loc; - } - - return 0; - -error: - source_location_destroy(_src_loc); - bt_dwarf_cu_destroy(cu); - return -1; -} diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am index 9bb50605..f55d88a1 100644 --- a/tests/lib/Makefile.am +++ b/tests/lib/Makefile.am @@ -39,14 +39,14 @@ test_dwarf_LDADD = $(LIBTAP) \ $(top_builddir)/lib/libdebuginfo.la test_dwarf_SOURCES = test_dwarf.c -test_so_info_LDFLAGS = -static -test_so_info_LDADD = $(LIBTAP) \ +test_bin_info_LDFLAGS = -static +test_bin_info_LDADD = $(LIBTAP) \ $(top_builddir)/lib/libbabeltrace.la \ $(top_builddir)/lib/libdebuginfo.la -test_so_info_SOURCES = test_so_info.c +test_bin_info_SOURCES = test_bin_info.c -noinst_PROGRAMS += test_dwarf test_so_info -SCRIPT_LIST += test_dwarf_complete test_so_info_complete +noinst_PROGRAMS += test_dwarf test_bin_info +SCRIPT_LIST += test_dwarf_complete test_bin_info_complete endif dist_noinst_SCRIPTS = $(SCRIPT_LIST) diff --git a/tests/lib/test_bin_info.c b/tests/lib/test_bin_info.c new file mode 100644 index 00000000..011b09a1 --- /dev/null +++ b/tests/lib/test_bin_info.c @@ -0,0 +1,265 @@ +/* + * test_bin_info.c + * + * Babeltrace SO info tests + * + * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation + * Copyright (c) 2015 Antoine Busque + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; under version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include "tap/tap.h" + +#define NR_TESTS 36 +#define SO_NAME "libhello.so" +#define SO_NAME_ELF "libhello_elf.so" +#define SO_NAME_BUILD_ID "libhello_build_id.so" +#define SO_NAME_DEBUG_LINK "libhello_debug_link.so" +#define SO_LOW_ADDR 0x400000 +#define SO_MEMSZ 0x400000 +#define FUNC_FOO_ADDR 0x4014ee +#define FUNC_FOO_LINE_NO 8 +#define FUNC_FOO_FILENAME "/efficios/libhello.c" +#define FUNC_FOO_TP_ADDR 0x4014d3 +#define FUNC_FOO_TP_LINE_NO 7 +#define FUNC_FOO_TP_FILENAME "/efficios/libhello.c" +#define FUNC_FOO_ADDR_ELF 0x4013ef +#define FUNC_FOO_ADDR_DBG_LINK 0x40148e +#define FUNC_FOO_NAME "foo+0xc3" +#define FUNC_FOO_NAME_ELF "foo+0x24" +#define BUILD_ID_LEN 20 + +char *opt_debug_info_dir; +char *opt_debug_info_target_prefix; + +static +void test_bin_info_build_id(const char *data_dir) +{ + int ret; + char path[PATH_MAX]; + char *func_name = NULL; + struct bin_info *bin = NULL; + struct source_location *src_loc = NULL; + uint8_t build_id[BUILD_ID_LEN] = { + 0xcd, 0xd9, 0x8c, 0xdd, 0x87, 0xf7, 0xfe, 0x64, 0xc1, 0x3b, + 0x6d, 0xaa, 0xd5, 0x53, 0x98, 0x7e, 0xaf, 0xd4, 0x0c, 0xbb + }; + + diag("bin-info tests - separate DWARF via build ID"); + + snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_BUILD_ID); + + bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); + ok(bin != NULL, "bin_info_create succesful"); + + /* Test setting build_id */ + ret = bin_info_set_build_id(bin, build_id, BUILD_ID_LEN); + ok(ret == 0, "bin_info_set_build_id succesful"); + + /* Test function name lookup (with DWARF) */ + ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name); + ok(ret == 0, "bin_info_lookup_function_name succesful"); + ok(strcmp(func_name, FUNC_FOO_NAME) == 0, + "bin_info_lookup_function_name - correct func_name value"); + free(func_name); + + /* Test source location lookup */ + ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); + ok(ret == 0, "bin_info_lookup_source_location succesful"); + ok(src_loc->line_no == FUNC_FOO_LINE_NO, + "bin_info_lookup_source_location - correct line_no"); + ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, + "bin_info_lookup_source_location - correct filename"); + source_location_destroy(src_loc); + + bin_info_destroy(bin); +} + +static +void test_bin_info_debug_link(const char *data_dir) +{ + int ret; + char path[PATH_MAX]; + char *func_name = NULL; + struct bin_info *bin = NULL; + struct source_location *src_loc = NULL; + char *dbg_filename = "libhello_debug_link.so.debug"; + uint32_t crc = 0xe55c2b98; + + diag("bin-info tests - separate DWARF via debug link"); + + snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_DEBUG_LINK); + + bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); + ok(bin != NULL, "bin_info_create succesful"); + + /* Test setting debug link */ + ret = bin_info_set_debug_link(bin, dbg_filename, crc); + ok(ret == 0, "bin_info_set_debug_link succesful"); + + /* Test function name lookup (with DWARF) */ + ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_DBG_LINK, + &func_name); + ok(ret == 0, "bin_info_lookup_function_name succesful"); + ok(strcmp(func_name, FUNC_FOO_NAME) == 0, + "bin_info_lookup_function_name - correct func_name value"); + free(func_name); + + /* Test source location lookup */ + ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_DBG_LINK, + &src_loc); + ok(ret == 0, "bin_info_lookup_source_location succesful"); + ok(src_loc->line_no == FUNC_FOO_LINE_NO, + "bin_info_lookup_source_location - correct line_no"); + ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, + "bin_info_lookup_source_location - correct filename"); + source_location_destroy(src_loc); + + bin_info_destroy(bin); +} + +static +void test_bin_info_elf(const char *data_dir) +{ + int ret; + char path[PATH_MAX]; + char *func_name = NULL; + struct bin_info *bin = NULL; + struct source_location *src_loc = NULL; + + diag("bin-info tests - ELF only"); + + snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_ELF); + + bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); + ok(bin != NULL, "bin_info_create succesful"); + + /* Test function name lookup (with ELF) */ + ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_ELF, &func_name); + ok(ret == 0, "bin_info_lookup_function_name succesful"); + ok(strcmp(func_name, FUNC_FOO_NAME_ELF) == 0, + "bin_info_lookup_function_name - correct func_name value"); + free(func_name); + func_name = NULL; + + /* Test function name lookup - erroneous address */ + ret = bin_info_lookup_function_name(bin, 0, &func_name); + ok(ret == -1 && func_name == NULL, + "bin_info_lookup_function_name - fail on addr not found"); + + /* Test source location location - should fail on ELF only file */ + ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_ELF, &src_loc); + ok(ret == -1, "bin_info_lookup_source_location - fail on ELF only file"); + + source_location_destroy(src_loc); + bin_info_destroy(bin); +} + +static +void test_bin_info(const char *data_dir) +{ + int ret; + char path[PATH_MAX]; + char *func_name = NULL; + struct bin_info *bin = NULL; + struct source_location *src_loc = NULL; + + diag("bin-info tests - DWARF bundled with SO file"); + + snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME); + + bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); + ok(bin != NULL, "bin_info_create succesful"); + + /* Test bin_info_has_address */ + ret = bin_info_has_address(bin, 0); + ok(ret == 0, "bin_info_has_address - address under so's range"); + ret = bin_info_has_address(bin, SO_LOW_ADDR); + ok(ret == 1, "bin_info_has_address - lower bound of so's range"); + ret = bin_info_has_address(bin, FUNC_FOO_ADDR); + ok(ret == 1, "bin_info_has_address - address in so's range"); + ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ - 1); + ok(ret == 1, "bin_info_has_address - upper bound of so's range"); + ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ); + ok(ret == 0, "bin_info_has_address - address above so's range"); + + /* Test function name lookup (with DWARF) */ + ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name); + ok(ret == 0, "bin_info_lookup_function_name succesful"); + ok(strcmp(func_name, FUNC_FOO_NAME) == 0, + "bin_info_lookup_function_name - correct func_name value"); + free(func_name); + func_name = NULL; + + /* Test function name lookup - erroneous address */ + ret = bin_info_lookup_function_name(bin, 0, &func_name); + ok(ret == -1 && func_name == NULL, + "bin_info_lookup_function_name - fail on addr not found"); + + /* Test source location lookup */ + ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); + ok(ret == 0, "bin_info_lookup_source_location succesful"); + ok(src_loc->line_no == FUNC_FOO_LINE_NO, + "bin_info_lookup_source_location - correct line_no"); + ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, + "bin_info_lookup_source_location - correct filename"); + source_location_destroy(src_loc); + src_loc = NULL; + + /* Test source location lookup - inlined function */ + ret = bin_info_lookup_source_location(bin, FUNC_FOO_TP_ADDR, &src_loc); + ok(ret == 0, + "bin_info_lookup_source_location (inlined func) succesful"); + ok(src_loc->line_no == FUNC_FOO_TP_LINE_NO, + "bin_info_lookup_source_location (inlined func) - correct line_no"); + ok(strcmp(src_loc->filename, FUNC_FOO_TP_FILENAME) == 0, + "bin_info_lookup_source_location (inlined func) - correct filename"); + source_location_destroy(src_loc); + src_loc = NULL; + + /* Test source location lookup - erroneous address */ + ret = bin_info_lookup_source_location(bin, 0, &src_loc); + ok(ret == -1 && src_loc == NULL, + "bin_info_lookup_source_location - fail on addr not found"); + + bin_info_destroy(bin); +} + +int main(int argc, char **argv) +{ + int ret; + + plan_tests(NR_TESTS); + + if (argc != 2) { + return EXIT_FAILURE; + } else { + opt_debug_info_dir = argv[1]; + } + + ret = bin_info_init(); + ok(ret == 0, "bin_info_init succesful"); + + test_bin_info(opt_debug_info_dir); + test_bin_info_elf(opt_debug_info_dir); + test_bin_info_build_id(opt_debug_info_dir); + test_bin_info_debug_link(opt_debug_info_dir); + + return EXIT_SUCCESS; +} diff --git a/tests/lib/test_bin_info_complete b/tests/lib/test_bin_info_complete new file mode 100755 index 00000000..0f930109 --- /dev/null +++ b/tests/lib/test_bin_info_complete @@ -0,0 +1,22 @@ +#!/bin/sh +# +# Copyright (C) 2015 - Antoine Busque +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; only version 2 +# of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +CURDIR=$(dirname $0)/ +ROOTDIR=$CURDIR../.. + +$CURDIR/test_bin_info $ROOTDIR/tests/debuginfo-data diff --git a/tests/lib/test_so_info.c b/tests/lib/test_so_info.c deleted file mode 100644 index 4ef14ed6..00000000 --- a/tests/lib/test_so_info.c +++ /dev/null @@ -1,265 +0,0 @@ -/* - * test_so_info.c - * - * Babeltrace SO info tests - * - * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation - * Copyright (c) 2015 Antoine Busque - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; under version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include -#include -#include -#include -#include "tap/tap.h" - -#define NR_TESTS 36 -#define SO_NAME "libhello.so" -#define SO_NAME_ELF "libhello_elf.so" -#define SO_NAME_BUILD_ID "libhello_build_id.so" -#define SO_NAME_DEBUG_LINK "libhello_debug_link.so" -#define SO_LOW_ADDR 0x400000 -#define SO_MEMSZ 0x400000 -#define FUNC_FOO_ADDR 0x4014ee -#define FUNC_FOO_LINE_NO 8 -#define FUNC_FOO_FILENAME "/efficios/libhello.c" -#define FUNC_FOO_TP_ADDR 0x4014d3 -#define FUNC_FOO_TP_LINE_NO 7 -#define FUNC_FOO_TP_FILENAME "/efficios/libhello.c" -#define FUNC_FOO_ADDR_ELF 0x4013ef -#define FUNC_FOO_ADDR_DBG_LINK 0x40148e -#define FUNC_FOO_NAME "foo+0xc3" -#define FUNC_FOO_NAME_ELF "foo+0x24" -#define BUILD_ID_LEN 20 - -char *opt_debug_info_dir; -char *opt_debug_info_target_prefix; - -static -void test_so_info_build_id(const char *data_dir) -{ - int ret; - char path[PATH_MAX]; - char *func_name = NULL; - struct so_info *so = NULL; - struct source_location *src_loc = NULL; - uint8_t build_id[BUILD_ID_LEN] = { - 0xcd, 0xd9, 0x8c, 0xdd, 0x87, 0xf7, 0xfe, 0x64, 0xc1, 0x3b, - 0x6d, 0xaa, 0xd5, 0x53, 0x98, 0x7e, 0xaf, 0xd4, 0x0c, 0xbb - }; - - diag("so-info tests - separate DWARF via build ID"); - - snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_BUILD_ID); - - so = so_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); - ok(so != NULL, "so_info_create succesful"); - - /* Test setting build_id */ - ret = so_info_set_build_id(so, build_id, BUILD_ID_LEN); - ok(ret == 0, "so_info_set_build_id succesful"); - - /* Test function name lookup (with DWARF) */ - ret = so_info_lookup_function_name(so, FUNC_FOO_ADDR, &func_name); - ok(ret == 0, "so_info_lookup_function_name succesful"); - ok(strcmp(func_name, FUNC_FOO_NAME) == 0, - "so_info_lookup_function_name - correct func_name value"); - free(func_name); - - /* Test source location lookup */ - ret = so_info_lookup_source_location(so, FUNC_FOO_ADDR, &src_loc); - ok(ret == 0, "so_info_lookup_source_location succesful"); - ok(src_loc->line_no == FUNC_FOO_LINE_NO, - "so_info_lookup_source_location - correct line_no"); - ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, - "so_info_lookup_source_location - correct filename"); - source_location_destroy(src_loc); - - so_info_destroy(so); -} - -static -void test_so_info_debug_link(const char *data_dir) -{ - int ret; - char path[PATH_MAX]; - char *func_name = NULL; - struct so_info *so = NULL; - struct source_location *src_loc = NULL; - char *dbg_filename = "libhello_debug_link.so.debug"; - uint32_t crc = 0xe55c2b98; - - diag("so-info tests - separate DWARF via debug link"); - - snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_DEBUG_LINK); - - so = so_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); - ok(so != NULL, "so_info_create succesful"); - - /* Test setting debug link */ - ret = so_info_set_debug_link(so, dbg_filename, crc); - ok(ret == 0, "so_info_set_debug_link succesful"); - - /* Test function name lookup (with DWARF) */ - ret = so_info_lookup_function_name(so, FUNC_FOO_ADDR_DBG_LINK, - &func_name); - ok(ret == 0, "so_info_lookup_function_name succesful"); - ok(strcmp(func_name, FUNC_FOO_NAME) == 0, - "so_info_lookup_function_name - correct func_name value"); - free(func_name); - - /* Test source location lookup */ - ret = so_info_lookup_source_location(so, FUNC_FOO_ADDR_DBG_LINK, - &src_loc); - ok(ret == 0, "so_info_lookup_source_location succesful"); - ok(src_loc->line_no == FUNC_FOO_LINE_NO, - "so_info_lookup_source_location - correct line_no"); - ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, - "so_info_lookup_source_location - correct filename"); - source_location_destroy(src_loc); - - so_info_destroy(so); -} - -static -void test_so_info_elf(const char *data_dir) -{ - int ret; - char path[PATH_MAX]; - char *func_name = NULL; - struct so_info *so = NULL; - struct source_location *src_loc = NULL; - - diag("so-info tests - ELF only"); - - snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_ELF); - - so = so_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); - ok(so != NULL, "so_info_create succesful"); - - /* Test function name lookup (with ELF) */ - ret = so_info_lookup_function_name(so, FUNC_FOO_ADDR_ELF, &func_name); - ok(ret == 0, "so_info_lookup_function_name succesful"); - ok(strcmp(func_name, FUNC_FOO_NAME_ELF) == 0, - "so_info_lookup_function_name - correct func_name value"); - free(func_name); - func_name = NULL; - - /* Test function name lookup - erroneous address */ - ret = so_info_lookup_function_name(so, 0, &func_name); - ok(ret == -1 && func_name == NULL, - "so_info_lookup_function_name - fail on addr not found"); - - /* Test source location location - should fail on ELF only file */ - ret = so_info_lookup_source_location(so, FUNC_FOO_ADDR_ELF, &src_loc); - ok(ret == -1, "so_info_lookup_source_location - fail on ELF only file"); - - source_location_destroy(src_loc); - so_info_destroy(so); -} - -static -void test_so_info(const char *data_dir) -{ - int ret; - char path[PATH_MAX]; - char *func_name = NULL; - struct so_info *so = NULL; - struct source_location *src_loc = NULL; - - diag("so-info tests - DWARF bundled with SO file"); - - snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME); - - so = so_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); - ok(so != NULL, "so_info_create succesful"); - - /* Test so_info_has_address */ - ret = so_info_has_address(so, 0); - ok(ret == 0, "so_info_has_address - address under so's range"); - ret = so_info_has_address(so, SO_LOW_ADDR); - ok(ret == 1, "so_info_has_address - lower bound of so's range"); - ret = so_info_has_address(so, FUNC_FOO_ADDR); - ok(ret == 1, "so_info_has_address - address in so's range"); - ret = so_info_has_address(so, SO_LOW_ADDR + SO_MEMSZ - 1); - ok(ret == 1, "so_info_has_address - upper bound of so's range"); - ret = so_info_has_address(so, SO_LOW_ADDR + SO_MEMSZ); - ok(ret == 0, "so_info_has_address - address above so's range"); - - /* Test function name lookup (with DWARF) */ - ret = so_info_lookup_function_name(so, FUNC_FOO_ADDR, &func_name); - ok(ret == 0, "so_info_lookup_function_name succesful"); - ok(strcmp(func_name, FUNC_FOO_NAME) == 0, - "so_info_lookup_function_name - correct func_name value"); - free(func_name); - func_name = NULL; - - /* Test function name lookup - erroneous address */ - ret = so_info_lookup_function_name(so, 0, &func_name); - ok(ret == -1 && func_name == NULL, - "so_info_lookup_function_name - fail on addr not found"); - - /* Test source location lookup */ - ret = so_info_lookup_source_location(so, FUNC_FOO_ADDR, &src_loc); - ok(ret == 0, "so_info_lookup_source_location succesful"); - ok(src_loc->line_no == FUNC_FOO_LINE_NO, - "so_info_lookup_source_location - correct line_no"); - ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, - "so_info_lookup_source_location - correct filename"); - source_location_destroy(src_loc); - src_loc = NULL; - - /* Test source location lookup - inlined function */ - ret = so_info_lookup_source_location(so, FUNC_FOO_TP_ADDR, &src_loc); - ok(ret == 0, - "so_info_lookup_source_location (inlined func) succesful"); - ok(src_loc->line_no == FUNC_FOO_TP_LINE_NO, - "so_info_lookup_source_location (inlined func) - correct line_no"); - ok(strcmp(src_loc->filename, FUNC_FOO_TP_FILENAME) == 0, - "so_info_lookup_source_location (inlined func) - correct filename"); - source_location_destroy(src_loc); - src_loc = NULL; - - /* Test source location lookup - erroneous address */ - ret = so_info_lookup_source_location(so, 0, &src_loc); - ok(ret == -1 && src_loc == NULL, - "so_info_lookup_source_location - fail on addr not found"); - - so_info_destroy(so); -} - -int main(int argc, char **argv) -{ - int ret; - - plan_tests(NR_TESTS); - - if (argc != 2) { - return EXIT_FAILURE; - } else { - opt_debug_info_dir = argv[1]; - } - - ret = so_info_init(); - ok(ret == 0, "so_info_init succesful"); - - test_so_info(opt_debug_info_dir); - test_so_info_elf(opt_debug_info_dir); - test_so_info_build_id(opt_debug_info_dir); - test_so_info_debug_link(opt_debug_info_dir); - - return EXIT_SUCCESS; -} diff --git a/tests/lib/test_so_info_complete b/tests/lib/test_so_info_complete deleted file mode 100755 index 771fbfb7..00000000 --- a/tests/lib/test_so_info_complete +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh -# -# Copyright (C) 2015 - Antoine Busque -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; only version 2 -# of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# -CURDIR=$(dirname $0)/ -ROOTDIR=$CURDIR../.. - -$CURDIR/test_so_info $ROOTDIR/tests/debuginfo-data diff --git a/tests/tests_debuginfo b/tests/tests_debuginfo index 4917f448..df87fc1d 100644 --- a/tests/tests_debuginfo +++ b/tests/tests_debuginfo @@ -1,2 +1,2 @@ lib/test_dwarf_complete -lib/test_so_info_complete +lib/test_bin_info_complete