flt.lttng-utils.debug-info: use glib memory and string functions
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Wed, 27 Mar 2019 14:44:05 +0000 (10:44 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 3 May 2019 22:19:39 +0000 (18:19 -0400)
- Change use of glibc's malloc() to glib's g_new0() to follow the rest of
  the project.

- Change use of glibc string functions to their glib equivalent.

- Remove uses of GNU extension of conditional expressions with omitted
  operands as it would only be useful if the evaluation of the condition
  had side effects which it doesn't. Let's prefer code simplicity.

[0]:  https://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Conditionals.html

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
plugins/lttng-utils/bin-info.c
plugins/lttng-utils/bin-info.h
plugins/lttng-utils/debug-info.c
plugins/lttng-utils/dwarf.c

index 176b37784b9db0f94973902def34ffdf0222e53c..daf61e2cf8fb4bdbf87a439bcd87bc48ffc45247 100644 (file)
@@ -95,7 +95,7 @@ struct bin_info *bin_info_create(const char *path, uint64_t low_addr,
        }
 
        if (debug_info_dir) {
-               bin->debug_info_dir = strdup(debug_info_dir);
+               bin->debug_info_dir = g_strdup(debug_info_dir);
                if (!bin->debug_info_dir) {
                        goto error;
                }
@@ -125,11 +125,11 @@ void bin_info_destroy(struct bin_info *bin)
 
        dwarf_end(bin->dwarf_info);
 
-       free(bin->debug_info_dir);
-       free(bin->elf_path);
-       free(bin->dwarf_path);
+       g_free(bin->debug_info_dir);
+       g_free(bin->elf_path);
+       g_free(bin->dwarf_path);
        g_free(bin->build_id);
-       free(bin->dbg_link_filename);
+       g_free(bin->dbg_link_filename);
 
        elf_end(bin->elf_file);
 
@@ -409,7 +409,7 @@ int bin_info_set_debug_link(struct bin_info *bin, const char *filename,
                goto error;
        }
 
-       bin->dbg_link_filename = strdup(filename);
+       bin->dbg_link_filename = g_strdup(filename);
        if (!bin->dbg_link_filename) {
                goto error;
        }
@@ -475,7 +475,7 @@ int bin_info_set_dwarf_info_from_path(struct bin_info *bin, char *path)
        }
 
        bin->dwarf_fd = fd;
-       bin->dwarf_path = strdup(path);
+       bin->dwarf_path = g_strdup(path);
        if (!bin->dwarf_path) {
                goto error;
        }
@@ -516,23 +516,23 @@ int bin_info_set_dwarf_info_build_id(struct bin_info *bin)
                goto error;
        }
 
-       dbg_dir = bin->debug_info_dir ? : DEFAULT_DEBUG_DIR;
+       dbg_dir = bin->debug_info_dir ? bin->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);
+       build_id_file = g_new0(gchar, build_id_file_len);
        if (!build_id_file) {
                goto error;
        }
 
-       snprintf(build_id_file, 4, "%02x/", bin->build_id[0]);
+       g_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]);
+               g_snprintf(&build_id_file[path_idx], 3, "%02x", bin->build_id[i]);
        }
-       strcat(build_id_file, BUILD_ID_SUFFIX);
+       g_strconcat(build_id_file, BUILD_ID_SUFFIX, NULL);
 
        path = g_build_path("/", dbg_dir, BUILD_ID_SUBDIR, build_id_file, NULL);
        if (!path) {
@@ -610,60 +610,40 @@ 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;
+       const gchar *dbg_dir = NULL;
+       gchar *bin_dir = NULL, *dir_name = NULL, *path = NULL;
 
        if (!bin || !bin->dbg_link_filename) {
                goto error;
        }
 
-       dbg_dir = bin->debug_info_dir ? : DEFAULT_DEBUG_DIR;
-
+       dbg_dir = bin->debug_info_dir ? bin->debug_info_dir : DEFAULT_DEBUG_DIR;
        dir_name = g_path_get_dirname(bin->elf_path);
        if (!dir_name) {
                goto error;
        }
 
-       /* bin_dir is just dir_name with a trailing slash */
-       bin_dir = g_new0(char, 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 = g_new0(char, max_path_len);
-       if (!path) {
-               goto error;
-       }
+       bin_dir = g_strconcat(dir_name, "/", NULL);
 
        /* First look in the executable's dir */
-       strcpy(path, bin_dir);
-       strcat(path, bin->dbg_link_filename);
+       path = g_strconcat(bin_dir, bin->dbg_link_filename, NULL);
 
        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);
+       g_free(path);
+       path = g_strconcat(bin_dir, DEBUG_SUBDIR, bin->dbg_link_filename, NULL);
 
        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);
