debug-info: free existing build-id in bin_info_set_build_id
[babeltrace.git] / src / plugins / lttng-utils / debug-info / bin-info.c
index cbf9310a079a10d1fd11e5fea522c687cd4a301a..b9e92871141557123cd950c5b365e4b48d4fb88e 100644 (file)
  * SOFTWARE.
  */
 
+#define BT_COMP_LOG_SELF_COMP (bin->self_comp)
+#define BT_LOG_OUTPUT_LEVEL (bin->log_level)
 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/BIN-INFO"
-#include "logging.h"
+#include "logging/comp-logging.h"
 
+#include <babeltrace2/logging.h>
 #include <dwarf.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
 #include <libgen.h>
 #include <math.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #define BUILD_ID_NOTE_NAME "GNU"
 
 BT_HIDDEN
-int bin_info_init(void)
+int bin_info_init(bt_logging_level log_level, bt_self_component *self_comp)
 {
        int ret = 0;
 
        if (elf_version(EV_CURRENT) == EV_NONE) {
-               BT_LOGI("ELF library initialization failed: %s.",
+               BT_COMP_LOG_CUR_LVL(BT_LOG_INFO, log_level, self_comp,
+                       "ELF library initialization failed: %s.",
                        elf_errmsg(-1));
                ret = -1;
        }
@@ -74,7 +79,8 @@ int bin_info_init(void)
 BT_HIDDEN
 struct bin_info *bin_info_create(struct bt_fd_cache *fdc, const char *path,
                uint64_t low_addr, uint64_t memsz, bool is_pic,
-               const char *debug_info_dir, const char *target_prefix)
+               const char *debug_info_dir, const char *target_prefix,
+               bt_logging_level log_level, bt_self_component *self_comp)
 {
        struct bin_info *bin = NULL;
 
@@ -89,6 +95,8 @@ struct bin_info *bin_info_create(struct bt_fd_cache *fdc, const char *path,
                goto error;
        }
 
+       bin->log_level = log_level;
+       bin->self_comp = self_comp;
        if (target_prefix) {
                bin->elf_path = g_build_filename(target_prefix, path, NULL);
        } else {
@@ -156,14 +164,13 @@ int bin_info_set_elf_file(struct bin_info *bin)
 {
        struct bt_fd_cache_handle *elf_handle = NULL;
        Elf *elf_file = NULL;
+       int ret;
 
-       if (!bin) {
-               goto error;
-       }
+       BT_ASSERT(bin);
 
        elf_handle = bt_fd_cache_get_handle(bin->fd_cache, bin->elf_path);
        if (!elf_handle) {
-               BT_LOGI("Failed to open %s", bin->elf_path);
+               BT_COMP_LOGI("Failed to open %s", bin->elf_path);
                goto error;
        }
        bin->elf_handle = elf_handle;
@@ -171,23 +178,30 @@ int bin_info_set_elf_file(struct bin_info *bin)
        elf_file = elf_begin(bt_fd_cache_handle_get_fd(bin->elf_handle),
                ELF_C_READ, NULL);
        if (!elf_file) {
-               BT_LOGE("elf_begin failed: %s", elf_errmsg(-1));
+               BT_COMP_LOGE_APPEND_CAUSE(bin->self_comp,
+                       "elf_begin failed: %s", elf_errmsg(-1));
                goto error;
        }
 
        bin->elf_file = elf_file;
 
        if (elf_kind(elf_file) != ELF_K_ELF) {
-               BT_LOGE("Error: %s is not an ELF object", bin->elf_path);
+               BT_COMP_LOGE_APPEND_CAUSE(bin->self_comp,
+                       "Error: %s is not an ELF object", bin->elf_path);
                goto error;
        }
 
-       return 0;
+
+       ret = 0;
+       goto end;
 
 error:
        bt_fd_cache_put_handle(bin->fd_cache, elf_handle);
        elf_end(elf_file);
-       return -1;
+       ret = -1;
+
+end:
+       return ret;
 }
 
 /**
@@ -363,6 +377,9 @@ int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id,
                goto error;
        }
 
+       /* Free any previously set build id. */
+       g_free(bin->build_id);
+
        /* Set the build id. */
        bin->build_id = g_new0(uint8_t, build_id_len);
        if (!bin->build_id) {
@@ -378,7 +395,7 @@ int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id,
         */
        bin->file_build_id_matches = is_build_id_matching(bin);
        if (!bin->file_build_id_matches) {
-               BT_LOGI_STR("Supplied Build ID does not match Build ID of the "
+               BT_COMP_LOGI_STR("Supplied Build ID does not match Build ID of the "
                                "binary or library found on the file system.");
                goto error;
        }
@@ -481,7 +498,9 @@ int bin_info_set_dwarf_info_from_path(struct bin_info *bin, char *path)
        return 0;
 
 error:
-       bt_fd_cache_put_handle(bin->fd_cache, dwarf_handle);
+       if (bin) {
+               bt_fd_cache_put_handle(bin->fd_cache, dwarf_handle);
+       }
        dwarf_end(dwarf_info);
        g_free(dwarf_info);
        free(cu);
@@ -1115,7 +1134,7 @@ int bin_info_lookup_function_name(struct bin_info *bin,
        if (!bin->dwarf_info && !bin->is_elf_only) {
                ret = bin_info_set_dwarf_info(bin);
                if (ret) {
-                       BT_LOGI_STR("Failed to set bin dwarf info, falling "
+                       BT_COMP_LOGI_STR("Failed to set bin dwarf info, falling "
                                        "back to ELF lookup.");
                        /* Failed to set DWARF info, fallback to ELF. */
                        bin->is_elf_only = true;
@@ -1138,14 +1157,14 @@ int bin_info_lookup_function_name(struct bin_info *bin,
                ret = bin_info_lookup_elf_function_name(bin, addr,
                                &_func_name);
                if (ret) {
-                       BT_LOGI("Failed to lookup function name (ELF): "
+                       BT_COMP_LOGI("Failed to lookup function name (ELF): "
                                "ret=%d", ret);
                }
        } else {
                ret = bin_info_lookup_dwarf_function_name(bin, addr,
                                &_func_name);
                if (ret) {
-                       BT_LOGI("Failed to lookup function name (DWARF): "
+                       BT_COMP_LOGI("Failed to lookup function name (DWARF): "
                                "ret=%d", ret);
                }
        }
This page took 0.024915 seconds and 4 git commands to generate.