Commit | Line | Data |
---|---|---|
33ba099b | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: GPL-2.0-only |
33ba099b | 3 | * |
0235b0db MJ |
4 | * Copyright (C) 2015 EfficiOS Inc. and Linux Foundation |
5 | * Copyright (C) 2015 Antoine Busque <abusque@efficios.com> | |
33ba099b | 6 | * |
0235b0db | 7 | * Babeltrace bt_dwarf (DWARF utilities) tests |
33ba099b AB |
8 | */ |
9 | ||
10 | #include <fcntl.h> | |
91d81473 | 11 | #include <glib.h> |
33ba099b AB |
12 | #include <stdlib.h> |
13 | #include <stdint.h> | |
14 | #include <string.h> | |
15 | #include <unistd.h> | |
c283522b | 16 | #include <lttng-utils/debug-info/dwarf.h> |
33ba099b AB |
17 | #include "tap/tap.h" |
18 | ||
8e65ce6d | 19 | #define NR_TESTS 17 |
33ba099b | 20 | |
6a284733 MJ |
21 | #define SO_NAME "libhello_so" |
22 | #define DWARF_DIR_NAME "dwarf_full" | |
8e65ce6d | 23 | #define ELF_DIR_NAME "elf_only" |
6a284733 | 24 | |
8e65ce6d MJ |
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); | |
5084732e | 36 | if (!path) { |
8e65ce6d MJ |
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); | |
5084732e | 47 | ok(!dwarf_info, "dwarf_begin failed as expected"); |
8e65ce6d MJ |
48 | } |
49 | ||
5084732e | 50 | if (dwarf_info) { |
8e65ce6d MJ |
51 | dwarf_end(dwarf_info); |
52 | } | |
eda096d7 FD |
53 | |
54 | if (fd >= 0) { | |
55 | close(fd); | |
56 | } | |
8e65ce6d MJ |
57 | g_free(path); |
58 | } | |
59 | ||
60 | /* | |
61 | * Test with a proper ELF file with DWARF. | |
62 | */ | |
33ba099b AB |
63 | static |
64 | void test_bt_dwarf(const char *data_dir) | |
65 | { | |
66 | int fd, ret, tag; | |
6a284733 | 67 | char *path; |
33ba099b AB |
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 | ||
6a284733 | 73 | path = g_build_filename(data_dir, DWARF_DIR_NAME, SO_NAME, NULL); |
5084732e | 74 | if (!path) { |
8e65ce6d | 75 | diag("Failed to allocate memory for path"); |
6a284733 MJ |
76 | exit(EXIT_FAILURE); |
77 | } | |
33ba099b AB |
78 | |
79 | fd = open(path, O_RDONLY); | |
80 | ok(fd >= 0, "Open DWARF file %s", path); | |
8c4bb40b JG |
81 | if (fd < 0) { |
82 | exit(EXIT_FAILURE); | |
83 | } | |
33ba099b | 84 | dwarf_info = dwarf_begin(fd, DWARF_C_READ); |
5084732e | 85 | ok(dwarf_info, "dwarf_begin successful"); |
33ba099b | 86 | cu = bt_dwarf_cu_create(dwarf_info); |
5084732e | 87 | ok(cu, "bt_dwarf_cu_create successful"); |
33ba099b AB |
88 | ret = bt_dwarf_cu_next(cu); |
89 | ok(ret == 0, "bt_dwarf_cu_next successful"); | |
90 | die = bt_dwarf_die_create(cu); | |
5084732e | 91 | ok(die, "bt_dwarf_die_create successful"); |
b1d10d85 JG |
92 | if (!die) { |
93 | exit(EXIT_FAILURE); | |
94 | } | |
33ba099b AB |
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); | |
b1d10d85 JG |
112 | if (!die) { |
113 | diag("Failed to create bt_dwarf_die"); | |
114 | exit(EXIT_FAILURE); | |
115 | } | |
33ba099b AB |
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); | |
6a284733 | 134 | g_free(path); |
33ba099b AB |
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 | ||
8e65ce6d | 149 | test_bt_no_dwarf(data_dir); |
33ba099b AB |
150 | test_bt_dwarf(data_dir); |
151 | ||
d6641c89 | 152 | return exit_status(); |
33ba099b | 153 | } |