Typo: succesful* -> successful*
[babeltrace.git] / tests / lib / test_bin_info.c
CommitLineData
6ab5bdaa 1/*
d5ddf820 2 * test_bin_info.c
6ab5bdaa
AB
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>
d5ddf820 26#include <babeltrace/bin-info.h>
6ab5bdaa
AB
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
ff334941 44#define FUNC_FOO_NAME "foo+0xc3"
6ab5bdaa
AB
45#define FUNC_FOO_NAME_ELF "foo+0x24"
46#define BUILD_ID_LEN 20
47
05984e0c 48char *opt_debug_info_dir;
5cde0dc1 49char *opt_debug_info_target_prefix;
6ab5bdaa
AB
50
51static
d5ddf820 52void test_bin_info_build_id(const char *data_dir)
6ab5bdaa
AB
53{
54 int ret;
55 char path[PATH_MAX];
56 char *func_name = NULL;
d5ddf820 57 struct bin_info *bin = NULL;
6ab5bdaa
AB
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
d5ddf820 64 diag("bin-info tests - separate DWARF via build ID");
6ab5bdaa
AB
65
66 snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_BUILD_ID);
67
d5ddf820 68 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
4caab45b 69 ok(bin != NULL, "bin_info_create successful");
6ab5bdaa
AB
70
71 /* Test setting build_id */
d5ddf820 72 ret = bin_info_set_build_id(bin, build_id, BUILD_ID_LEN);
4caab45b 73 ok(ret == 0, "bin_info_set_build_id successful");
6ab5bdaa
AB
74
75 /* Test function name lookup (with DWARF) */
d5ddf820 76 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name);
4caab45b 77 ok(ret == 0, "bin_info_lookup_function_name successful");
6ab5bdaa 78 ok(strcmp(func_name, FUNC_FOO_NAME) == 0,
d5ddf820 79 "bin_info_lookup_function_name - correct func_name value");
6ab5bdaa
AB
80 free(func_name);
81
82 /* Test source location lookup */
d5ddf820 83 ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc);
4caab45b 84 ok(ret == 0, "bin_info_lookup_source_location successful");
6ab5bdaa 85 ok(src_loc->line_no == FUNC_FOO_LINE_NO,
d5ddf820 86 "bin_info_lookup_source_location - correct line_no");
6ab5bdaa 87 ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0,
d5ddf820 88 "bin_info_lookup_source_location - correct filename");
6ab5bdaa
AB
89 source_location_destroy(src_loc);
90
d5ddf820 91 bin_info_destroy(bin);
6ab5bdaa
AB
92}
93
94static
d5ddf820 95void test_bin_info_debug_link(const char *data_dir)
6ab5bdaa
AB
96{
97 int ret;
98 char path[PATH_MAX];
99 char *func_name = NULL;
d5ddf820 100 struct bin_info *bin = NULL;
6ab5bdaa
AB
101 struct source_location *src_loc = NULL;
102 char *dbg_filename = "libhello_debug_link.so.debug";
103 uint32_t crc = 0xe55c2b98;
104
d5ddf820 105 diag("bin-info tests - separate DWARF via debug link");
6ab5bdaa
AB
106
107 snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_DEBUG_LINK);
108
d5ddf820 109 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
4caab45b 110 ok(bin != NULL, "bin_info_create successful");
6ab5bdaa
AB
111
112 /* Test setting debug link */
d5ddf820 113 ret = bin_info_set_debug_link(bin, dbg_filename, crc);
4caab45b 114 ok(ret == 0, "bin_info_set_debug_link successful");
6ab5bdaa
AB
115
116 /* Test function name lookup (with DWARF) */
d5ddf820 117 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_DBG_LINK,
6ab5bdaa 118 &func_name);
4caab45b 119 ok(ret == 0, "bin_info_lookup_function_name successful");
6ab5bdaa 120 ok(strcmp(func_name, FUNC_FOO_NAME) == 0,
d5ddf820 121 "bin_info_lookup_function_name - correct func_name value");
6ab5bdaa
AB
122 free(func_name);
123
124 /* Test source location lookup */
d5ddf820 125 ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_DBG_LINK,
6ab5bdaa 126 &src_loc);
4caab45b 127 ok(ret == 0, "bin_info_lookup_source_location successful");
6ab5bdaa 128 ok(src_loc->line_no == FUNC_FOO_LINE_NO,
d5ddf820 129 "bin_info_lookup_source_location - correct line_no");
6ab5bdaa 130 ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0,
d5ddf820 131 "bin_info_lookup_source_location - correct filename");
6ab5bdaa
AB
132 source_location_destroy(src_loc);
133
d5ddf820 134 bin_info_destroy(bin);
6ab5bdaa
AB
135}
136
137static
d5ddf820 138void test_bin_info_elf(const char *data_dir)
6ab5bdaa
AB
139{
140 int ret;
141 char path[PATH_MAX];
142 char *func_name = NULL;
d5ddf820 143 struct bin_info *bin = NULL;
6ab5bdaa
AB
144 struct source_location *src_loc = NULL;
145
d5ddf820 146 diag("bin-info tests - ELF only");
6ab5bdaa
AB
147
148 snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_ELF);
149
d5ddf820 150 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
4caab45b 151 ok(bin != NULL, "bin_info_create successful");
6ab5bdaa
AB
152
153 /* Test function name lookup (with ELF) */
d5ddf820 154 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_ELF, &func_name);
4caab45b 155 ok(ret == 0, "bin_info_lookup_function_name successful");
6ab5bdaa 156 ok(strcmp(func_name, FUNC_FOO_NAME_ELF) == 0,
d5ddf820 157 "bin_info_lookup_function_name - correct func_name value");
6ab5bdaa
AB
158 free(func_name);
159 func_name = NULL;
160
161 /* Test function name lookup - erroneous address */
d5ddf820 162 ret = bin_info_lookup_function_name(bin, 0, &func_name);
6ab5bdaa 163 ok(ret == -1 && func_name == NULL,
d5ddf820 164 "bin_info_lookup_function_name - fail on addr not found");
6ab5bdaa
AB
165
166 /* Test source location location - should fail on ELF only file */
d5ddf820
AB
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");
6ab5bdaa
AB
169
170 source_location_destroy(src_loc);
d5ddf820 171 bin_info_destroy(bin);
6ab5bdaa
AB
172}
173
174static
d5ddf820 175void test_bin_info(const char *data_dir)
6ab5bdaa
AB
176{
177 int ret;
178 char path[PATH_MAX];
179 char *func_name = NULL;
d5ddf820 180 struct bin_info *bin = NULL;
6ab5bdaa
AB
181 struct source_location *src_loc = NULL;
182
d5ddf820 183 diag("bin-info tests - DWARF bundled with SO file");
6ab5bdaa
AB
184
185 snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME);
186
d5ddf820 187 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
4caab45b 188 ok(bin != NULL, "bin_info_create successful");
d5ddf820
AB
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");
6ab5bdaa
AB
201
202 /* Test function name lookup (with DWARF) */
d5ddf820 203 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name);
4caab45b 204 ok(ret == 0, "bin_info_lookup_function_name successful");
6ab5bdaa 205 ok(strcmp(func_name, FUNC_FOO_NAME) == 0,
d5ddf820 206 "bin_info_lookup_function_name - correct func_name value");
6ab5bdaa
AB
207 free(func_name);
208 func_name = NULL;
209
210 /* Test function name lookup - erroneous address */
d5ddf820 211 ret = bin_info_lookup_function_name(bin, 0, &func_name);
6ab5bdaa 212 ok(ret == -1 && func_name == NULL,
d5ddf820 213 "bin_info_lookup_function_name - fail on addr not found");
6ab5bdaa
AB
214
215 /* Test source location lookup */
d5ddf820 216 ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc);
4caab45b 217 ok(ret == 0, "bin_info_lookup_source_location successful");
6ab5bdaa 218 ok(src_loc->line_no == FUNC_FOO_LINE_NO,
d5ddf820 219 "bin_info_lookup_source_location - correct line_no");
6ab5bdaa 220 ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0,
d5ddf820 221 "bin_info_lookup_source_location - correct filename");
6ab5bdaa
AB
222 source_location_destroy(src_loc);
223 src_loc = NULL;
224
225 /* Test source location lookup - inlined function */
d5ddf820 226 ret = bin_info_lookup_source_location(bin, FUNC_FOO_TP_ADDR, &src_loc);
6ab5bdaa 227 ok(ret == 0,
4caab45b 228 "bin_info_lookup_source_location (inlined func) successful");
6ab5bdaa 229 ok(src_loc->line_no == FUNC_FOO_TP_LINE_NO,
d5ddf820 230 "bin_info_lookup_source_location (inlined func) - correct line_no");
6ab5bdaa 231 ok(strcmp(src_loc->filename, FUNC_FOO_TP_FILENAME) == 0,
d5ddf820 232 "bin_info_lookup_source_location (inlined func) - correct filename");
6ab5bdaa
AB
233 source_location_destroy(src_loc);
234 src_loc = NULL;
235
236 /* Test source location lookup - erroneous address */
d5ddf820 237 ret = bin_info_lookup_source_location(bin, 0, &src_loc);
6ab5bdaa 238 ok(ret == -1 && src_loc == NULL,
d5ddf820 239 "bin_info_lookup_source_location - fail on addr not found");
6ab5bdaa 240
d5ddf820 241 bin_info_destroy(bin);
6ab5bdaa
AB
242}
243
244int 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 {
05984e0c 253 opt_debug_info_dir = argv[1];
6ab5bdaa
AB
254 }
255
d5ddf820 256 ret = bin_info_init();
4caab45b 257 ok(ret == 0, "bin_info_init successful");
6ab5bdaa 258
d5ddf820
AB
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);
6ab5bdaa
AB
263
264 return EXIT_SUCCESS;
265}
This page took 0.035953 seconds and 4 git commands to generate.