Typo: succesful* -> successful*
[babeltrace.git] / tests / lib / test_bin_info.c
1 /*
2 * test_bin_info.c
3 *
4 * Babeltrace SO info 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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <babeltrace/bin-info.h>
27 #include "tap/tap.h"
28
29 #define NR_TESTS 36
30 #define SO_NAME "libhello.so"
31 #define SO_NAME_ELF "libhello_elf.so"
32 #define SO_NAME_BUILD_ID "libhello_build_id.so"
33 #define SO_NAME_DEBUG_LINK "libhello_debug_link.so"
34 #define SO_LOW_ADDR 0x400000
35 #define SO_MEMSZ 0x400000
36 #define FUNC_FOO_ADDR 0x4014ee
37 #define FUNC_FOO_LINE_NO 8
38 #define FUNC_FOO_FILENAME "/efficios/libhello.c"
39 #define FUNC_FOO_TP_ADDR 0x4014d3
40 #define FUNC_FOO_TP_LINE_NO 7
41 #define FUNC_FOO_TP_FILENAME "/efficios/libhello.c"
42 #define FUNC_FOO_ADDR_ELF 0x4013ef
43 #define FUNC_FOO_ADDR_DBG_LINK 0x40148e
44 #define FUNC_FOO_NAME "foo+0xc3"
45 #define FUNC_FOO_NAME_ELF "foo+0x24"
46 #define BUILD_ID_LEN 20
47
48 char *opt_debug_info_dir;
49 char *opt_debug_info_target_prefix;
50
51 static
52 void test_bin_info_build_id(const char *data_dir)
53 {
54 int ret;
55 char path[PATH_MAX];
56 char *func_name = NULL;
57 struct bin_info *bin = NULL;
58 struct source_location *src_loc = NULL;
59 uint8_t build_id[BUILD_ID_LEN] = {
60 0xcd, 0xd9, 0x8c, 0xdd, 0x87, 0xf7, 0xfe, 0x64, 0xc1, 0x3b,
61 0x6d, 0xaa, 0xd5, 0x53, 0x98, 0x7e, 0xaf, 0xd4, 0x0c, 0xbb
62 };
63
64 diag("bin-info tests - separate DWARF via build ID");
65
66 snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_BUILD_ID);
67
68 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
69 ok(bin != NULL, "bin_info_create successful");
70
71 /* Test setting build_id */
72 ret = bin_info_set_build_id(bin, build_id, BUILD_ID_LEN);
73 ok(ret == 0, "bin_info_set_build_id successful");
74
75 /* Test function name lookup (with DWARF) */
76 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name);
77 ok(ret == 0, "bin_info_lookup_function_name successful");
78 ok(strcmp(func_name, FUNC_FOO_NAME) == 0,
79 "bin_info_lookup_function_name - correct func_name value");
80 free(func_name);
81
82 /* Test source location lookup */
83 ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc);
84 ok(ret == 0, "bin_info_lookup_source_location successful");
85 ok(src_loc->line_no == FUNC_FOO_LINE_NO,
86 "bin_info_lookup_source_location - correct line_no");
87 ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0,
88 "bin_info_lookup_source_location - correct filename");
89 source_location_destroy(src_loc);
90
91 bin_info_destroy(bin);
92 }
93
94 static
95 void test_bin_info_debug_link(const char *data_dir)
96 {
97 int ret;
98 char path[PATH_MAX];
99 char *func_name = NULL;
100 struct bin_info *bin = NULL;
101 struct source_location *src_loc = NULL;
102 char *dbg_filename = "libhello_debug_link.so.debug";
103 uint32_t crc = 0xe55c2b98;
104
105 diag("bin-info tests - separate DWARF via debug link");
106
107 snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_DEBUG_LINK);
108
109 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
110 ok(bin != NULL, "bin_info_create successful");
111
112 /* Test setting debug link */
113 ret = bin_info_set_debug_link(bin, dbg_filename, crc);
114 ok(ret == 0, "bin_info_set_debug_link successful");
115
116 /* Test function name lookup (with DWARF) */
117 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_DBG_LINK,
118 &func_name);
119 ok(ret == 0, "bin_info_lookup_function_name successful");
120 ok(strcmp(func_name, FUNC_FOO_NAME) == 0,
121 "bin_info_lookup_function_name - correct func_name value");
122 free(func_name);
123
124 /* Test source location lookup */
125 ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_DBG_LINK,
126 &src_loc);
127 ok(ret == 0, "bin_info_lookup_source_location successful");
128 ok(src_loc->line_no == FUNC_FOO_LINE_NO,
129 "bin_info_lookup_source_location - correct line_no");
130 ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0,
131 "bin_info_lookup_source_location - correct filename");
132 source_location_destroy(src_loc);
133
134 bin_info_destroy(bin);
135 }
136
137 static
138 void test_bin_info_elf(const char *data_dir)
139 {
140 int ret;
141 char path[PATH_MAX];
142 char *func_name = NULL;
143 struct bin_info *bin = NULL;
144 struct source_location *src_loc = NULL;
145
146 diag("bin-info tests - ELF only");
147
148 snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_ELF);
149
150 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
151 ok(bin != NULL, "bin_info_create successful");
152
153 /* Test function name lookup (with ELF) */
154 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_ELF, &func_name);
155 ok(ret == 0, "bin_info_lookup_function_name successful");
156 ok(strcmp(func_name, FUNC_FOO_NAME_ELF) == 0,
157 "bin_info_lookup_function_name - correct func_name value");
158 free(func_name);
159 func_name = NULL;
160
161 /* Test function name lookup - erroneous address */
162 ret = bin_info_lookup_function_name(bin, 0, &func_name);
163 ok(ret == -1 && func_name == NULL,
164 "bin_info_lookup_function_name - fail on addr not found");
165
166 /* Test source location location - should fail on ELF only file */
167 ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_ELF, &src_loc);
168 ok(ret == -1, "bin_info_lookup_source_location - fail on ELF only file");
169
170 source_location_destroy(src_loc);
171 bin_info_destroy(bin);
172 }
173
174 static
175 void test_bin_info(const char *data_dir)
176 {
177 int ret;
178 char path[PATH_MAX];
179 char *func_name = NULL;
180 struct bin_info *bin = NULL;
181 struct source_location *src_loc = NULL;
182
183 diag("bin-info tests - DWARF bundled with SO file");
184
185 snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME);
186
187 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
188 ok(bin != NULL, "bin_info_create successful");
189
190 /* Test bin_info_has_address */
191 ret = bin_info_has_address(bin, 0);
192 ok(ret == 0, "bin_info_has_address - address under so's range");
193 ret = bin_info_has_address(bin, SO_LOW_ADDR);
194 ok(ret == 1, "bin_info_has_address - lower bound of so's range");
195 ret = bin_info_has_address(bin, FUNC_FOO_ADDR);
196 ok(ret == 1, "bin_info_has_address - address in so's range");
197 ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ - 1);
198 ok(ret == 1, "bin_info_has_address - upper bound of so's range");
199 ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ);
200 ok(ret == 0, "bin_info_has_address - address above so's range");
201
202 /* Test function name lookup (with DWARF) */
203 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name);
204 ok(ret == 0, "bin_info_lookup_function_name successful");
205 ok(strcmp(func_name, FUNC_FOO_NAME) == 0,
206 "bin_info_lookup_function_name - correct func_name value");
207 free(func_name);
208 func_name = NULL;
209
210 /* Test function name lookup - erroneous address */
211 ret = bin_info_lookup_function_name(bin, 0, &func_name);
212 ok(ret == -1 && func_name == NULL,
213 "bin_info_lookup_function_name - fail on addr not found");
214
215 /* Test source location lookup */
216 ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc);
217 ok(ret == 0, "bin_info_lookup_source_location successful");
218 ok(src_loc->line_no == FUNC_FOO_LINE_NO,
219 "bin_info_lookup_source_location - correct line_no");
220 ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0,
221 "bin_info_lookup_source_location - correct filename");
222 source_location_destroy(src_loc);
223 src_loc = NULL;
224
225 /* Test source location lookup - inlined function */
226 ret = bin_info_lookup_source_location(bin, FUNC_FOO_TP_ADDR, &src_loc);
227 ok(ret == 0,
228 "bin_info_lookup_source_location (inlined func) successful");
229 ok(src_loc->line_no == FUNC_FOO_TP_LINE_NO,
230 "bin_info_lookup_source_location (inlined func) - correct line_no");
231 ok(strcmp(src_loc->filename, FUNC_FOO_TP_FILENAME) == 0,
232 "bin_info_lookup_source_location (inlined func) - correct filename");
233 source_location_destroy(src_loc);
234 src_loc = NULL;
235
236 /* Test source location lookup - erroneous address */
237 ret = bin_info_lookup_source_location(bin, 0, &src_loc);
238 ok(ret == -1 && src_loc == NULL,
239 "bin_info_lookup_source_location - fail on addr not found");
240
241 bin_info_destroy(bin);
242 }
243
244 int main(int argc, char **argv)
245 {
246 int ret;
247
248 plan_tests(NR_TESTS);
249
250 if (argc != 2) {
251 return EXIT_FAILURE;
252 } else {
253 opt_debug_info_dir = argv[1];
254 }
255
256 ret = bin_info_init();
257 ok(ret == 0, "bin_info_init successful");
258
259 test_bin_info(opt_debug_info_dir);
260 test_bin_info_elf(opt_debug_info_dir);
261 test_bin_info_build_id(opt_debug_info_dir);
262 test_bin_info_debug_link(opt_debug_info_dir);
263
264 return EXIT_SUCCESS;
265 }
This page took 0.033519 seconds and 4 git commands to generate.