+       g_free(path);
 
+       path = g_strconcat(dbg_dir, bin_dir, bin->dbg_link_filename, NULL);
        if (is_valid_debug_file(path, bin->dbg_link_crc)) {
                goto found;
        }
@@ -673,7 +653,6 @@ error:
 end:
        g_free(dir_name);
        g_free(path);
-       g_free(bin_dir);
 
        return ret;
 
@@ -757,10 +736,9 @@ 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;
@@ -768,17 +746,10 @@ int bin_info_append_offset_str(const char *base_str, uint64_t low_addr,
 
        offset = high_addr - low_addr;
 
-       _result = malloc(strlen(base_str) + ADDR_STR_LEN);
+       _result = g_strdup_printf("%s+%#0" PRIx64, base_str, offset);
        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;
@@ -1166,8 +1137,7 @@ error:
 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;
+       gchar *_bin_loc = NULL;
 
        if (!bin || !bin_loc) {
                goto error;
@@ -1183,12 +1153,12 @@ int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc)
 
        if (bin->is_pic) {
                addr -= bin->low_addr;
-               ret = asprintf(&_bin_loc, "+%#0" PRIx64, addr);
+               _bin_loc = g_strdup_printf("+%#0" PRIx64, addr);
        } else {
-               ret = asprintf(&_bin_loc, "@%#0" PRIx64, addr);
+               _bin_loc = g_strdup_printf("@%#0" PRIx64, addr);
        }
 
-       if (ret == -1 || !_bin_loc) {
+       if (!_bin_loc) {
                goto error;
        }
 
@@ -1424,7 +1394,7 @@ int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu *cu, uint64_t addr,
                }
 
                _src_loc->line_no = line_no;
-               _src_loc->filename = strdup(filename);
+               _src_loc->filename = g_strdup(filename);
        }
 
        bt_dwarf_die_destroy(die);
