| 1 | /* |
| 2 | * SPDX-License-Identifier: GPL-2.0-only |
| 3 | * |
| 4 | * Copyright (C) 2015 EfficiOS Inc. and Linux Foundation |
| 5 | * Copyright (C) 2015 Antoine Busque <abusque@efficios.com> |
| 6 | * |
| 7 | * Babeltrace bt_dwarf (DWARF utilities) tests |
| 8 | */ |
| 9 | |
| 10 | #include <fcntl.h> |
| 11 | #include <glib.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <stdint.h> |
| 14 | #include <string.h> |
| 15 | #include <unistd.h> |
| 16 | #include <lttng-utils/debug-info/dwarf.h> |
| 17 | #include "tap/tap.h" |
| 18 | |
| 19 | #define NR_TESTS 17 |
| 20 | |
| 21 | #define SO_NAME "libhello-so" |
| 22 | #define DWARF_DIR_NAME "dwarf-full" |
| 23 | #define ELF_DIR_NAME "elf-only" |
| 24 | |
| 25 | /* |
| 26 | * Test that we fail on an ELF file without DWARF. |
| 27 | */ |
| 28 | static |
| 29 | void test_bt_no_dwarf(const char *data_dir) |
| 30 | { |
| 31 | int fd; |
| 32 | char *path; |
| 33 | Dwarf *dwarf_info = NULL; |
| 34 | |
| 35 | path = g_build_filename(data_dir, ELF_DIR_NAME, SO_NAME, NULL); |
| 36 | if (!path) { |
| 37 | diag("Failed to allocate memory for path"); |
| 38 | exit(EXIT_FAILURE); |
| 39 | } |
| 40 | |
| 41 | fd = open(path, O_RDONLY); |
| 42 | ok(fd >= 0, "Open ELF file %s", path); |
| 43 | if (fd < 0) { |
| 44 | skip(1, "dwarf_begin failed as expected"); |
| 45 | } else { |
| 46 | dwarf_info = dwarf_begin(fd, DWARF_C_READ); |
| 47 | ok(!dwarf_info, "dwarf_begin failed as expected"); |
| 48 | } |
| 49 | |
| 50 | if (dwarf_info) { |
| 51 | dwarf_end(dwarf_info); |
| 52 | } |
| 53 | |
| 54 | if (fd >= 0) { |
| 55 | close(fd); |
| 56 | } |
| 57 | g_free(path); |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Test with a proper ELF file with DWARF. |
| 62 | */ |
| 63 | static |
| 64 | void test_bt_dwarf(const char *data_dir) |
| 65 | { |
| 66 | int fd, ret, tag = -1; |
| 67 | char *path; |
| 68 | char *die_name = NULL; |
| 69 | struct bt_dwarf_cu *cu = NULL; |
| 70 | struct bt_dwarf_die *die = NULL; |
| 71 | Dwarf *dwarf_info = NULL; |
| 72 | |
| 73 | path = g_build_filename(data_dir, DWARF_DIR_NAME, SO_NAME, NULL); |
| 74 | if (!path) { |
| 75 | diag("Failed to allocate memory for path"); |
| 76 | exit(EXIT_FAILURE); |
| 77 | } |
| 78 | |
| 79 | fd = open(path, O_RDONLY); |
| 80 | ok(fd >= 0, "Open DWARF file %s", path); |
| 81 | if (fd < 0) { |
| 82 | exit(EXIT_FAILURE); |
| 83 | } |
| 84 | dwarf_info = dwarf_begin(fd, DWARF_C_READ); |
| 85 | ok(dwarf_info, "dwarf_begin successful"); |
| 86 | cu = bt_dwarf_cu_create(dwarf_info); |
| 87 | ok(cu, "bt_dwarf_cu_create successful"); |
| 88 | ret = bt_dwarf_cu_next(cu); |
| 89 | ok(ret == 0, "bt_dwarf_cu_next successful"); |
| 90 | die = bt_dwarf_die_create(cu); |
| 91 | ok(die, "bt_dwarf_die_create successful"); |
| 92 | if (!die) { |
| 93 | exit(EXIT_FAILURE); |
| 94 | } |
| 95 | /* |
| 96 | * Test bt_dwarf_die_next twice, as the code path is different |
| 97 | * for DIEs at depth 0 (just created) and other depths. |
| 98 | */ |
| 99 | ret = bt_dwarf_die_next(die); |
| 100 | ok(ret == 0, "bt_dwarf_die_next from root DIE successful"); |
| 101 | ok(die->depth == 1, |
| 102 | "bt_dwarf_die_next from root DIE - correct depth value"); |
| 103 | ret = bt_dwarf_die_next(die); |
| 104 | ok(ret == 0, |
| 105 | "bt_dwarf_die_next from non-root DIE successful"); |
| 106 | ok(die->depth == 1, |
| 107 | "bt_dwarf_die_next from non-root DIE - correct depth value"); |
| 108 | |
| 109 | /* Reset DIE to test dwarf_child */ |
| 110 | bt_dwarf_die_destroy(die); |
| 111 | die = bt_dwarf_die_create(cu); |
| 112 | if (!die) { |
| 113 | diag("Failed to create bt_dwarf_die"); |
| 114 | exit(EXIT_FAILURE); |
| 115 | } |
| 116 | |
| 117 | ret = bt_dwarf_die_child(die); |
| 118 | ok(ret == 0, "bt_dwarf_die_child successful"); |
| 119 | ok(die->depth == 1, "bt_dwarf_die_child - correct depth value"); |
| 120 | |
| 121 | ret = bt_dwarf_die_get_tag(die, &tag); |
| 122 | ok(ret == 0, "bt_dwarf_die_get_tag successful"); |
| 123 | ok(tag == DW_TAG_typedef, "bt_dwarf_die_get_tag - correct tag value"); |
| 124 | ret = bt_dwarf_die_get_name(die, &die_name); |
| 125 | ok(ret == 0, "bt_dwarf_die_get_name successful"); |
| 126 | ok(strcmp(die_name, "size_t") == 0, |
| 127 | "bt_dwarf_die_get_name - correct name value"); |
| 128 | |
| 129 | bt_dwarf_die_destroy(die); |
| 130 | bt_dwarf_cu_destroy(cu); |
| 131 | dwarf_end(dwarf_info); |
| 132 | free(die_name); |
| 133 | close(fd); |
| 134 | g_free(path); |
| 135 | } |
| 136 | |
| 137 | int main(int argc, char **argv) |
| 138 | { |
| 139 | const char *data_dir; |
| 140 | |
| 141 | plan_tests(NR_TESTS); |
| 142 | |
| 143 | if (argc != 2) { |
| 144 | return EXIT_FAILURE; |
| 145 | } else { |
| 146 | data_dir = argv[1]; |
| 147 | } |
| 148 | |
| 149 | test_bt_no_dwarf(data_dir); |
| 150 | test_bt_dwarf(data_dir); |
| 151 | |
| 152 | return exit_status(); |
| 153 | } |