| 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 | if (func_name) { |
| 79 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, |
| 80 | "bin_info_lookup_function_name - correct func_name value"); |
| 81 | free(func_name); |
| 82 | } else { |
| 83 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); |
| 84 | } |
| 85 | |
| 86 | /* Test source location lookup */ |
| 87 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); |
| 88 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
| 89 | if (src_loc) { |
| 90 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, |
| 91 | "bin_info_lookup_source_location - correct line_no"); |
| 92 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, |
| 93 | "bin_info_lookup_source_location - correct filename"); |
| 94 | source_location_destroy(src_loc); |
| 95 | } else { |
| 96 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); |
| 97 | } |
| 98 | |
| 99 | bin_info_destroy(bin); |
| 100 | } |
| 101 | |
| 102 | static |
| 103 | void test_bin_info_debug_link(const char *data_dir) |
| 104 | { |
| 105 | int ret; |
| 106 | char path[PATH_MAX]; |
| 107 | char *func_name = NULL; |
| 108 | struct bin_info *bin = NULL; |
| 109 | struct source_location *src_loc = NULL; |
| 110 | char *dbg_filename = "libhello_debug_link.so.debug"; |
| 111 | uint32_t crc = 0xe55c2b98; |
| 112 | |
| 113 | diag("bin-info tests - separate DWARF via debug link"); |
| 114 | |
| 115 | snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_DEBUG_LINK); |
| 116 | |
| 117 | bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); |
| 118 | ok(bin != NULL, "bin_info_create successful"); |
| 119 | |
| 120 | /* Test setting debug link */ |
| 121 | ret = bin_info_set_debug_link(bin, dbg_filename, crc); |
| 122 | ok(ret == 0, "bin_info_set_debug_link successful"); |
| 123 | |
| 124 | /* Test function name lookup (with DWARF) */ |
| 125 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_DBG_LINK, |
| 126 | &func_name); |
| 127 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
| 128 | if (func_name) { |
| 129 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, |
| 130 | "bin_info_lookup_function_name - correct func_name value"); |
| 131 | free(func_name); |
| 132 | } else { |
| 133 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); |
| 134 | } |
| 135 | |
| 136 | /* Test source location lookup */ |
| 137 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_DBG_LINK, |
| 138 | &src_loc); |
| 139 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
| 140 | if (src_loc) { |
| 141 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, |
| 142 | "bin_info_lookup_source_location - correct line_no"); |
| 143 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, |
| 144 | "bin_info_lookup_source_location - correct filename"); |
| 145 | source_location_destroy(src_loc); |
| 146 | } else { |
| 147 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); |
| 148 | } |
| 149 | |
| 150 | bin_info_destroy(bin); |
| 151 | } |
| 152 | |
| 153 | static |
| 154 | void test_bin_info_elf(const char *data_dir) |
| 155 | { |
| 156 | int ret; |
| 157 | char path[PATH_MAX]; |
| 158 | char *func_name = NULL; |
| 159 | struct bin_info *bin = NULL; |
| 160 | struct source_location *src_loc = NULL; |
| 161 | |
| 162 | diag("bin-info tests - ELF only"); |
| 163 | |
| 164 | snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_ELF); |
| 165 | |
| 166 | bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); |
| 167 | ok(bin != NULL, "bin_info_create successful"); |
| 168 | |
| 169 | /* Test function name lookup (with ELF) */ |
| 170 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_ELF, &func_name); |
| 171 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
| 172 | if (func_name) { |
| 173 | ok(strcmp(func_name, FUNC_FOO_NAME_ELF) == 0, |
| 174 | "bin_info_lookup_function_name - correct func_name value"); |
| 175 | free(func_name); |
| 176 | func_name = NULL; |
| 177 | } else { |
| 178 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); |
| 179 | } |
| 180 | |
| 181 | /* Test function name lookup - erroneous address */ |
| 182 | ret = bin_info_lookup_function_name(bin, 0, &func_name); |
| 183 | ok(ret == -1 && func_name == NULL, |
| 184 | "bin_info_lookup_function_name - fail on addr not found"); |
| 185 | |
| 186 | /* Test source location location - should fail on ELF only file */ |
| 187 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_ELF, &src_loc); |
| 188 | ok(ret == -1, "bin_info_lookup_source_location - fail on ELF only file"); |
| 189 | |
| 190 | source_location_destroy(src_loc); |
| 191 | bin_info_destroy(bin); |
| 192 | } |
| 193 | |
| 194 | static |
| 195 | void test_bin_info(const char *data_dir) |
| 196 | { |
| 197 | int ret; |
| 198 | char path[PATH_MAX]; |
| 199 | char *func_name = NULL; |
| 200 | struct bin_info *bin = NULL; |
| 201 | struct source_location *src_loc = NULL; |
| 202 | |
| 203 | diag("bin-info tests - DWARF bundled with SO file"); |
| 204 | |
| 205 | snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME); |
| 206 | |
| 207 | bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); |
| 208 | ok(bin != NULL, "bin_info_create successful"); |
| 209 | |
| 210 | /* Test bin_info_has_address */ |
| 211 | ret = bin_info_has_address(bin, 0); |
| 212 | ok(ret == 0, "bin_info_has_address - address under so's range"); |
| 213 | ret = bin_info_has_address(bin, SO_LOW_ADDR); |
| 214 | ok(ret == 1, "bin_info_has_address - lower bound of so's range"); |
| 215 | ret = bin_info_has_address(bin, FUNC_FOO_ADDR); |
| 216 | ok(ret == 1, "bin_info_has_address - address in so's range"); |
| 217 | ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ - 1); |
| 218 | ok(ret == 1, "bin_info_has_address - upper bound of so's range"); |
| 219 | ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ); |
| 220 | ok(ret == 0, "bin_info_has_address - address above so's range"); |
| 221 | |
| 222 | /* Test function name lookup (with DWARF) */ |
| 223 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name); |
| 224 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
| 225 | if (func_name) { |
| 226 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, |
| 227 | "bin_info_lookup_function_name - correct func_name value"); |
| 228 | free(func_name); |
| 229 | func_name = NULL; |
| 230 | } else { |
| 231 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); |
| 232 | } |
| 233 | |
| 234 | /* Test function name lookup - erroneous address */ |
| 235 | ret = bin_info_lookup_function_name(bin, 0, &func_name); |
| 236 | ok(ret == -1 && func_name == NULL, |
| 237 | "bin_info_lookup_function_name - fail on addr not found"); |
| 238 | |
| 239 | /* Test source location lookup */ |
| 240 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); |
| 241 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
| 242 | if (src_loc) { |
| 243 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, |
| 244 | "bin_info_lookup_source_location - correct line_no"); |
| 245 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, |
| 246 | "bin_info_lookup_source_location - correct filename"); |
| 247 | source_location_destroy(src_loc); |
| 248 | src_loc = NULL; |
| 249 | } else { |
| 250 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); |
| 251 | } |
| 252 | |
| 253 | /* Test source location lookup - inlined function */ |
| 254 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_TP_ADDR, &src_loc); |
| 255 | ok(ret == 0, |
| 256 | "bin_info_lookup_source_location (inlined func) successful"); |
| 257 | if (src_loc) { |
| 258 | ok(src_loc->line_no == FUNC_FOO_TP_LINE_NO, |
| 259 | "bin_info_lookup_source_location (inlined func) - correct line_no"); |
| 260 | ok(strcmp(src_loc->filename, FUNC_FOO_TP_FILENAME) == 0, |
| 261 | "bin_info_lookup_source_location (inlined func) - correct filename"); |
| 262 | source_location_destroy(src_loc); |
| 263 | src_loc = NULL; |
| 264 | } else { |
| 265 | skip(2, "bin_info_lookup_source_location (inlined func) - src_loc is NULL"); |
| 266 | } |
| 267 | |
| 268 | /* Test source location lookup - erroneous address */ |
| 269 | ret = bin_info_lookup_source_location(bin, 0, &src_loc); |
| 270 | ok(ret == -1 && src_loc == NULL, |
| 271 | "bin_info_lookup_source_location - fail on addr not found"); |
| 272 | |
| 273 | bin_info_destroy(bin); |
| 274 | } |
| 275 | |
| 276 | int main(int argc, char **argv) |
| 277 | { |
| 278 | int ret; |
| 279 | |
| 280 | plan_tests(NR_TESTS); |
| 281 | |
| 282 | if (argc != 2) { |
| 283 | return EXIT_FAILURE; |
| 284 | } else { |
| 285 | opt_debug_info_dir = argv[1]; |
| 286 | } |
| 287 | |
| 288 | ret = bin_info_init(); |
| 289 | ok(ret == 0, "bin_info_init successful"); |
| 290 | |
| 291 | test_bin_info(opt_debug_info_dir); |
| 292 | test_bin_info_elf(opt_debug_info_dir); |
| 293 | test_bin_info_build_id(opt_debug_info_dir); |
| 294 | test_bin_info_debug_link(opt_debug_info_dir); |
| 295 | |
| 296 | return EXIT_SUCCESS; |
| 297 | } |