| 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 | |
| 27 | #include <babeltrace/assert-internal.h> |
| 28 | #include <lttng-utils/debug-info/bin-info.h> |
| 29 | |
| 30 | #include "tap/tap.h" |
| 31 | |
| 32 | #define NR_TESTS 36 |
| 33 | #define SO_NAME "libhello_so" |
| 34 | |
| 35 | #define DWARF_DIR_NAME "dwarf_full" |
| 36 | #define ELF_DIR_NAME "elf_only" |
| 37 | #define BUILDID_DIR_NAME "build_id" |
| 38 | #define DEBUGLINK_DIR_NAME "debug_link" |
| 39 | |
| 40 | #define SO_LOW_ADDR 0x400000 |
| 41 | #define SO_MEMSZ 0x400000 |
| 42 | #define FUNC_FOO_ADDR 0x402367 |
| 43 | #define FUNC_FOO_LINE_NO 36 |
| 44 | #define FUNC_FOO_FILENAME "./libhello.c" |
| 45 | #define FUNC_FOO_TP_ADDR 0x402300 |
| 46 | #define FUNC_FOO_TP_LINE_NO 35 |
| 47 | #define FUNC_FOO_TP_FILENAME "./libhello.c" |
| 48 | #define FUNC_FOO_ADDR_ELF 0x402367 |
| 49 | #define FUNC_FOO_ADDR_DBG_LINK 0x402367 |
| 50 | #define FUNC_FOO_NAME "foo+0xf0" |
| 51 | #define FUNC_FOO_NAME_ELF "foo+0xf0" |
| 52 | #define BUILD_ID_LEN 20 |
| 53 | |
| 54 | char *opt_debug_info_dir; |
| 55 | char *opt_debug_info_target_prefix; |
| 56 | |
| 57 | static |
| 58 | void test_bin_info_build_id(const char *bin_info_dir) |
| 59 | { |
| 60 | int ret; |
| 61 | char *data_dir, *bin_path; |
| 62 | char *func_name = NULL; |
| 63 | struct bin_info *bin = NULL; |
| 64 | struct source_location *src_loc = NULL; |
| 65 | struct bt_fd_cache fdc; |
| 66 | uint8_t build_id[BUILD_ID_LEN] = { |
| 67 | 0xcd, 0xd9, 0x8c, 0xdd, 0x87, 0xf7, 0xfe, 0x64, 0xc1, 0x3b, |
| 68 | 0x6d, 0xaa, 0xd5, 0x53, 0x98, 0x7e, 0xaf, 0xd4, 0x0c, 0xbb |
| 69 | }; |
| 70 | |
| 71 | diag("bin-info tests - separate DWARF via build ID"); |
| 72 | |
| 73 | data_dir = g_build_filename(bin_info_dir, BUILDID_DIR_NAME, NULL); |
| 74 | bin_path = g_build_filename(bin_info_dir, BUILDID_DIR_NAME, SO_NAME, NULL); |
| 75 | |
| 76 | if (data_dir == NULL || bin_path == NULL) { |
| 77 | exit(EXIT_FAILURE); |
| 78 | } |
| 79 | |
| 80 | ret = bt_fd_cache_init(&fdc); |
| 81 | BT_ASSERT(ret == 0); |
| 82 | bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, NULL); |
| 83 | ok(bin != NULL, "bin_info_create successful"); |
| 84 | |
| 85 | /* Test setting build_id */ |
| 86 | ret = bin_info_set_build_id(bin, build_id, BUILD_ID_LEN); |
| 87 | ok(ret == 0, "bin_info_set_build_id successful"); |
| 88 | |
| 89 | /* Test function name lookup (with DWARF) */ |
| 90 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name); |
| 91 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
| 92 | if (func_name) { |
| 93 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, |
| 94 | "bin_info_lookup_function_name - correct func_name value"); |
| 95 | free(func_name); |
| 96 | } else { |
| 97 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); |
| 98 | } |
| 99 | |
| 100 | /* Test source location lookup */ |
| 101 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); |
| 102 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
| 103 | if (src_loc) { |
| 104 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, |
| 105 | "bin_info_lookup_source_location - correct line_no"); |
| 106 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, |
| 107 | "bin_info_lookup_source_location - correct filename"); |
| 108 | source_location_destroy(src_loc); |
| 109 | } else { |
| 110 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); |
| 111 | } |
| 112 | |
| 113 | bin_info_destroy(bin); |
| 114 | bt_fd_cache_fini(&fdc); |
| 115 | g_free(data_dir); |
| 116 | g_free(bin_path); |
| 117 | } |
| 118 | |
| 119 | static |
| 120 | void test_bin_info_debug_link(const char *bin_info_dir) |
| 121 | { |
| 122 | int ret; |
| 123 | char *data_dir, *bin_path; |
| 124 | char *func_name = NULL; |
| 125 | struct bin_info *bin = NULL; |
| 126 | struct source_location *src_loc = NULL; |
| 127 | char *dbg_filename = "libhello_so.debug"; |
| 128 | uint32_t crc = 0x289a8fdc; |
| 129 | struct bt_fd_cache fdc; |
| 130 | |
| 131 | diag("bin-info tests - separate DWARF via debug link"); |
| 132 | |
| 133 | data_dir = g_build_filename(bin_info_dir, DEBUGLINK_DIR_NAME, NULL); |
| 134 | bin_path = g_build_filename(bin_info_dir, DEBUGLINK_DIR_NAME, SO_NAME, NULL); |
| 135 | |
| 136 | if (data_dir == NULL || bin_path == NULL) { |
| 137 | exit(EXIT_FAILURE); |
| 138 | } |
| 139 | |
| 140 | ret = bt_fd_cache_init(&fdc); |
| 141 | BT_ASSERT(ret == 0); |
| 142 | bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, |
| 143 | NULL); |
| 144 | ok(bin != NULL, "bin_info_create successful"); |
| 145 | |
| 146 | /* Test setting debug link */ |
| 147 | ret = bin_info_set_debug_link(bin, dbg_filename, crc); |
| 148 | ok(ret == 0, "bin_info_set_debug_link successful"); |
| 149 | |
| 150 | /* Test function name lookup (with DWARF) */ |
| 151 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_DBG_LINK, |
| 152 | &func_name); |
| 153 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
| 154 | if (func_name) { |
| 155 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, |
| 156 | "bin_info_lookup_function_name - correct func_name value"); |
| 157 | free(func_name); |
| 158 | } else { |
| 159 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); |
| 160 | } |
| 161 | |
| 162 | /* Test source location lookup */ |
| 163 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_DBG_LINK, |
| 164 | &src_loc); |
| 165 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
| 166 | if (src_loc) { |
| 167 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, |
| 168 | "bin_info_lookup_source_location - correct line_no"); |
| 169 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, |
| 170 | "bin_info_lookup_source_location - correct filename"); |
| 171 | source_location_destroy(src_loc); |
| 172 | } else { |
| 173 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); |
| 174 | } |
| 175 | |
| 176 | bin_info_destroy(bin); |
| 177 | bt_fd_cache_fini(&fdc); |
| 178 | g_free(data_dir); |
| 179 | g_free(bin_path); |
| 180 | } |
| 181 | |
| 182 | static |
| 183 | void test_bin_info_elf(const char *bin_info_dir) |
| 184 | { |
| 185 | int ret; |
| 186 | char *data_dir, *bin_path; |
| 187 | char *func_name = NULL; |
| 188 | struct bin_info *bin = NULL; |
| 189 | struct source_location *src_loc = NULL; |
| 190 | struct bt_fd_cache fdc; |
| 191 | |
| 192 | diag("bin-info tests - ELF only"); |
| 193 | |
| 194 | data_dir = g_build_filename(bin_info_dir, ELF_DIR_NAME, NULL); |
| 195 | bin_path = g_build_filename(bin_info_dir, ELF_DIR_NAME, SO_NAME, NULL); |
| 196 | |
| 197 | if (data_dir == NULL || bin_path == NULL) { |
| 198 | exit(EXIT_FAILURE); |
| 199 | } |
| 200 | |
| 201 | ret = bt_fd_cache_init(&fdc); |
| 202 | BT_ASSERT(ret == 0); |
| 203 | bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, NULL); |
| 204 | ok(bin != NULL, "bin_info_create successful"); |
| 205 | |
| 206 | /* Test function name lookup (with ELF) */ |
| 207 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_ELF, &func_name); |
| 208 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
| 209 | if (func_name) { |
| 210 | ok(strcmp(func_name, FUNC_FOO_NAME_ELF) == 0, |
| 211 | "bin_info_lookup_function_name - correct func_name value"); |
| 212 | free(func_name); |
| 213 | func_name = NULL; |
| 214 | } else { |
| 215 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); |
| 216 | } |
| 217 | |
| 218 | /* Test function name lookup - erroneous address */ |
| 219 | ret = bin_info_lookup_function_name(bin, 0, &func_name); |
| 220 | ok(ret == -1 && func_name == NULL, |
| 221 | "bin_info_lookup_function_name - fail on addr not found"); |
| 222 | |
| 223 | /* Test source location location - should fail on ELF only file */ |
| 224 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_ELF, &src_loc); |
| 225 | ok(ret == -1, "bin_info_lookup_source_location - fail on ELF only file"); |
| 226 | |
| 227 | source_location_destroy(src_loc); |
| 228 | bin_info_destroy(bin); |
| 229 | bt_fd_cache_fini(&fdc); |
| 230 | g_free(data_dir); |
| 231 | g_free(bin_path); |
| 232 | } |
| 233 | |
| 234 | static |
| 235 | void test_bin_info(const char *bin_info_dir) |
| 236 | { |
| 237 | int ret; |
| 238 | char *data_dir, *bin_path; |
| 239 | char *func_name = NULL; |
| 240 | struct bin_info *bin = NULL; |
| 241 | struct source_location *src_loc = NULL; |
| 242 | struct bt_fd_cache fdc; |
| 243 | |
| 244 | diag("bin-info tests - DWARF bundled with SO file"); |
| 245 | |
| 246 | |
| 247 | data_dir = g_build_filename(bin_info_dir, DWARF_DIR_NAME, NULL); |
| 248 | bin_path = g_build_filename(bin_info_dir, DWARF_DIR_NAME, SO_NAME, NULL); |
| 249 | |
| 250 | if (data_dir == NULL || bin_path == NULL) { |
| 251 | exit(EXIT_FAILURE); |
| 252 | } |
| 253 | |
| 254 | ret = bt_fd_cache_init(&fdc); |
| 255 | BT_ASSERT(ret == 0); |
| 256 | bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, NULL); |
| 257 | ok(bin != NULL, "bin_info_create successful"); |
| 258 | |
| 259 | /* Test bin_info_has_address */ |
| 260 | ret = bin_info_has_address(bin, 0); |
| 261 | ok(ret == 0, "bin_info_has_address - address under so's range"); |
| 262 | ret = bin_info_has_address(bin, SO_LOW_ADDR); |
| 263 | ok(ret == 1, "bin_info_has_address - lower bound of so's range"); |
| 264 | ret = bin_info_has_address(bin, FUNC_FOO_ADDR); |
| 265 | ok(ret == 1, "bin_info_has_address - address in so's range"); |
| 266 | ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ - 1); |
| 267 | ok(ret == 1, "bin_info_has_address - upper bound of so's range"); |
| 268 | ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ); |
| 269 | ok(ret == 0, "bin_info_has_address - address above so's range"); |
| 270 | |
| 271 | /* Test function name lookup (with DWARF) */ |
| 272 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name); |
| 273 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
| 274 | if (func_name) { |
| 275 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, |
| 276 | "bin_info_lookup_function_name - correct func_name value"); |
| 277 | free(func_name); |
| 278 | func_name = NULL; |
| 279 | } else { |
| 280 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); |
| 281 | } |
| 282 | |
| 283 | /* Test function name lookup - erroneous address */ |
| 284 | ret = bin_info_lookup_function_name(bin, 0, &func_name); |
| 285 | ok(ret == -1 && func_name == NULL, |
| 286 | "bin_info_lookup_function_name - fail on addr not found"); |
| 287 | |
| 288 | /* Test source location lookup */ |
| 289 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); |
| 290 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
| 291 | if (src_loc) { |
| 292 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, |
| 293 | "bin_info_lookup_source_location - correct line_no"); |
| 294 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, |
| 295 | "bin_info_lookup_source_location - correct filename"); |
| 296 | source_location_destroy(src_loc); |
| 297 | src_loc = NULL; |
| 298 | } else { |
| 299 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); |
| 300 | } |
| 301 | |
| 302 | /* Test source location lookup - inlined function */ |
| 303 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_TP_ADDR, &src_loc); |
| 304 | ok(ret == 0, |
| 305 | "bin_info_lookup_source_location (inlined func) successful"); |
| 306 | if (src_loc) { |
| 307 | ok(src_loc->line_no == FUNC_FOO_TP_LINE_NO, |
| 308 | "bin_info_lookup_source_location (inlined func) - correct line_no"); |
| 309 | ok(strcmp(src_loc->filename, FUNC_FOO_TP_FILENAME) == 0, |
| 310 | "bin_info_lookup_source_location (inlined func) - correct filename"); |
| 311 | source_location_destroy(src_loc); |
| 312 | src_loc = NULL; |
| 313 | } else { |
| 314 | skip(2, "bin_info_lookup_source_location (inlined func) - src_loc is NULL"); |
| 315 | } |
| 316 | |
| 317 | /* Test source location lookup - erroneous address */ |
| 318 | ret = bin_info_lookup_source_location(bin, 0, &src_loc); |
| 319 | ok(ret == -1 && src_loc == NULL, |
| 320 | "bin_info_lookup_source_location - fail on addr not found"); |
| 321 | |
| 322 | bin_info_destroy(bin); |
| 323 | bt_fd_cache_fini(&fdc); |
| 324 | g_free(data_dir); |
| 325 | g_free(bin_path); |
| 326 | } |
| 327 | |
| 328 | int main(int argc, char **argv) |
| 329 | { |
| 330 | int ret; |
| 331 | |
| 332 | plan_tests(NR_TESTS); |
| 333 | |
| 334 | if (argc != 2) { |
| 335 | return EXIT_FAILURE; |
| 336 | } else { |
| 337 | opt_debug_info_dir = argv[1]; |
| 338 | } |
| 339 | |
| 340 | ret = bin_info_init(); |
| 341 | ok(ret == 0, "bin_info_init successful"); |
| 342 | |
| 343 | test_bin_info(opt_debug_info_dir); |
| 344 | test_bin_info_elf(opt_debug_info_dir); |
| 345 | test_bin_info_build_id(opt_debug_info_dir); |
| 346 | test_bin_info_debug_link(opt_debug_info_dir); |
| 347 | |
| 348 | return EXIT_SUCCESS; |
| 349 | } |