01a7b5172500d0a1716dd6e038420f8b88eccac6
[babeltrace.git] / tests / plugins / flt.lttng-utils.debug-info / test_dwarf.c
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 <glib.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <lttng-utils/debug-info/dwarf.h>
30 #include "tap/tap.h"
31
32 #define NR_TESTS 17
33
34 #define SO_NAME "libhello_so"
35 #define DWARF_DIR_NAME "dwarf_full"
36 #define ELF_DIR_NAME "elf_only"
37
38 /*
39 * Test that we fail on an ELF file without DWARF.
40 */
41 static
42 void test_bt_no_dwarf(const char *data_dir)
43 {
44 int fd;
45 char *path;
46 Dwarf *dwarf_info = NULL;
47
48 path = g_build_filename(data_dir, ELF_DIR_NAME, SO_NAME, NULL);
49 if (!path) {
50 diag("Failed to allocate memory for path");
51 exit(EXIT_FAILURE);
52 }
53
54 fd = open(path, O_RDONLY);
55 ok(fd >= 0, "Open ELF file %s", path);
56 if (fd < 0) {
57 skip(1, "dwarf_begin failed as expected");
58 } else {
59 dwarf_info = dwarf_begin(fd, DWARF_C_READ);
60 ok(!dwarf_info, "dwarf_begin failed as expected");
61 }
62
63 if (dwarf_info) {
64 dwarf_end(dwarf_info);
65 }
66
67 if (fd >= 0) {
68 close(fd);
69 }
70 g_free(path);
71 }
72
73 /*
74 * Test with a proper ELF file with DWARF.
75 */
76 static
77 void test_bt_dwarf(const char *data_dir)
78 {
79 int fd, ret, tag;
80 char *path;
81 char *die_name = NULL;
82 struct bt_dwarf_cu *cu = NULL;
83 struct bt_dwarf_die *die = NULL;
84 Dwarf *dwarf_info = NULL;
85
86 path = g_build_filename(data_dir, DWARF_DIR_NAME, SO_NAME, NULL);
87 if (!path) {
88 diag("Failed to allocate memory for path");
89 exit(EXIT_FAILURE);
90 }
91
92 fd = open(path, O_RDONLY);
93 ok(fd >= 0, "Open DWARF file %s", path);
94 if (fd < 0) {
95 exit(EXIT_FAILURE);
96 }
97 dwarf_info = dwarf_begin(fd, DWARF_C_READ);
98 ok(dwarf_info, "dwarf_begin successful");
99 cu = bt_dwarf_cu_create(dwarf_info);
100 ok(cu, "bt_dwarf_cu_create successful");
101 ret = bt_dwarf_cu_next(cu);
102 ok(ret == 0, "bt_dwarf_cu_next successful");
103 die = bt_dwarf_die_create(cu);
104 ok(die, "bt_dwarf_die_create successful");
105 if (!die) {
106 exit(EXIT_FAILURE);
107 }
108 /*
109 * Test bt_dwarf_die_next twice, as the code path is different
110 * for DIEs at depth 0 (just created) and other depths.
111 */
112 ret = bt_dwarf_die_next(die);
113 ok(ret == 0, "bt_dwarf_die_next from root DIE successful");
114 ok(die->depth == 1,
115 "bt_dwarf_die_next from root DIE - correct depth value");
116 ret = bt_dwarf_die_next(die);
117 ok(ret == 0,
118 "bt_dwarf_die_next from non-root DIE successful");
119 ok(die->depth == 1,
120 "bt_dwarf_die_next from non-root DIE - correct depth value");
121
122 /* Reset DIE to test dwarf_child */
123 bt_dwarf_die_destroy(die);
124 die = bt_dwarf_die_create(cu);
125 if (!die) {
126 diag("Failed to create bt_dwarf_die");
127 exit(EXIT_FAILURE);
128 }
129
130 ret = bt_dwarf_die_child(die);
131 ok(ret == 0, "bt_dwarf_die_child successful");
132 ok(die->depth == 1, "bt_dwarf_die_child - correct depth value");
133
134 ret = bt_dwarf_die_get_tag(die, &tag);
135 ok(ret == 0, "bt_dwarf_die_get_tag successful");
136 ok(tag == DW_TAG_typedef, "bt_dwarf_die_get_tag - correct tag value");
137 ret = bt_dwarf_die_get_name(die, &die_name);
138 ok(ret == 0, "bt_dwarf_die_get_name successful");
139 ok(strcmp(die_name, "size_t") == 0,
140 "bt_dwarf_die_get_name - correct name value");
141
142 bt_dwarf_die_destroy(die);
143 bt_dwarf_cu_destroy(cu);
144 dwarf_end(dwarf_info);
145 free(die_name);
146 close(fd);
147 g_free(path);
148 }
149
150 int main(int argc, char **argv)
151 {
152 const char *data_dir;
153
154 plan_tests(NR_TESTS);
155
156 if (argc != 2) {
157 return EXIT_FAILURE;
158 } else {
159 data_dir = argv[1];
160 }
161
162 test_bt_no_dwarf(data_dir);
163 test_bt_dwarf(data_dir);
164
165 return EXIT_SUCCESS;
166 }
This page took 0.035204 seconds and 3 git commands to generate.