4 * Babeltrace SO info tests
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Antoine Busque <abusque@efficios.com>
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.
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.
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.
26 #include <babeltrace/so-info.h>
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"
45 #define FUNC_FOO_NAME_ELF "foo+0x24"
46 #define BUILD_ID_LEN 20
48 char *opt_debug_info_dir
;
49 char *opt_debug_info_target_prefix
;
52 void test_so_info_build_id(const char *data_dir
)
56 char *func_name
= NULL
;
57 struct so_info
*so
= 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
64 diag("so-info tests - separate DWARF via build ID");
66 snprintf(path
, PATH_MAX
, "%s/%s", data_dir
, SO_NAME_BUILD_ID
);
68 so
= so_info_create(path
, SO_LOW_ADDR
, SO_MEMSZ
, true);
69 ok(so
!= NULL
, "so_info_create succesful");
71 /* Test setting build_id */
72 ret
= so_info_set_build_id(so
, build_id
, BUILD_ID_LEN
);
73 ok(ret
== 0, "so_info_set_build_id succesful");
75 /* Test function name lookup (with DWARF) */
76 ret
= so_info_lookup_function_name(so
, FUNC_FOO_ADDR
, &func_name
);
77 ok(ret
== 0, "so_info_lookup_function_name succesful");
78 ok(strcmp(func_name
, FUNC_FOO_NAME
) == 0,
79 "so_info_lookup_function_name - correct func_name value");
82 /* Test source location lookup */
83 ret
= so_info_lookup_source_location(so
, FUNC_FOO_ADDR
, &src_loc
);
84 ok(ret
== 0, "so_info_lookup_source_location succesful");
85 ok(src_loc
->line_no
== FUNC_FOO_LINE_NO
,
86 "so_info_lookup_source_location - correct line_no");
87 ok(strcmp(src_loc
->filename
, FUNC_FOO_FILENAME
) == 0,
88 "so_info_lookup_source_location - correct filename");
89 source_location_destroy(src_loc
);
95 void test_so_info_debug_link(const char *data_dir
)
99 char *func_name
= NULL
;
100 struct so_info
*so
= NULL
;
101 struct source_location
*src_loc
= NULL
;
102 char *dbg_filename
= "libhello_debug_link.so.debug";
103 uint32_t crc
= 0xe55c2b98;
105 diag("so-info tests - separate DWARF via debug link");
107 snprintf(path
, PATH_MAX
, "%s/%s", data_dir
, SO_NAME_DEBUG_LINK
);
109 so
= so_info_create(path
, SO_LOW_ADDR
, SO_MEMSZ
, true);
110 ok(so
!= NULL
, "so_info_create succesful");
112 /* Test setting debug link */
113 ret
= so_info_set_debug_link(so
, dbg_filename
, crc
);
114 ok(ret
== 0, "so_info_set_debug_link succesful");
116 /* Test function name lookup (with DWARF) */
117 ret
= so_info_lookup_function_name(so
, FUNC_FOO_ADDR_DBG_LINK
,
119 ok(ret
== 0, "so_info_lookup_function_name succesful");
120 ok(strcmp(func_name
, FUNC_FOO_NAME
) == 0,
121 "so_info_lookup_function_name - correct func_name value");
124 /* Test source location lookup */
125 ret
= so_info_lookup_source_location(so
, FUNC_FOO_ADDR_DBG_LINK
,
127 ok(ret
== 0, "so_info_lookup_source_location succesful");
128 ok(src_loc
->line_no
== FUNC_FOO_LINE_NO
,
129 "so_info_lookup_source_location - correct line_no");
130 ok(strcmp(src_loc
->filename
, FUNC_FOO_FILENAME
) == 0,
131 "so_info_lookup_source_location - correct filename");
132 source_location_destroy(src_loc
);
138 void test_so_info_elf(const char *data_dir
)
142 char *func_name
= NULL
;
143 struct so_info
*so
= NULL
;
144 struct source_location
*src_loc
= NULL
;
146 diag("so-info tests - ELF only");
148 snprintf(path
, PATH_MAX
, "%s/%s", data_dir
, SO_NAME_ELF
);
150 so
= so_info_create(path
, SO_LOW_ADDR
, SO_MEMSZ
, true);
151 ok(so
!= NULL
, "so_info_create succesful");
153 /* Test function name lookup (with ELF) */
154 ret
= so_info_lookup_function_name(so
, FUNC_FOO_ADDR_ELF
, &func_name
);
155 ok(ret
== 0, "so_info_lookup_function_name succesful");
156 ok(strcmp(func_name
, FUNC_FOO_NAME_ELF
) == 0,
157 "so_info_lookup_function_name - correct func_name value");
161 /* Test function name lookup - erroneous address */
162 ret
= so_info_lookup_function_name(so
, 0, &func_name
);
163 ok(ret
== -1 && func_name
== NULL
,
164 "so_info_lookup_function_name - fail on addr not found");
166 /* Test source location location - should fail on ELF only file */
167 ret
= so_info_lookup_source_location(so
, FUNC_FOO_ADDR_ELF
, &src_loc
);
168 ok(ret
== -1, "so_info_lookup_source_location - fail on ELF only file");
170 source_location_destroy(src_loc
);
175 void test_so_info(const char *data_dir
)
179 char *func_name
= NULL
;
180 struct so_info
*so
= NULL
;
181 struct source_location
*src_loc
= NULL
;
183 diag("so-info tests - DWARF bundled with SO file");
185 snprintf(path
, PATH_MAX
, "%s/%s", data_dir
, SO_NAME
);
187 so
= so_info_create(path
, SO_LOW_ADDR
, SO_MEMSZ
, true);
188 ok(so
!= NULL
, "so_info_create succesful");
190 /* Test so_info_has_address */
191 ret
= so_info_has_address(so
, 0);
192 ok(ret
== 0, "so_info_has_address - address under so's range");
193 ret
= so_info_has_address(so
, SO_LOW_ADDR
);
194 ok(ret
== 1, "so_info_has_address - lower bound of so's range");
195 ret
= so_info_has_address(so
, FUNC_FOO_ADDR
);
196 ok(ret
== 1, "so_info_has_address - address in so's range");
197 ret
= so_info_has_address(so
, SO_LOW_ADDR
+ SO_MEMSZ
- 1);
198 ok(ret
== 1, "so_info_has_address - upper bound of so's range");
199 ret
= so_info_has_address(so
, SO_LOW_ADDR
+ SO_MEMSZ
);
200 ok(ret
== 0, "so_info_has_address - address above so's range");
202 /* Test function name lookup (with DWARF) */
203 ret
= so_info_lookup_function_name(so
, FUNC_FOO_ADDR
, &func_name
);
204 ok(ret
== 0, "so_info_lookup_function_name succesful");
205 ok(strcmp(func_name
, FUNC_FOO_NAME
) == 0,
206 "so_info_lookup_function_name - correct func_name value");
210 /* Test function name lookup - erroneous address */
211 ret
= so_info_lookup_function_name(so
, 0, &func_name
);
212 ok(ret
== -1 && func_name
== NULL
,
213 "so_info_lookup_function_name - fail on addr not found");
215 /* Test source location lookup */
216 ret
= so_info_lookup_source_location(so
, FUNC_FOO_ADDR
, &src_loc
);
217 ok(ret
== 0, "so_info_lookup_source_location succesful");
218 ok(src_loc
->line_no
== FUNC_FOO_LINE_NO
,
219 "so_info_lookup_source_location - correct line_no");
220 ok(strcmp(src_loc
->filename
, FUNC_FOO_FILENAME
) == 0,
221 "so_info_lookup_source_location - correct filename");
222 source_location_destroy(src_loc
);
225 /* Test source location lookup - inlined function */
226 ret
= so_info_lookup_source_location(so
, FUNC_FOO_TP_ADDR
, &src_loc
);
228 "so_info_lookup_source_location (inlined func) succesful");
229 ok(src_loc
->line_no
== FUNC_FOO_TP_LINE_NO
,
230 "so_info_lookup_source_location (inlined func) - correct line_no");
231 ok(strcmp(src_loc
->filename
, FUNC_FOO_TP_FILENAME
) == 0,
232 "so_info_lookup_source_location (inlined func) - correct filename");
233 source_location_destroy(src_loc
);
236 /* Test source location lookup - erroneous address */
237 ret
= so_info_lookup_source_location(so
, 0, &src_loc
);
238 ok(ret
== -1 && src_loc
== NULL
,
239 "so_info_lookup_source_location - fail on addr not found");
244 int main(int argc
, char **argv
)
248 plan_tests(NR_TESTS
);
253 opt_debug_info_dir
= argv
[1];
256 ret
= so_info_init();
257 ok(ret
== 0, "so_info_init succesful");
259 test_so_info(opt_debug_info_dir
);
260 test_so_info_elf(opt_debug_info_dir
);
261 test_so_info_build_id(opt_debug_info_dir
);
262 test_so_info_debug_link(opt_debug_info_dir
);