| 1 | /* |
| 2 | * Babeltrace - Debug Information State Tracker |
| 3 | * |
| 4 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation |
| 5 | * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com> |
| 6 | * Copyright (c) 2015 Antoine Busque <abusque@efficios.com> |
| 7 | * Copyright (c) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #include <assert.h> |
| 29 | #include <glib.h> |
| 30 | #include <babeltrace/types.h> |
| 31 | #include <babeltrace/ctf-ir/metadata.h> |
| 32 | #include <babeltrace/debug-info.h> |
| 33 | #include <babeltrace/bin-info.h> |
| 34 | #include <babeltrace/babeltrace-internal.h> |
| 35 | #include <babeltrace/utils.h> |
| 36 | |
| 37 | struct proc_debug_info_sources { |
| 38 | /* |
| 39 | * Hash table: base address (pointer to uint64_t) to bin info; owned by |
| 40 | * proc_debug_info_sources. |
| 41 | */ |
| 42 | GHashTable *baddr_to_bin_info; |
| 43 | |
| 44 | /* |
| 45 | * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *); |
| 46 | * owned by proc_debug_info_sources. |
| 47 | */ |
| 48 | GHashTable *ip_to_debug_info_src; |
| 49 | }; |
| 50 | |
| 51 | struct debug_info { |
| 52 | /* |
| 53 | * Hash table of VPIDs (pointer to int64_t) to |
| 54 | * (struct ctf_proc_debug_infos*); owned by debug_info. |
| 55 | */ |
| 56 | GHashTable *vpid_to_proc_dbg_info_src; |
| 57 | GQuark q_statedump_bin_info; |
| 58 | GQuark q_statedump_debug_link; |
| 59 | GQuark q_statedump_build_id; |
| 60 | GQuark q_statedump_start; |
| 61 | GQuark q_dl_open; |
| 62 | }; |
| 63 | |
| 64 | static |
| 65 | int debug_info_init(struct debug_info *info) |
| 66 | { |
| 67 | info->q_statedump_bin_info = g_quark_from_string( |
| 68 | "lttng_ust_statedump:bin_info"); |
| 69 | info->q_statedump_debug_link = g_quark_from_string( |
| 70 | "lttng_ust_statedump:debug_link)"); |
| 71 | info->q_statedump_build_id = g_quark_from_string( |
| 72 | "lttng_ust_statedump:build_id"); |
| 73 | info->q_statedump_start = g_quark_from_string( |
| 74 | "lttng_ust_statedump:start"); |
| 75 | info->q_dl_open = g_quark_from_string("lttng_ust_dl:dlopen"); |
| 76 | |
| 77 | return bin_info_init(); |
| 78 | } |
| 79 | |
| 80 | static |
| 81 | void debug_info_source_destroy(struct debug_info_source *debug_info_src) |
| 82 | { |
| 83 | if (!debug_info_src) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | free(debug_info_src->func); |
| 88 | free(debug_info_src->src_path); |
| 89 | free(debug_info_src->bin_path); |
| 90 | free(debug_info_src->bin_loc); |
| 91 | g_free(debug_info_src); |
| 92 | } |
| 93 | |
| 94 | static |
| 95 | struct debug_info_source *debug_info_source_create_from_bin(struct bin_info *bin, |
| 96 | uint64_t ip) |
| 97 | { |
| 98 | int ret; |
| 99 | struct debug_info_source *debug_info_src = NULL; |
| 100 | struct source_location *src_loc = NULL; |
| 101 | |
| 102 | debug_info_src = g_new0(struct debug_info_source, 1); |
| 103 | |
| 104 | if (!debug_info_src) { |
| 105 | goto end; |
| 106 | } |
| 107 | |
| 108 | /* Lookup function name */ |
| 109 | ret = bin_info_lookup_function_name(bin, ip, &debug_info_src->func); |
| 110 | if (ret) { |
| 111 | goto error; |
| 112 | } |
| 113 | |
| 114 | /* Can't retrieve src_loc from ELF, or could not find binary, skip. */ |
| 115 | if (!bin->is_elf_only || !debug_info_src->func) { |
| 116 | /* Lookup source location */ |
| 117 | ret = bin_info_lookup_source_location(bin, ip, &src_loc); |
| 118 | printf_verbose("Failed to lookup source location (err: %i)\n", ret); |
| 119 | } |
| 120 | |
| 121 | if (src_loc) { |
| 122 | debug_info_src->line_no = src_loc->line_no; |
| 123 | |
| 124 | if (src_loc->filename) { |
| 125 | debug_info_src->src_path = strdup(src_loc->filename); |
| 126 | if (!debug_info_src->src_path) { |
| 127 | goto error; |
| 128 | } |
| 129 | |
| 130 | debug_info_src->short_src_path = get_filename_from_path( |
| 131 | debug_info_src->src_path); |
| 132 | } |
| 133 | |
| 134 | source_location_destroy(src_loc); |
| 135 | } |
| 136 | |
| 137 | if (bin->elf_path) { |
| 138 | debug_info_src->bin_path = strdup(bin->elf_path); |
| 139 | if (!debug_info_src->bin_path) { |
| 140 | goto error; |
| 141 | } |
| 142 | |
| 143 | debug_info_src->short_bin_path = get_filename_from_path( |
| 144 | debug_info_src->bin_path); |
| 145 | |
| 146 | ret = bin_info_get_bin_loc(bin, ip, &(debug_info_src->bin_loc)); |
| 147 | if (ret) { |
| 148 | goto error; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | end: |
| 153 | return debug_info_src; |
| 154 | |
| 155 | error: |
| 156 | debug_info_source_destroy(debug_info_src); |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | static |
| 161 | void proc_debug_info_sources_destroy( |
| 162 | struct proc_debug_info_sources *proc_dbg_info_src) |
| 163 | { |
| 164 | if (!proc_dbg_info_src) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | if (proc_dbg_info_src->baddr_to_bin_info) { |
| 169 | g_hash_table_destroy(proc_dbg_info_src->baddr_to_bin_info); |
| 170 | } |
| 171 | |
| 172 | if (proc_dbg_info_src->ip_to_debug_info_src) { |
| 173 | g_hash_table_destroy(proc_dbg_info_src->ip_to_debug_info_src); |
| 174 | } |
| 175 | |
| 176 | g_free(proc_dbg_info_src); |
| 177 | } |
| 178 | |
| 179 | static |
| 180 | struct proc_debug_info_sources *proc_debug_info_sources_create(void) |
| 181 | { |
| 182 | struct proc_debug_info_sources *proc_dbg_info_src = NULL; |
| 183 | |
| 184 | proc_dbg_info_src = g_new0(struct proc_debug_info_sources, 1); |
| 185 | if (!proc_dbg_info_src) { |
| 186 | goto end; |
| 187 | } |
| 188 | |
| 189 | proc_dbg_info_src->baddr_to_bin_info = g_hash_table_new_full( |
| 190 | g_int64_hash, g_int64_equal, (GDestroyNotify) g_free, |
| 191 | (GDestroyNotify) bin_info_destroy); |
| 192 | if (!proc_dbg_info_src->baddr_to_bin_info) { |
| 193 | goto error; |
| 194 | } |
| 195 | |
| 196 | proc_dbg_info_src->ip_to_debug_info_src = g_hash_table_new_full( |
| 197 | g_int64_hash, g_int64_equal, (GDestroyNotify) g_free, |
| 198 | (GDestroyNotify) debug_info_source_destroy); |
| 199 | if (!proc_dbg_info_src->ip_to_debug_info_src) { |
| 200 | goto error; |
| 201 | } |
| 202 | |
| 203 | end: |
| 204 | return proc_dbg_info_src; |
| 205 | |
| 206 | error: |
| 207 | proc_debug_info_sources_destroy(proc_dbg_info_src); |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | static |
| 212 | struct proc_debug_info_sources *proc_debug_info_sources_ht_get_entry( |
| 213 | GHashTable *ht, int64_t vpid) |
| 214 | { |
| 215 | gpointer key = g_new0(int64_t, 1); |
| 216 | struct proc_debug_info_sources *proc_dbg_info_src = NULL; |
| 217 | |
| 218 | if (!key) { |
| 219 | goto end; |
| 220 | } |
| 221 | |
| 222 | *((int64_t *) key) = vpid; |
| 223 | |
| 224 | /* Exists? Return it */ |
| 225 | proc_dbg_info_src = g_hash_table_lookup(ht, key); |
| 226 | if (proc_dbg_info_src) { |
| 227 | goto end; |
| 228 | } |
| 229 | |
| 230 | /* Otherwise, create and return it */ |
| 231 | proc_dbg_info_src = proc_debug_info_sources_create(); |
| 232 | if (!proc_dbg_info_src) { |
| 233 | goto end; |
| 234 | } |
| 235 | |
| 236 | g_hash_table_insert(ht, key, proc_dbg_info_src); |
| 237 | /* Ownership passed to ht */ |
| 238 | key = NULL; |
| 239 | end: |
| 240 | g_free(key); |
| 241 | return proc_dbg_info_src; |
| 242 | } |
| 243 | |
| 244 | static |
| 245 | struct debug_info_source *proc_debug_info_sources_get_entry( |
| 246 | struct proc_debug_info_sources *proc_dbg_info_src, uint64_t ip) |
| 247 | { |
| 248 | struct debug_info_source *debug_info_src = NULL; |
| 249 | gpointer key = g_new0(uint64_t, 1); |
| 250 | GHashTableIter iter; |
| 251 | gpointer baddr, value; |
| 252 | |
| 253 | if (!key) { |
| 254 | goto end; |
| 255 | } |
| 256 | |
| 257 | *((uint64_t *) key) = ip; |
| 258 | |
| 259 | /* Look in IP to debug infos hash table first. */ |
| 260 | debug_info_src = g_hash_table_lookup( |
| 261 | proc_dbg_info_src->ip_to_debug_info_src, |
| 262 | key); |
| 263 | if (debug_info_src) { |
| 264 | goto end; |
| 265 | } |
| 266 | |
| 267 | /* Check in all bin_infos. */ |
| 268 | g_hash_table_iter_init(&iter, proc_dbg_info_src->baddr_to_bin_info); |
| 269 | |
| 270 | while (g_hash_table_iter_next(&iter, &baddr, &value)) |
| 271 | { |
| 272 | struct bin_info *bin = value; |
| 273 | |
| 274 | if (!bin_info_has_address(value, ip)) { |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Found; add it to cache. |
| 280 | * |
| 281 | * FIXME: this should be bounded in size (and implement |
| 282 | * a caching policy), and entries should be prunned when |
| 283 | * libraries are unmapped. |
| 284 | */ |
| 285 | debug_info_src = debug_info_source_create_from_bin(bin, ip); |
| 286 | if (debug_info_src) { |
| 287 | g_hash_table_insert( |
| 288 | proc_dbg_info_src->ip_to_debug_info_src, |
| 289 | key, debug_info_src); |
| 290 | /* Ownership passed to ht. */ |
| 291 | key = NULL; |
| 292 | } |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | end: |
| 297 | free(key); |
| 298 | return debug_info_src; |
| 299 | } |
| 300 | |
| 301 | BT_HIDDEN |
| 302 | struct debug_info_source *debug_info_query(struct debug_info *debug_info, |
| 303 | int64_t vpid, uint64_t ip) |
| 304 | { |
| 305 | struct debug_info_source *dbg_info_src = NULL; |
| 306 | struct proc_debug_info_sources *proc_dbg_info_src; |
| 307 | |
| 308 | proc_dbg_info_src = proc_debug_info_sources_ht_get_entry( |
| 309 | debug_info->vpid_to_proc_dbg_info_src, vpid); |
| 310 | if (!proc_dbg_info_src) { |
| 311 | goto end; |
| 312 | } |
| 313 | |
| 314 | dbg_info_src = proc_debug_info_sources_get_entry( |
| 315 | proc_dbg_info_src, ip); |
| 316 | if (!dbg_info_src) { |
| 317 | goto end; |
| 318 | } |
| 319 | |
| 320 | end: |
| 321 | return dbg_info_src; |
| 322 | } |
| 323 | |
| 324 | BT_HIDDEN |
| 325 | struct debug_info *debug_info_create(void) |
| 326 | { |
| 327 | int ret; |
| 328 | struct debug_info *debug_info; |
| 329 | |
| 330 | debug_info = g_new0(struct debug_info, 1); |
| 331 | if (!debug_info) { |
| 332 | goto end; |
| 333 | } |
| 334 | |
| 335 | debug_info->vpid_to_proc_dbg_info_src = g_hash_table_new_full( |
| 336 | g_int64_hash, g_int64_equal, (GDestroyNotify) g_free, |
| 337 | (GDestroyNotify) proc_debug_info_sources_destroy); |
| 338 | if (!debug_info->vpid_to_proc_dbg_info_src) { |
| 339 | goto error; |
| 340 | } |
| 341 | |
| 342 | ret = debug_info_init(debug_info); |
| 343 | if (ret) { |
| 344 | goto error; |
| 345 | } |
| 346 | |
| 347 | end: |
| 348 | return debug_info; |
| 349 | error: |
| 350 | g_free(debug_info); |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | BT_HIDDEN |
| 355 | void debug_info_destroy(struct debug_info *debug_info) |
| 356 | { |
| 357 | if (!debug_info) { |
| 358 | goto end; |
| 359 | } |
| 360 | |
| 361 | if (debug_info->vpid_to_proc_dbg_info_src) { |
| 362 | g_hash_table_destroy(debug_info->vpid_to_proc_dbg_info_src); |
| 363 | } |
| 364 | |
| 365 | g_free(debug_info); |
| 366 | end: |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | static |
| 371 | void handle_statedump_build_id_event(struct debug_info *debug_info, |
| 372 | struct ctf_event_definition *event_def) |
| 373 | { |
| 374 | struct proc_debug_info_sources *proc_dbg_info_src; |
| 375 | struct bt_definition *event_fields_def = NULL; |
| 376 | struct bt_definition *sec_def = NULL; |
| 377 | struct bt_definition *baddr_def = NULL; |
| 378 | struct bt_definition *vpid_def = NULL; |
| 379 | struct bt_definition *build_id_def = NULL; |
| 380 | struct definition_sequence *build_id_seq; |
| 381 | struct bin_info *bin = NULL; |
| 382 | int i; |
| 383 | int64_t vpid; |
| 384 | uint64_t baddr; |
| 385 | uint8_t *build_id = NULL; |
| 386 | uint64_t build_id_len; |
| 387 | |
| 388 | event_fields_def = (struct bt_definition *) event_def->event_fields; |
| 389 | sec_def = (struct bt_definition *) |
| 390 | event_def->stream->stream_event_context; |
| 391 | |
| 392 | if (!event_fields_def || !sec_def) { |
| 393 | goto end; |
| 394 | } |
| 395 | |
| 396 | baddr_def = bt_lookup_definition(event_fields_def, "_baddr"); |
| 397 | if (!baddr_def) { |
| 398 | goto end; |
| 399 | } |
| 400 | |
| 401 | vpid_def = bt_lookup_definition(sec_def, "_vpid"); |
| 402 | if (!vpid_def) { |
| 403 | goto end; |
| 404 | } |
| 405 | |
| 406 | build_id_def = bt_lookup_definition(event_fields_def, "_build_id"); |
| 407 | if (!build_id_def) { |
| 408 | goto end; |
| 409 | } |
| 410 | |
| 411 | if (baddr_def->declaration->id != CTF_TYPE_INTEGER) { |
| 412 | goto end; |
| 413 | } |
| 414 | |
| 415 | if (vpid_def->declaration->id != CTF_TYPE_INTEGER) { |
| 416 | goto end; |
| 417 | } |
| 418 | |
| 419 | if (build_id_def->declaration->id != CTF_TYPE_SEQUENCE) { |
| 420 | goto end; |
| 421 | } |
| 422 | |
| 423 | baddr = bt_get_unsigned_int(baddr_def); |
| 424 | vpid = bt_get_signed_int(vpid_def); |
| 425 | build_id_seq = container_of(build_id_def, |
| 426 | struct definition_sequence, p); |
| 427 | build_id_len = build_id_seq->length->value._unsigned; |
| 428 | |
| 429 | build_id = g_malloc(build_id_len); |
| 430 | if (!build_id) { |
| 431 | goto end; |
| 432 | } |
| 433 | |
| 434 | for (i = 0; i < build_id_len; ++i) { |
| 435 | struct bt_definition **field; |
| 436 | |
| 437 | field = (struct bt_definition **) &g_ptr_array_index( |
| 438 | build_id_seq->elems, i); |
| 439 | build_id[i] = bt_get_unsigned_int(*field); |
| 440 | } |
| 441 | |
| 442 | proc_dbg_info_src = proc_debug_info_sources_ht_get_entry( |
| 443 | debug_info->vpid_to_proc_dbg_info_src, vpid); |
| 444 | if (!proc_dbg_info_src) { |
| 445 | goto end; |
| 446 | } |
| 447 | |
| 448 | bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info, |
| 449 | (gpointer) &baddr); |
| 450 | if (!bin) { |
| 451 | /* |
| 452 | * The build_id event comes after the bin has been |
| 453 | * created. If it isn't found, just ignore this event. |
| 454 | */ |
| 455 | goto end; |
| 456 | } |
| 457 | |
| 458 | bin_info_set_build_id(bin, build_id, build_id_len); |
| 459 | |
| 460 | end: |
| 461 | free(build_id); |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | static |
| 466 | void handle_statedump_debug_link_event(struct debug_info *debug_info, |
| 467 | struct ctf_event_definition *event_def) |
| 468 | { |
| 469 | struct proc_debug_info_sources *proc_dbg_info_src; |
| 470 | struct bt_definition *event_fields_def = NULL; |
| 471 | struct bt_definition *sec_def = NULL; |
| 472 | struct bt_definition *baddr_def = NULL; |
| 473 | struct bt_definition *vpid_def = NULL; |
| 474 | struct bt_definition *filename_def = NULL; |
| 475 | struct bt_definition *crc32_def = NULL; |
| 476 | struct bin_info *bin = NULL; |
| 477 | int64_t vpid; |
| 478 | uint64_t baddr; |
| 479 | char *filename = NULL; |
| 480 | uint32_t crc32; |
| 481 | |
| 482 | event_fields_def = (struct bt_definition *) event_def->event_fields; |
| 483 | sec_def = (struct bt_definition *) |
| 484 | event_def->stream->stream_event_context; |
| 485 | |
| 486 | if (!event_fields_def || !sec_def) { |
| 487 | goto end; |
| 488 | } |
| 489 | |
| 490 | baddr_def = bt_lookup_definition(event_fields_def, "_baddr"); |
| 491 | if (!baddr_def) { |
| 492 | goto end; |
| 493 | } |
| 494 | |
| 495 | vpid_def = bt_lookup_definition(sec_def, "_vpid"); |
| 496 | if (!vpid_def) { |
| 497 | goto end; |
| 498 | } |
| 499 | |
| 500 | filename_def = bt_lookup_definition(event_fields_def, "_filename"); |
| 501 | if (!filename_def) { |
| 502 | goto end; |
| 503 | } |
| 504 | |
| 505 | crc32_def = bt_lookup_definition(event_fields_def, "_crc32"); |
| 506 | if (!crc32_def) { |
| 507 | goto end; |
| 508 | } |
| 509 | |
| 510 | if (baddr_def->declaration->id != CTF_TYPE_INTEGER) { |
| 511 | goto end; |
| 512 | } |
| 513 | |
| 514 | if (vpid_def->declaration->id != CTF_TYPE_INTEGER) { |
| 515 | goto end; |
| 516 | } |
| 517 | |
| 518 | if (filename_def->declaration->id != CTF_TYPE_STRING) { |
| 519 | goto end; |
| 520 | } |
| 521 | |
| 522 | if (crc32_def->declaration->id != CTF_TYPE_INTEGER) { |
| 523 | goto end; |
| 524 | } |
| 525 | |
| 526 | baddr = bt_get_unsigned_int(baddr_def); |
| 527 | vpid = bt_get_signed_int(vpid_def); |
| 528 | |
| 529 | proc_dbg_info_src = proc_debug_info_sources_ht_get_entry( |
| 530 | debug_info->vpid_to_proc_dbg_info_src, vpid); |
| 531 | if (!proc_dbg_info_src) { |
| 532 | goto end; |
| 533 | } |
| 534 | |
| 535 | bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info, |
| 536 | (gpointer) &baddr); |
| 537 | if (!bin) { |
| 538 | /* |
| 539 | * The debug_link event comes after the bin has been |
| 540 | * created. If it isn't found, just ignore this event. |
| 541 | */ |
| 542 | goto end; |
| 543 | } |
| 544 | |
| 545 | filename = bt_get_string(filename_def); |
| 546 | crc32 = bt_get_unsigned_int(crc32_def); |
| 547 | |
| 548 | bin_info_set_debug_link(bin, filename, crc32); |
| 549 | |
| 550 | end: |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | static |
| 555 | void handle_bin_info_event(struct debug_info *debug_info, |
| 556 | struct ctf_event_definition *event_def, bool has_pic_field) |
| 557 | { |
| 558 | struct bt_definition *baddr_def = NULL; |
| 559 | struct bt_definition *memsz_def = NULL; |
| 560 | struct bt_definition *path_def = NULL; |
| 561 | struct bt_definition *is_pic_def = NULL; |
| 562 | struct bt_definition *vpid_def = NULL; |
| 563 | struct bt_definition *event_fields_def = NULL; |
| 564 | struct bt_definition *sec_def = NULL; |
| 565 | struct proc_debug_info_sources *proc_dbg_info_src; |
| 566 | struct bin_info *bin; |
| 567 | uint64_t baddr, memsz; |
| 568 | int64_t vpid; |
| 569 | const char *path; |
| 570 | gpointer key = NULL; |
| 571 | bool is_pic; |
| 572 | |
| 573 | event_fields_def = (struct bt_definition *) event_def->event_fields; |
| 574 | sec_def = (struct bt_definition *) |
| 575 | event_def->stream->stream_event_context; |
| 576 | |
| 577 | if (!event_fields_def || !sec_def) { |
| 578 | goto end; |
| 579 | } |
| 580 | |
| 581 | baddr_def = bt_lookup_definition(event_fields_def, "_baddr"); |
| 582 | if (!baddr_def) { |
| 583 | goto end; |
| 584 | } |
| 585 | |
| 586 | memsz_def = bt_lookup_definition(event_fields_def, "_memsz"); |
| 587 | if (!memsz_def) { |
| 588 | goto end; |
| 589 | } |
| 590 | |
| 591 | path_def = bt_lookup_definition(event_fields_def, "_path"); |
| 592 | if (!path_def) { |
| 593 | goto end; |
| 594 | } |
| 595 | |
| 596 | if (has_pic_field) { |
| 597 | is_pic_def = bt_lookup_definition(event_fields_def, "_is_pic"); |
| 598 | if (!is_pic_def) { |
| 599 | goto end; |
| 600 | } |
| 601 | |
| 602 | if (is_pic_def->declaration->id != CTF_TYPE_INTEGER) { |
| 603 | goto end; |
| 604 | } |
| 605 | |
| 606 | is_pic = (bt_get_unsigned_int(is_pic_def) == 1); |
| 607 | } else { |
| 608 | /* |
| 609 | * dlopen has no is_pic field, because the shared |
| 610 | * object is always PIC. |
| 611 | */ |
| 612 | is_pic = true; |
| 613 | } |
| 614 | |
| 615 | vpid_def = bt_lookup_definition(sec_def, "_vpid"); |
| 616 | if (!vpid_def) { |
| 617 | goto end; |
| 618 | } |
| 619 | |
| 620 | if (baddr_def->declaration->id != CTF_TYPE_INTEGER) { |
| 621 | goto end; |
| 622 | } |
| 623 | |
| 624 | if (memsz_def->declaration->id != CTF_TYPE_INTEGER) { |
| 625 | goto end; |
| 626 | } |
| 627 | |
| 628 | if (path_def->declaration->id != CTF_TYPE_STRING) { |
| 629 | goto end; |
| 630 | } |
| 631 | |
| 632 | if (vpid_def->declaration->id != CTF_TYPE_INTEGER) { |
| 633 | goto end; |
| 634 | } |
| 635 | |
| 636 | baddr = bt_get_unsigned_int(baddr_def); |
| 637 | memsz = bt_get_unsigned_int(memsz_def); |
| 638 | path = bt_get_string(path_def); |
| 639 | vpid = bt_get_signed_int(vpid_def); |
| 640 | |
| 641 | if (!path) { |
| 642 | goto end; |
| 643 | } |
| 644 | |
| 645 | if (memsz == 0) { |
| 646 | /* Ignore VDSO. */ |
| 647 | goto end; |
| 648 | } |
| 649 | |
| 650 | proc_dbg_info_src = proc_debug_info_sources_ht_get_entry( |
| 651 | debug_info->vpid_to_proc_dbg_info_src, vpid); |
| 652 | if (!proc_dbg_info_src) { |
| 653 | goto end; |
| 654 | } |
| 655 | |
| 656 | key = g_new0(uint64_t, 1); |
| 657 | if (!key) { |
| 658 | goto end; |
| 659 | } |
| 660 | |
| 661 | *((uint64_t *) key) = baddr; |
| 662 | |
| 663 | bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info, |
| 664 | key); |
| 665 | if (bin) { |
| 666 | goto end; |
| 667 | } |
| 668 | |
| 669 | bin = bin_info_create(path, baddr, memsz, is_pic); |
| 670 | if (!bin) { |
| 671 | goto end; |
| 672 | } |
| 673 | |
| 674 | g_hash_table_insert(proc_dbg_info_src->baddr_to_bin_info, |
| 675 | key, bin); |
| 676 | /* Ownership passed to ht. */ |
| 677 | key = NULL; |
| 678 | |
| 679 | end: |
| 680 | g_free(key); |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | static inline |
| 685 | void handle_statedump_bin_info_event(struct debug_info *debug_info, |
| 686 | struct ctf_event_definition *event_def) |
| 687 | { |
| 688 | handle_bin_info_event(debug_info, event_def, true); |
| 689 | } |
| 690 | |
| 691 | static inline |
| 692 | void handle_dlopen_event(struct debug_info *debug_info, |
| 693 | struct ctf_event_definition *event_def) |
| 694 | { |
| 695 | handle_bin_info_event(debug_info, event_def, false); |
| 696 | } |
| 697 | |
| 698 | |
| 699 | static |
| 700 | void handle_statedump_start(struct debug_info *debug_info, |
| 701 | struct ctf_event_definition *event_def) |
| 702 | { |
| 703 | struct bt_definition *vpid_def = NULL; |
| 704 | struct bt_definition *sec_def = NULL; |
| 705 | struct proc_debug_info_sources *proc_dbg_info_src; |
| 706 | int64_t vpid; |
| 707 | |
| 708 | sec_def = (struct bt_definition *) |
| 709 | event_def->stream->stream_event_context; |
| 710 | if (!sec_def) { |
| 711 | goto end; |
| 712 | } |
| 713 | |
| 714 | vpid_def = bt_lookup_definition(sec_def, "_vpid"); |
| 715 | if (!vpid_def) { |
| 716 | goto end; |
| 717 | } |
| 718 | |
| 719 | vpid = bt_get_signed_int(vpid_def); |
| 720 | |
| 721 | proc_dbg_info_src = proc_debug_info_sources_ht_get_entry( |
| 722 | debug_info->vpid_to_proc_dbg_info_src, vpid); |
| 723 | if (!proc_dbg_info_src) { |
| 724 | goto end; |
| 725 | } |
| 726 | |
| 727 | g_hash_table_remove_all(proc_dbg_info_src->baddr_to_bin_info); |
| 728 | g_hash_table_remove_all(proc_dbg_info_src->ip_to_debug_info_src); |
| 729 | |
| 730 | end: |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | static |
| 735 | void register_event_debug_infos(struct debug_info *debug_info, |
| 736 | struct ctf_event_definition *event) |
| 737 | { |
| 738 | struct bt_definition *ip_def, *vpid_def; |
| 739 | int64_t vpid; |
| 740 | uint64_t ip; |
| 741 | struct bt_definition *sec_def; |
| 742 | |
| 743 | /* Get stream event context definition. */ |
| 744 | sec_def = (struct bt_definition *) event->stream->stream_event_context; |
| 745 | if (!sec_def) { |
| 746 | goto end; |
| 747 | } |
| 748 | |
| 749 | /* Get "ip" and "vpid" definitions. */ |
| 750 | vpid_def = bt_lookup_definition((struct bt_definition *) sec_def, |
| 751 | "_vpid"); |
| 752 | ip_def = bt_lookup_definition((struct bt_definition *) sec_def, "_ip"); |
| 753 | |
| 754 | if (!vpid_def || !ip_def) { |
| 755 | goto end; |
| 756 | } |
| 757 | |
| 758 | vpid = bt_get_signed_int(vpid_def); |
| 759 | ip = bt_get_unsigned_int(ip_def); |
| 760 | |
| 761 | /* Get debug info for this context. */ |
| 762 | ((struct definition_integer *) ip_def)->debug_info_src = |
| 763 | debug_info_query(debug_info, vpid, ip); |
| 764 | |
| 765 | end: |
| 766 | return; |
| 767 | } |
| 768 | |
| 769 | BT_HIDDEN |
| 770 | void debug_info_handle_event(struct debug_info *debug_info, |
| 771 | struct ctf_event_definition *event) |
| 772 | { |
| 773 | struct ctf_event_declaration *event_class; |
| 774 | struct ctf_stream_declaration *stream_class; |
| 775 | |
| 776 | if (!debug_info || !event) { |
| 777 | goto end; |
| 778 | } |
| 779 | |
| 780 | stream_class = event->stream->stream_class; |
| 781 | event_class = g_ptr_array_index(stream_class->events_by_id, |
| 782 | event->stream->event_id); |
| 783 | |
| 784 | if (event_class->name == debug_info->q_statedump_bin_info) { |
| 785 | /* State dump */ |
| 786 | handle_statedump_bin_info_event(debug_info, event); |
| 787 | } else if (event_class->name == debug_info->q_dl_open) { |
| 788 | handle_dlopen_event(debug_info, event); |
| 789 | } else if (event_class->name == debug_info->q_statedump_start) { |
| 790 | /* Start state dump */ |
| 791 | handle_statedump_start(debug_info, event); |
| 792 | } else if (event_class->name == debug_info->q_statedump_debug_link) { |
| 793 | /* Debug link info */ |
| 794 | handle_statedump_debug_link_event(debug_info, event); |
| 795 | } else if (event_class->name == debug_info->q_statedump_build_id) { |
| 796 | /* Build ID info */ |
| 797 | handle_statedump_build_id_event(debug_info, event); |
| 798 | } |
| 799 | |
| 800 | /* All events: register debug infos */ |
| 801 | register_event_debug_infos(debug_info, event); |
| 802 | |
| 803 | end: |
| 804 | return; |
| 805 | } |