Cleanup: remove private babeltrace.h
[babeltrace.git] / tests / plugins / 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 == NULL) {
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 == NULL, "dwarf_begin failed as expected");
61 }
62
63 if (dwarf_info != NULL) {
64 dwarf_end(dwarf_info);
65 }
66 close(fd);
67 g_free(path);
68 }
69
70 /*
71 * Test with a proper ELF file with DWARF.
72 */
73 static
74 void test_bt_dwarf(const char *data_dir)
75 {
76 int fd, ret, tag;
77 char *path;
78 char *die_name = NULL;
79 struct bt_dwarf_cu *cu = NULL;
80 struct bt_dwarf_die *die = NULL;
81 Dwarf *dwarf_info = NULL;
82
83 path = g_build_filename(data_dir, DWARF_DIR_NAME, SO_NAME, NULL);
84 if (path == NULL) {
85 diag("Failed to allocate memory for path");
86 exit(EXIT_FAILURE);
87 }
88
89 fd = open(path, O_RDONLY);
90 ok(fd >= 0, "Open DWARF file %s", path);
91 if (fd < 0) {
92 exit(EXIT_FAILURE);
93 }
94 dwarf_info = dwarf_begin(fd, DWARF_C_READ);
95 ok(dwarf_info != NULL, "dwarf_begin successful");
96 cu = bt_dwarf_cu_create(dwarf_info);
97 ok(cu != NULL, "bt_dwarf_cu_create successful");
98 ret = bt_dwarf_cu_next(cu);
99 ok(ret == 0, "bt_dwarf_cu_next successful");
100 die = bt_dwarf_die_create(cu);
101 ok(die != NULL, "bt_dwarf_die_create successful");
102 if (!die) {
103 exit(EXIT_FAILURE);
104 }
105 /*
106 * Test bt_dwarf_die_next twice, as the code path is different
107 * for DIEs at depth 0 (just created) and other depths.
108 */
109 ret = bt_dwarf_die_next(die);
110 ok(ret == 0, "bt_dwarf_die_next from root DIE successful");
111 ok(die->depth == 1,
112 "bt_dwarf_die_next from root DIE - correct depth value");
113 ret = bt_dwarf_die_next(die);
114 ok(ret == 0,
115 "bt_dwarf_die_next from non-root DIE successful");
116 ok(die->depth == 1,
117 "bt_dwarf_die_next from non-root DIE - correct depth value");
118
119 /* Reset DIE to test dwarf_child */
120 bt_dwarf_die_destroy(die);
121 die = bt_dwarf_die_create(cu);
122 if (!die) {
123 diag("Failed to create bt_dwarf_die");
124 exit(EXIT_FAILURE);
125 }
126
127 ret = bt_dwarf_die_child(die);
128 ok(ret == 0, "bt_dwarf_die_child successful");
129 ok(die->depth == 1, "bt_dwarf_die_child - correct depth value");
130
131 ret = bt_dwarf_die_get_tag(die, &tag);
132 ok(ret == 0, "bt_dwarf_die_get_tag successful");
133 ok(tag == DW_TAG_typedef, "bt_dwarf_die_get_tag - correct tag value");
134 ret = bt_dwarf_die_get_name(die, &die_name);
135 ok(ret == 0, "bt_dwarf_die_get_name successful");
136 ok(strcmp(die_name, "size_t") == 0,
137 "bt_dwarf_die_get_name - correct name value");
138
139 bt_dwarf_die_destroy(die);
140 bt_dwarf_cu_destroy(cu);
141 dwarf_end(dwarf_info);
142 free(die_name);
143 close(fd);
144 g_free(path);
145 }
146
147 int main(int argc, char **argv)
148 {
149 const char *data_dir;
150
151 plan_tests(NR_TESTS);
152
153 if (argc != 2) {
154 return EXIT_FAILURE;
155 } else {
156 data_dir = argv[1];
157 }
158
159 test_bt_no_dwarf(data_dir);
160 test_bt_dwarf(data_dir);
161
162 return EXIT_SUCCESS;
163 }
This page took 0.048715 seconds and 4 git commands to generate.