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