| 1 | /* |
| 2 | * test_dwarf.c |
| 3 | * |
| 4 | * Babeltrace bt_dwarf (DWARF utilities) tests |
| 5 | * |
| 6 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation |
| 7 | * Copyright (c) 2015 Antoine Busque <abusque@efficios.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; under version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include <fcntl.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdint.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | #include <babeltrace/dwarf.h> |
| 29 | #include "tap/tap.h" |
| 30 | |
| 31 | #define NR_TESTS 15 |
| 32 | |
| 33 | static |
| 34 | void test_bt_dwarf(const char *data_dir) |
| 35 | { |
| 36 | int fd, ret, tag; |
| 37 | char path[PATH_MAX]; |
| 38 | char *die_name = NULL; |
| 39 | struct bt_dwarf_cu *cu = NULL; |
| 40 | struct bt_dwarf_die *die = NULL; |
| 41 | Dwarf *dwarf_info = NULL; |
| 42 | |
| 43 | snprintf(path, PATH_MAX, "%s/libhello_so", data_dir); |
| 44 | |
| 45 | fd = open(path, O_RDONLY); |
| 46 | ok(fd >= 0, "Open DWARF file %s", path); |
| 47 | if (fd < 0) { |
| 48 | exit(EXIT_FAILURE); |
| 49 | } |
| 50 | dwarf_info = dwarf_begin(fd, DWARF_C_READ); |
| 51 | ok(dwarf_info != NULL, "dwarf_begin successful"); |
| 52 | cu = bt_dwarf_cu_create(dwarf_info); |
| 53 | ok(cu != NULL, "bt_dwarf_cu_create successful"); |
| 54 | ret = bt_dwarf_cu_next(cu); |
| 55 | ok(ret == 0, "bt_dwarf_cu_next successful"); |
| 56 | die = bt_dwarf_die_create(cu); |
| 57 | ok(die != NULL, "bt_dwarf_die_create successful"); |
| 58 | if (!die) { |
| 59 | exit(EXIT_FAILURE); |
| 60 | } |
| 61 | /* |
| 62 | * Test bt_dwarf_die_next twice, as the code path is different |
| 63 | * for DIEs at depth 0 (just created) and other depths. |
| 64 | */ |
| 65 | ret = bt_dwarf_die_next(die); |
| 66 | ok(ret == 0, "bt_dwarf_die_next from root DIE successful"); |
| 67 | ok(die->depth == 1, |
| 68 | "bt_dwarf_die_next from root DIE - correct depth value"); |
| 69 | ret = bt_dwarf_die_next(die); |
| 70 | ok(ret == 0, |
| 71 | "bt_dwarf_die_next from non-root DIE successful"); |
| 72 | ok(die->depth == 1, |
| 73 | "bt_dwarf_die_next from non-root DIE - correct depth value"); |
| 74 | |
| 75 | /* Reset DIE to test dwarf_child */ |
| 76 | bt_dwarf_die_destroy(die); |
| 77 | die = bt_dwarf_die_create(cu); |
| 78 | if (!die) { |
| 79 | diag("Failed to create bt_dwarf_die"); |
| 80 | exit(EXIT_FAILURE); |
| 81 | } |
| 82 | |
| 83 | ret = bt_dwarf_die_child(die); |
| 84 | ok(ret == 0, "bt_dwarf_die_child successful"); |
| 85 | ok(die->depth == 1, "bt_dwarf_die_child - correct depth value"); |
| 86 | |
| 87 | ret = bt_dwarf_die_get_tag(die, &tag); |
| 88 | ok(ret == 0, "bt_dwarf_die_get_tag successful"); |
| 89 | ok(tag == DW_TAG_typedef, "bt_dwarf_die_get_tag - correct tag value"); |
| 90 | ret = bt_dwarf_die_get_name(die, &die_name); |
| 91 | ok(ret == 0, "bt_dwarf_die_get_name successful"); |
| 92 | ok(strcmp(die_name, "size_t") == 0, |
| 93 | "bt_dwarf_die_get_name - correct name value"); |
| 94 | |
| 95 | bt_dwarf_die_destroy(die); |
| 96 | bt_dwarf_cu_destroy(cu); |
| 97 | dwarf_end(dwarf_info); |
| 98 | free(die_name); |
| 99 | close(fd); |
| 100 | } |
| 101 | |
| 102 | int main(int argc, char **argv) |
| 103 | { |
| 104 | const char *data_dir; |
| 105 | |
| 106 | plan_tests(NR_TESTS); |
| 107 | |
| 108 | if (argc != 2) { |
| 109 | return EXIT_FAILURE; |
| 110 | } else { |
| 111 | data_dir = argv[1]; |
| 112 | } |
| 113 | |
| 114 | test_bt_dwarf(data_dir); |
| 115 | |
| 116 | return EXIT_SUCCESS; |
| 117 | } |