index 42cb1abcb60a9e8aeba358dfff1b11528b2341ff..2bcd9fae42cd55b1896fe41f765300abdb667da6 100644 (file)
@@ -46,8 +46,8 @@ struct bin_info {
        /* Size of exec address space. */
        uint64_t memsz;
        /* Paths to ELF and DWARF files. */
-       char *elf_path;
-       char *dwarf_path;
+       gchar *elf_path;
+       gchar *dwarf_path;
        /* libelf and libdw objects representing the files. */
        Elf *elf_file;
        Dwarf *dwarf_info;
@@ -56,13 +56,13 @@ struct bin_info {
        size_t build_id_len;
 
        /* Optional debug link info. */
-       char *dbg_link_filename;
+       gchar *dbg_link_filename;
        uint32_t dbg_link_crc;
        /* FDs to ELF and DWARF files. */
        int elf_fd;
        int dwarf_fd;
        /* Configuration. */
-       char *debug_info_dir;
+       gchar *debug_info_dir;
        /* Denotes whether the executable is position independent code. */
        bool is_pic:1;
        /* denotes whether the build id in the trace matches to one on disk. */
@@ -76,7 +76,7 @@ struct bin_info {
 
 struct source_location {
        uint64_t line_no;
-       char *filename;
+       gchar *filename;
 };
 
 /**
index c15f473d79be18eb432a0e1b1f0794ccf5ab0db9..88ee77bc77f4ac39b9eff4a9045cacbdf4116126 100644 (file)
 #define PATH_FIELD_NAME                        "path"
 
 struct debug_info_component {
-       char *arg_debug_info_field_name;
-       const char *arg_debug_dir;
-       bool arg_full_path;
-       const char *arg_target_prefix;
+       gchar *arg_debug_dir;
+       gchar *arg_debug_info_field_name;
+       gchar *arg_target_prefix;
+       bt_bool arg_full_path;
 };
 
 struct debug_info_msg_iter {
@@ -74,23 +74,23 @@ struct debug_info_msg_iter {
 
 struct debug_info_source {
        /* Strings are owned by debug_info_source. */
-       char *func;
+       gchar *func;
        /*
         * Store the line number as a string so that the allocation and
         * conversion to string is only done once.
         */
-       char *line_no;
-       char *src_path;
+       gchar *line_no;
+       gchar *src_path;
        /* short_src_path points inside src_path, no need to free. */
-       const char *short_src_path;
-       char *bin_path;
+       const gchar *short_src_path;
+       gchar *bin_path;
        /* short_bin_path points inside bin_path, no need to free. */
-       const char *short_bin_path;
+       const gchar *short_bin_path;
        /*
         * Location within the binary. Either absolute (@0x1234) or
         * relative (+0x4321).
         */
-       char *bin_loc;
+       gchar *bin_loc;
 };
 
 struct proc_debug_info_sources {
@@ -151,11 +151,11 @@ void debug_info_source_destroy(struct debug_info_source *debug_info_src)
                return;
        }
 
-       free(debug_info_src->func);
-       free(debug_info_src->line_no);
-       free(debug_info_src->src_path);
-       free(debug_info_src->bin_path);
-       free(debug_info_src->bin_loc);
+       g_free(debug_info_src->func);
+       g_free(debug_info_src->line_no);
+       g_free(debug_info_src->src_path);
+       g_free(debug_info_src->bin_path);
+       g_free(debug_info_src->bin_loc);
        g_free(debug_info_src);
 }
 
@@ -189,14 +189,15 @@ struct debug_info_source *debug_info_source_create_from_bin(
        }
 
        if (src_loc) {
-               ret = asprintf(&debug_info_src->line_no, "%"PRId64, src_loc->line_no);
-               if (ret == -1) {
+               debug_info_src->line_no =
+                       g_strdup_printf("%"PRId64, src_loc->line_no);
+               if (!debug_info_src->line_no) {
                        BT_LOGD("Error occured when setting line_no field.");
                        goto error;
                }
 
                if (src_loc->filename) {
-                       debug_info_src->src_path = strdup(src_loc->filename);
+                       debug_info_src->src_path = g_strdup(src_loc->filename);
                        if (!debug_info_src->src_path) {
                                goto error;
                        }
@@ -208,7 +209,7 @@ struct debug_info_source *debug_info_source_create_from_bin(
        }
 
        if (bin->elf_path) {
-               debug_info_src->bin_path = strdup(bin->elf_path);
+               debug_info_src->bin_path = g_strdup(bin->elf_path);
                if (!debug_info_src->bin_path) {
                        goto error;
                }
@@ -907,7 +908,13 @@ void handle_event_statedump(struct debug_info_msg_iter *debug_it,
 static
 void destroy_debug_info_comp(struct debug_info_component *debug_info)
 {
-       free(debug_info->arg_debug_info_field_name);
+       if (!debug_info) {
+               return;
+       }
+
+       g_free(debug_info->arg_debug_dir);
+       g_free(debug_info->arg_debug_info_field_name);
+       g_free(debug_info->arg_target_prefix);
        g_free(debug_info);
 }
 
@@ -1098,7 +1105,7 @@ void fill_debug_info_event_if_needed(struct debug_info_msg_iter *debug_it,
        struct debug_info *debug_info;
        uint64_t vpid;
        int64_t ip;
-       char *debug_info_field_name =
+       gchar *debug_info_field_name =
                debug_it->debug_info_component->arg_debug_info_field_name;
 
        in_common_ctx_field = bt_event_borrow_common_context_field_const(
@@ -1734,43 +1741,37 @@ int init_from_params(struct debug_info_component *debug_info_component,
        value = bt_value_map_borrow_entry_value_const(params,
                        "debug-info-field-name");
        if (value) {
-               const char *debug_info_field_name;
-
-               debug_info_field_name = bt_value_string_get(value);
                debug_info_component->arg_debug_info_field_name =
-                       strdup(debug_info_field_name);
+                       g_strdup(bt_value_string_get(value));
        } else {
                debug_info_component->arg_debug_info_field_name =
-                       malloc(strlen(DEFAULT_DEBUG_INFO_FIELD_NAME) + 1);
-               if (!debug_info_component->arg_debug_info_field_name) {
-                       ret = BT_SELF_COMPONENT_STATUS_NOMEM;
-                       BT_LOGE_STR("Missing field name.");
-                       goto end;
-               }
-               sprintf(debug_info_component->arg_debug_info_field_name,
-                               DEFAULT_DEBUG_INFO_FIELD_NAME);
-       }
-       if (ret) {
-               goto end;
+                       g_strdup(DEFAULT_DEBUG_INFO_FIELD_NAME);
        }
 
        value = bt_value_map_borrow_entry_value_const(params, "debug-info-dir");
        if (value) {
-               debug_info_component->arg_debug_dir = bt_value_string_get(value);
+               debug_info_component->arg_debug_dir =
+                       g_strdup(bt_value_string_get(value));
+       } else {
+               debug_info_component->arg_debug_dir = NULL;
        }
 
+
        value = bt_value_map_borrow_entry_value_const(params, "target-prefix");
        if (value) {
                debug_info_component->arg_target_prefix =
-                       bt_value_string_get(value);
+                       g_strdup(bt_value_string_get(value));
+       } else {
+               debug_info_component->arg_target_prefix = NULL;
        }
 
        value = bt_value_map_borrow_entry_value_const(params, "full-path");
        if (value) {
                debug_info_component->arg_full_path = bt_value_bool_get(value);
+       } else {
+               debug_info_component->arg_full_path = BT_FALSE;
        }
 
-end:
        return ret;
 }
 
@@ -1934,6 +1935,7 @@ bt_self_message_iterator_status debug_info_msg_iter_init(
        struct bt_self_component_port_input *input_port;
        bt_self_component_port_input_message_iterator *upstream_iterator;
        struct debug_info_msg_iter *debug_info_msg_iter;
+       gchar *debug_info_field_name;
 
        /* Borrow the upstream input port. */
        input_port = bt_self_component_filter_borrow_input_port_by_name(
@@ -1957,6 +1959,7 @@ bt_self_message_iterator_status debug_info_msg_iter_init(
                goto end;
        }
 
+       /* Create hashtable to will contain debug info mapping. */
        debug_info_msg_iter->debug_info_map = g_hash_table_new_full(
                        g_direct_hash, g_direct_equal,
                        (GDestroyNotify) NULL,
@@ -1977,9 +1980,12 @@ bt_self_message_iterator_status debug_info_msg_iter_init(
                                bt_self_component_filter_as_self_component(
                                        self_comp));
 
+       debug_info_field_name =
+               debug_info_msg_iter->debug_info_component->arg_debug_info_field_name;
+
        debug_info_msg_iter->ir_maps = trace_ir_maps_create(
                        bt_self_component_filter_as_self_component(self_comp),
-                       debug_info_msg_iter->debug_info_component->arg_debug_info_field_name);
+                       debug_info_field_name);
        if (!debug_info_msg_iter->ir_maps) {
                g_hash_table_destroy(debug_info_msg_iter->debug_info_map);
                g_free(debug_info_msg_iter);
index 307bd2aa733396edcaba6d19691e19e6414483d4..3c7388a57679d8af953cf1b28649a597a58a6400 100644 (file)
@@ -242,7 +242,7 @@ int bt_dwarf_die_get_name(struct bt_dwarf_die *die, char **name)
                goto error;
        }
 
-       *name = strdup(_name);
+       *name = g_strdup(_name);
        if (!*name) {
                goto error;
        }
@@ -297,7 +297,7 @@ int bt_dwarf_die_get_call_file(struct bt_dwarf_die *die, char **filename)
                goto error;
        }
 
-       *filename = strdup(_filename);
+       *filename = g_strdup(_filename);
 
        bt_dwarf_die_destroy(cu_die);
        g_free(file_attr);
This page took 0.034008 seconds and 4 git commands to generate.