| 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/debuginfo.h> |
| 33 | #include <babeltrace/so-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 so info; owned by |
| 40 | * proc_debug_info_sources. |
| 41 | */ |
| 42 | GHashTable *baddr_to_so_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_soinfo; |
| 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_soinfo = g_quark_from_string( |
| 68 | "lttng_ust_statedump:soinfo"); |
| 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 so_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 | g_free(debug_info_src); |
| 91 | } |
| 92 | |
| 93 | static |
| 94 | struct debug_info_source *debug_info_source_create_from_so(struct so_info *so, |
| 95 | uint64_t ip) |
| 96 | { |
| 97 | int ret; |
| 98 | struct debug_info_source *debug_info_src = NULL; |
| 99 | struct source_location *src_loc = NULL; |
| 100 | |
| 101 | debug_info_src = g_new0(struct debug_info_source, 1); |
| 102 | |
| 103 | if (!debug_info_src) { |
| 104 | goto end; |
| 105 | } |
| 106 | |
| 107 | /* Lookup function name */ |
| 108 | ret = so_info_lookup_function_name(so, ip, &debug_info_src->func); |
| 109 | if (ret) { |
| 110 | goto error; |
| 111 | } |
| 112 | |
| 113 | /* Can't retrieve src_loc from ELF only, skip it */ |
| 114 | if (!so->is_elf_only) { |
| 115 | /* Lookup source location */ |
| 116 | ret = so_info_lookup_source_location(so, ip, &src_loc); |
| 117 | if (ret) { |
| 118 | goto error; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (src_loc) { |
| 123 | debug_info_src->line_no = src_loc->line_no; |
| 124 | |
| 125 | if (src_loc->filename) { |
| 126 | debug_info_src->src_path = strdup(src_loc->filename); |
| 127 | if (!debug_info_src->src_path) { |
| 128 | goto error; |
| 129 | } |
| 130 | |
| 131 | debug_info_src->short_src_path = get_filename_from_path( |
| 132 | debug_info_src->src_path); |
| 133 | } |
| 134 | |
| 135 | source_location_destroy(src_loc); |
| 136 | } |
| 137 | |
| 138 | if (so->elf_path) { |
| 139 | debug_info_src->bin_path = strdup(so->elf_path); |
| 140 | if (!debug_info_src->bin_path) { |
| 141 | goto error; |
| 142 | } |
| 143 | |
| 144 | debug_info_src->short_bin_path = get_filename_from_path( |
| 145 | debug_info_src->bin_path); |
| 146 | } |
| 147 | |
| 148 | end: |
| 149 | return debug_info_src; |
| 150 | |
| 151 | error: |
| 152 | debug_info_source_destroy(debug_info_src); |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | static |
| 157 | void proc_debug_info_sources_destroy( |
| 158 | struct proc_debug_info_sources *proc_dbg_info_src) |
| 159 | { |
| 160 | if (!proc_dbg_info_src) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | if (proc_dbg_info_src->baddr_to_so_info) { |
| 165 | g_hash_table_destroy(proc_dbg_info_src->baddr_to_so_info); |
| 166 | } |
| 167 | |
| 168 | if (proc_dbg_info_src->ip_to_debug_info_src) { |
| 169 | g_hash_table_destroy(proc_dbg_info_src->ip_to_debug_info_src); |
| 170 | } |
| 171 | |
| 172 | g_free(proc_dbg_info_src); |
| 173 | } |
| 174 | |
| 175 | static |
| 176 | struct proc_debug_info_sources *proc_debug_info_sources_create(void) |
| 177 | { |
| 178 | struct proc_debug_info_sources *proc_dbg_info_src = NULL; |
| 179 | |
| 180 | proc_dbg_info_src = g_new0(struct proc_debug_info_sources, 1); |
| 181 | if (!proc_dbg_info_src) { |
| 182 | goto end; |
| 183 | } |
| 184 | |
| 185 | proc_dbg_info_src->baddr_to_so_info = g_hash_table_new_full( |
| 186 | g_int64_hash, g_int64_equal, (GDestroyNotify) g_free, |
| 187 | (GDestroyNotify) so_info_destroy); |
| 188 | if (!proc_dbg_info_src->baddr_to_so_info) { |
| 189 | goto error; |
| 190 | } |
| 191 | |
| 192 | proc_dbg_info_src->ip_to_debug_info_src = g_hash_table_new_full( |
| 193 | g_int64_hash, g_int64_equal, (GDestroyNotify) g_free, |
| 194 | (GDestroyNotify) debug_info_source_destroy); |
| 195 | if (!proc_dbg_info_src->ip_to_debug_info_src) { |
| 196 | goto error; |
| 197 | } |
| 198 | |
| 199 | end: |
| 200 | return proc_dbg_info_src; |
| 201 | |
| 202 | error: |
| 203 | proc_debug_info_sources_destroy(proc_dbg_info_src); |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | static |
| 208 | struct proc_debug_info_sources *proc_debug_info_sources_ht_get_entry( |
| 209 | GHashTable *ht, int64_t vpid) |
| 210 | { |
| 211 | gpointer key = g_new0(int64_t, 1); |
| 212 | struct proc_debug_info_sources *proc_dbg_info_src = NULL; |
| 213 | |
| 214 | if (!key) { |
| 215 | goto end; |
| 216 | } |
| 217 | |
| 218 | *((int64_t *) key) = vpid; |
| 219 | |
| 220 | /* Exists? Return it */ |
| 221 | proc_dbg_info_src = g_hash_table_lookup(ht, key); |
| 222 | if (proc_dbg_info_src) { |
| 223 | goto end; |
| 224 | } |
| 225 | |
| 226 | /* Otherwise, create and return it */ |
| 227 | proc_dbg_info_src = proc_debug_info_sources_create(); |
| 228 | if (!proc_dbg_info_src) { |
| 229 | goto end; |
| 230 | } |
| 231 | |
| 232 | g_hash_table_insert(ht, key, proc_dbg_info_src); |
| 233 | /* Ownership passed to ht */ |
| 234 | key = NULL; |
| 235 | end: |
| 236 | g_free(key); |
| 237 | return proc_dbg_info_src; |
| 238 | } |
| 239 | |
| 240 | static |
| 241 | struct debug_info_source *proc_debug_info_sources_get_entry( |
| 242 | struct proc_debug_info_sources *proc_dbg_info_src, uint64_t ip) |
| 243 | { |
| 244 | struct debug_info_source *debug_info_src = NULL; |
| 245 | gpointer key = g_new0(uint64_t, 1); |
| 246 | GHashTableIter iter; |
| 247 | gpointer baddr, value; |
| 248 | |
| 249 | if (!key) { |
| 250 | goto end; |
| 251 | } |
| 252 | |
| 253 | *((uint64_t *) key) = ip; |
| 254 | |
| 255 | /* Look in IP to debug infos hash table first. */ |
| 256 | debug_info_src = g_hash_table_lookup( |
| 257 | proc_dbg_info_src->ip_to_debug_info_src, |
| 258 | key); |
| 259 | if (debug_info_src) { |
| 260 | goto end; |
| 261 | } |
| 262 | |
| 263 | /* Check in all so_infos. */ |
| 264 | g_hash_table_iter_init(&iter, proc_dbg_info_src->baddr_to_so_info); |
| 265 | |
| 266 | while (g_hash_table_iter_next(&iter, &baddr, &value)) |
| 267 | { |
| 268 | struct so_info *so = value; |
| 269 | |
| 270 | if (!so_info_has_address(value, ip)) { |
| 271 | continue; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Found; add it to cache. |
| 276 | * |
| 277 | * FIXME: this should be bounded in size (and implement |
| 278 | * a caching policy), and entries should be prunned when |
| 279 | * libraries are unmapped. |
| 280 | */ |
| 281 | debug_info_src = debug_info_source_create_from_so(so, ip); |
| 282 | if (debug_info_src) { |
| 283 | g_hash_table_insert( |
| 284 | proc_dbg_info_src->ip_to_debug_info_src, |
| 285 | key, debug_info_src); |
| 286 | /* Ownership passed to ht. */ |
| 287 | key = NULL; |
| 288 | } |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | end: |
| 293 | free(key); |
| 294 | return debug_info_src; |
| 295 | } |
| 296 | |
| 297 | BT_HIDDEN |
| 298 | struct debug_info_source *debug_info_query(struct debug_info *debug_info, |
| 299 | int64_t vpid, uint64_t ip) |
| 300 | { |
| 301 | struct debug_info_source *dbg_info_src = NULL; |
| 302 | struct proc_debug_info_sources *proc_dbg_info_src; |
| 303 | |
| 304 | proc_dbg_info_src = proc_debug_info_sources_ht_get_entry( |
| 305 | debug_info->vpid_to_proc_dbg_info_src, vpid); |
| 306 | if (!proc_dbg_info_src) { |
| 307 | goto end; |
| 308 | } |
| 309 | |
| 310 | dbg_info_src = proc_debug_info_sources_get_entry( |
| 311 | proc_dbg_info_src, ip); |
| 312 | if (!dbg_info_src) { |
| 313 | goto end; |
| 314 | } |
| 315 | |
| 316 | end: |
| 317 | return dbg_info_src; |
| 318 | } |
| 319 | |
| 320 | BT_HIDDEN |
| 321 | struct debug_info *debug_info_create(void) |
| 322 | { |
| 323 | int ret; |
| 324 | struct debug_info *debug_info; |
| 325 | |
| 326 | debug_info = g_new0(struct debug_info, 1); |
| 327 | if (!debug_info) { |
| 328 | goto end; |
| 329 | } |
| 330 | |
| 331 | debug_info->vpid_to_proc_dbg_info_src = g_hash_table_new_full( |
| 332 | g_int64_hash, g_int64_equal, (GDestroyNotify) g_free, |
| 333 | (GDestroyNotify) proc_debug_info_sources_destroy); |
| 334 | if (!debug_info->vpid_to_proc_dbg_info_src) { |
| 335 | goto error; |
| 336 | } |
| 337 | |
| 338 | ret = debug_info_init(debug_info); |
| 339 | if (ret) { |
| 340 | goto error; |
| 341 | } |
| 342 | |
| 343 | end: |
| 344 | return debug_info; |
| 345 | error: |
| 346 | g_free(debug_info); |
| 347 | return NULL; |
| 348 | } |
| 349 | |
| 350 | BT_HIDDEN |
| 351 | void debug_info_destroy(struct debug_info *debug_info) |
| 352 | { |
| 353 | if (!debug_info) { |
| 354 | goto end; |
| 355 | } |
| 356 | |
| 357 | if (debug_info->vpid_to_proc_dbg_info_src) { |
| 358 | g_hash_table_destroy(debug_info->vpid_to_proc_dbg_info_src); |
| 359 | } |
| 360 | |
| 361 | g_free(debug_info); |
| 362 | end: |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | static |
| 367 | void handle_statedump_build_id_event(struct debug_info *debug_info, |
| 368 | struct ctf_event_definition *event_def) |
| 369 | { |
| 370 | struct proc_debug_info_sources *proc_dbg_info_src; |
| 371 | struct bt_definition *event_fields_def = NULL; |
| 372 | struct bt_definition *sec_def = NULL; |
| 373 | struct bt_definition *baddr_def = NULL; |
| 374 | struct bt_definition *vpid_def = NULL; |
| 375 | struct bt_definition *build_id_def = NULL; |
| 376 | struct definition_sequence *build_id_seq; |
| 377 | struct so_info *so = NULL; |
| 378 | int i; |
| 379 | int64_t vpid; |
| 380 | uint64_t baddr; |
| 381 | uint8_t *build_id = NULL; |
| 382 | uint64_t build_id_len; |
| 383 | |
| 384 | event_fields_def = (struct bt_definition *) event_def->event_fields; |
| 385 | sec_def = (struct bt_definition *) |
| 386 | event_def->stream->stream_event_context; |
| 387 | |
| 388 | if (!event_fields_def || !sec_def) { |
| 389 | goto end; |
| 390 | } |
| 391 | |
| 392 | baddr_def = bt_lookup_definition(event_fields_def, "_baddr"); |
| 393 | if (!baddr_def) { |
| 394 | goto end; |
| 395 | } |
| 396 | |
| 397 | vpid_def = bt_lookup_definition(sec_def, "_vpid"); |
| 398 | if (!vpid_def) { |
| 399 | goto end; |
| 400 | } |
| 401 | |
| 402 | build_id_def = bt_lookup_definition(event_fields_def, "_build_id"); |
| 403 | if (!build_id_def) { |
| 404 | goto end; |
| 405 | } |
| 406 | |
| 407 | if (baddr_def->declaration->id != CTF_TYPE_INTEGER) { |
| 408 | goto end; |
| 409 | } |
| 410 | |
| 411 | if (vpid_def->declaration->id != CTF_TYPE_INTEGER) { |
| 412 | goto end; |
| 413 | } |
| 414 | |
| 415 | if (build_id_def->declaration->id != CTF_TYPE_SEQUENCE) { |
| 416 | goto end; |
| 417 | } |
| 418 | |
| 419 | baddr = bt_get_unsigned_int(baddr_def); |
| 420 | vpid = bt_get_signed_int(vpid_def); |
| 421 | build_id_seq = container_of(build_id_def, |
| 422 | struct definition_sequence, p); |
| 423 | build_id_len = build_id_seq->length->value._unsigned; |
| 424 | |
| 425 | build_id = g_malloc(build_id_len); |
| 426 | if (!build_id) { |
| 427 | goto end; |
| 428 | } |
| 429 | |
| 430 | for (i = 0; i < build_id_len; ++i) { |
| 431 | struct bt_definition **field; |
| 432 | |
| 433 | field = (struct bt_definition **) &g_ptr_array_index( |
| 434 | build_id_seq->elems, i); |
| 435 | build_id[i] = bt_get_unsigned_int(*field); |
| 436 | } |
| 437 | |
| 438 | proc_dbg_info_src = proc_debug_info_sources_ht_get_entry( |
| 439 | debug_info->vpid_to_proc_dbg_info_src, vpid); |
| 440 | if (!proc_dbg_info_src) { |
| 441 | goto end; |
| 442 | } |
| 443 | |
| 444 | so = g_hash_table_lookup(proc_dbg_info_src->baddr_to_so_info, |
| 445 | (gpointer) &baddr); |
| 446 | if (!so) { |
| 447 | /* |
| 448 | * The build_id event comes after the so has been |
| 449 | * created. If it isn't found, just ignore this event. |
| 450 | */ |
| 451 | goto end; |
| 452 | } |
| 453 | |
| 454 | so_info_set_build_id(so, build_id, build_id_len); |
| 455 | |
| 456 | end: |
| 457 | free(build_id); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | static |
| 462 | void handle_statedump_debug_link_event(struct debug_info *debug_info, |
| 463 | struct ctf_event_definition *event_def) |
| 464 | { |
| 465 | struct proc_debug_info_sources *proc_dbg_info_src; |
| 466 | struct bt_definition *event_fields_def = NULL; |
| 467 | struct bt_definition *sec_def = NULL; |
| 468 | struct bt_definition *baddr_def = NULL; |
| 469 | struct bt_definition *vpid_def = NULL; |
| 470 | struct bt_definition *filename_def = NULL; |
| 471 | struct bt_definition *crc32_def = NULL; |
| 472 | struct so_info *so = NULL; |
| 473 | int64_t vpid; |
| 474 | uint64_t baddr; |
| 475 | char *filename = NULL; |
| 476 | uint32_t crc32; |
| 477 | |
| 478 | event_fields_def = (struct bt_definition *) event_def->event_fields; |
| 479 | sec_def = (struct bt_definition *) |
| 480 | event_def->stream->stream_event_context; |
| 481 | |
| 482 | if (!event_fields_def || !sec_def) { |
| 483 | goto end; |
| 484 | } |
| 485 | |
| 486 | baddr_def = bt_lookup_definition(event_fields_def, "_baddr"); |
| 487 | if (!baddr_def) { |
| 488 | goto end; |
| 489 | } |
| 490 | |
| 491 | vpid_def = bt_lookup_definition(sec_def, "_vpid"); |
| 492 | if (!vpid_def) { |
| 493 | goto end; |
| 494 | } |
| 495 | |
| 496 | filename_def = bt_lookup_definition(event_fields_def, "_filename"); |
| 497 | if (!filename_def) { |
| 498 | goto end; |
| 499 | } |
| 500 | |
| 501 | crc32_def = bt_lookup_definition(event_fields_def, "_crc32"); |
| 502 | if (!crc32_def) { |
| 503 | goto end; |
| 504 | } |
| 505 | |
| 506 | if (baddr_def->declaration->id != CTF_TYPE_INTEGER) { |
| 507 | goto end; |
| 508 | } |
| 509 | |
| 510 | if (vpid_def->declaration->id != CTF_TYPE_INTEGER) { |
| 511 | goto end; |
| 512 | } |
| 513 | |
| 514 | if (filename_def->declaration->id != CTF_TYPE_STRING) { |
| 515 | goto end; |
| 516 | } |
| 517 | |
| 518 | if (crc32_def->declaration->id != CTF_TYPE_INTEGER) { |
| 519 | goto end; |
| 520 | } |
| 521 | |
| 522 | baddr = bt_get_unsigned_int(baddr_def); |
| 523 | vpid = bt_get_signed_int(vpid_def); |
| 524 | |
| 525 | proc_dbg_info_src = proc_debug_info_sources_ht_get_entry( |
| 526 | debug_info->vpid_to_proc_dbg_info_src, vpid); |
| 527 | if (!proc_dbg_info_src) { |
| 528 | goto end; |
| 529 | } |
| 530 | |
| 531 | so = g_hash_table_lookup(proc_dbg_info_src->baddr_to_so_info, |
| 532 | (gpointer) &baddr); |
| 533 | if (!so) { |
| 534 | /* |
| 535 | * The debug_link event comes after the so has been |
| 536 | * created. If it isn't found, just ignore this event. |
| 537 | */ |
| 538 | goto end; |
| 539 | } |
| 540 | |
| 541 | filename = bt_get_string(filename_def); |
| 542 | crc32 = bt_get_unsigned_int(crc32_def); |
| 543 | |
| 544 | so_info_set_debug_link(so, filename, crc32); |
| 545 | |
| 546 | end: |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | static |
| 551 | void handle_bin_info_event(struct debug_info *debug_info, |
| 552 | struct ctf_event_definition *event_def, bool has_pic_field) |
| 553 | { |
| 554 | struct bt_definition *baddr_def = NULL; |
| 555 | struct bt_definition *memsz_def = NULL; |
| 556 | struct bt_definition *sopath_def = NULL; |
| 557 | struct bt_definition *is_pic_def = NULL; |
| 558 | struct bt_definition *vpid_def = NULL; |
| 559 | struct bt_definition *event_fields_def = NULL; |
| 560 | struct bt_definition *sec_def = NULL; |
| 561 | struct proc_debug_info_sources *proc_dbg_info_src; |
| 562 | struct so_info *so; |
| 563 | uint64_t baddr, memsz; |
| 564 | int64_t vpid; |
| 565 | const char *sopath; |
| 566 | gpointer key = NULL; |
| 567 | bool is_pic; |
| 568 | |
| 569 | event_fields_def = (struct bt_definition *) event_def->event_fields; |
| 570 | sec_def = (struct bt_definition *) |
| 571 | event_def->stream->stream_event_context; |
| 572 | |
| 573 | if (!event_fields_def || !sec_def) { |
| 574 | goto end; |
| 575 | } |
| 576 | |
| 577 | baddr_def = bt_lookup_definition(event_fields_def, "_baddr"); |
| 578 | if (!baddr_def) { |
| 579 | goto end; |
| 580 | } |
| 581 | |
| 582 | memsz_def = bt_lookup_definition(event_fields_def, "_memsz"); |
| 583 | if (!memsz_def) { |
| 584 | goto end; |
| 585 | } |
| 586 | |
| 587 | sopath_def = bt_lookup_definition(event_fields_def, "_sopath"); |
| 588 | if (!sopath_def) { |
| 589 | goto end; |
| 590 | } |
| 591 | |
| 592 | if (has_pic_field) { |
| 593 | is_pic_def = bt_lookup_definition(event_fields_def, "_is_pic"); |
| 594 | if (!is_pic_def) { |
| 595 | goto end; |
| 596 | } |
| 597 | |
| 598 | if (is_pic_def->declaration->id != CTF_TYPE_INTEGER) { |
| 599 | goto end; |
| 600 | } |
| 601 | |
| 602 | is_pic = (bt_get_unsigned_int(is_pic_def) == 1); |
| 603 | } else { |
| 604 | /* |
| 605 | * dlopen has no is_pic field, because the shared |
| 606 | * object is always PIC. |
| 607 | */ |
| 608 | is_pic = true; |
| 609 | } |
| 610 | |
| 611 | vpid_def = bt_lookup_definition(sec_def, "_vpid"); |
| 612 | if (!vpid_def) { |
| 613 | goto end; |
| 614 | } |
| 615 | |
| 616 | if (baddr_def->declaration->id != CTF_TYPE_INTEGER) { |
| 617 | goto end; |
| 618 | } |
| 619 | |
| 620 | if (memsz_def->declaration->id != CTF_TYPE_INTEGER) { |
| 621 | goto end; |
| 622 | } |
| 623 | |
| 624 | if (sopath_def->declaration->id != CTF_TYPE_STRING) { |
| 625 | goto end; |
| 626 | } |
| 627 | |
| 628 | if (vpid_def->declaration->id != CTF_TYPE_INTEGER) { |
| 629 | goto end; |
| 630 | } |
| 631 | |
| 632 | baddr = bt_get_unsigned_int(baddr_def); |
| 633 | memsz = bt_get_unsigned_int(memsz_def); |
| 634 | sopath = bt_get_string(sopath_def); |
| 635 | vpid = bt_get_signed_int(vpid_def); |
| 636 | |
| 637 | if (!sopath) { |
| 638 | goto end; |
| 639 | } |
| 640 | |
| 641 | if (memsz == 0) { |
| 642 | /* Ignore VDSO. */ |
| 643 | goto end; |
| 644 | } |
| 645 | |
| 646 | proc_dbg_info_src = proc_debug_info_sources_ht_get_entry( |
| 647 | debug_info->vpid_to_proc_dbg_info_src, vpid); |
| 648 | if (!proc_dbg_info_src) { |
| 649 | goto end; |
| 650 | } |
| 651 | |
| 652 | key = g_new0(uint64_t, 1); |
| 653 | if (!key) { |
| 654 | goto end; |
| 655 | } |
| 656 | |
| 657 | *((uint64_t *) key) = baddr; |
| 658 | |
| 659 | so = g_hash_table_lookup(proc_dbg_info_src->baddr_to_so_info, |
| 660 | key); |
| 661 | if (so) { |
| 662 | goto end; |
| 663 | } |
| 664 | |
| 665 | so = so_info_create(sopath, baddr, memsz, is_pic); |
| 666 | if (!so) { |
| 667 | goto end; |
| 668 | } |
| 669 | |
| 670 | g_hash_table_insert(proc_dbg_info_src->baddr_to_so_info, |
| 671 | key, so); |
| 672 | /* Ownership passed to ht. */ |
| 673 | key = NULL; |
| 674 | |
| 675 | end: |
| 676 | g_free(key); |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | static inline |
| 681 | void handle_soinfo_event(struct debug_info *debug_info, |
| 682 | struct ctf_event_definition *event_def) |
| 683 | { |
| 684 | handle_bin_info_event(debug_info, event_def, true); |
| 685 | } |
| 686 | |
| 687 | static inline |
| 688 | void handle_dlopen_event(struct debug_info *debug_info, |
| 689 | struct ctf_event_definition *event_def) |
| 690 | { |
| 691 | handle_bin_info_event(debug_info, event_def, false); |
| 692 | } |
| 693 | |
| 694 | |
| 695 | static |
| 696 | void handle_statedump_start(struct debug_info *debug_info, |
| 697 | struct ctf_event_definition *event_def) |
| 698 | { |
| 699 | struct bt_definition *vpid_def = NULL; |
| 700 | struct bt_definition *sec_def = NULL; |
| 701 | struct proc_debug_info_sources *proc_dbg_info_src; |
| 702 | int64_t vpid; |
| 703 | |
| 704 | sec_def = (struct bt_definition *) |
| 705 | event_def->stream->stream_event_context; |
| 706 | if (!sec_def) { |
| 707 | goto end; |
| 708 | } |
| 709 | |
| 710 | vpid_def = bt_lookup_definition(sec_def, "_vpid"); |
| 711 | if (!vpid_def) { |
| 712 | goto end; |
| 713 | } |
| 714 | |
| 715 | vpid = bt_get_signed_int(vpid_def); |
| 716 | |
| 717 | proc_dbg_info_src = proc_debug_info_sources_ht_get_entry( |
| 718 | debug_info->vpid_to_proc_dbg_info_src, vpid); |
| 719 | if (!proc_dbg_info_src) { |
| 720 | goto end; |
| 721 | } |
| 722 | |
| 723 | g_hash_table_remove_all(proc_dbg_info_src->baddr_to_so_info); |
| 724 | g_hash_table_remove_all(proc_dbg_info_src->ip_to_debug_info_src); |
| 725 | |
| 726 | end: |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | static |
| 731 | void register_event_debug_infos(struct debug_info *debug_info, |
| 732 | struct ctf_event_definition *event) |
| 733 | { |
| 734 | struct bt_definition *ip_def, *vpid_def; |
| 735 | int64_t vpid; |
| 736 | uint64_t ip; |
| 737 | struct bt_definition *sec_def; |
| 738 | |
| 739 | /* Get stream event context definition. */ |
| 740 | sec_def = (struct bt_definition *) event->stream->stream_event_context; |
| 741 | if (!sec_def) { |
| 742 | goto end; |
| 743 | } |
| 744 | |
| 745 | /* Get "ip" and "vpid" definitions. */ |
| 746 | vpid_def = bt_lookup_definition((struct bt_definition *) sec_def, |
| 747 | "_vpid"); |
| 748 | ip_def = bt_lookup_definition((struct bt_definition *) sec_def, "_ip"); |
| 749 | |
| 750 | if (!vpid_def || !ip_def) { |
| 751 | goto end; |
| 752 | } |
| 753 | |
| 754 | vpid = bt_get_signed_int(vpid_def); |
| 755 | ip = bt_get_unsigned_int(ip_def); |
| 756 | |
| 757 | /* Get debug info for this context. */ |
| 758 | ((struct definition_integer *) ip_def)->debug_info_src = |
| 759 | debug_info_query(debug_info, vpid, ip); |
| 760 | |
| 761 | end: |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | BT_HIDDEN |
| 766 | void debug_info_handle_event(struct debug_info *debug_info, |
| 767 | struct ctf_event_definition *event) |
| 768 | { |
| 769 | struct ctf_event_declaration *event_class; |
| 770 | struct ctf_stream_declaration *stream_class; |
| 771 | |
| 772 | if (!debug_info || !event) { |
| 773 | goto end; |
| 774 | } |
| 775 | |
| 776 | stream_class = event->stream->stream_class; |
| 777 | event_class = g_ptr_array_index(stream_class->events_by_id, |
| 778 | event->stream->event_id); |
| 779 | |
| 780 | if (event_class->name == debug_info->q_statedump_soinfo) { |
| 781 | /* State dump */ |
| 782 | handle_soinfo_event(debug_info, event); |
| 783 | } else if (event_class->name == debug_info->q_dl_open) { |
| 784 | handle_dlopen_event(debug_info, event); |
| 785 | } else if (event_class->name == debug_info->q_statedump_start) { |
| 786 | /* Start state dump */ |
| 787 | handle_statedump_start(debug_info, event); |
| 788 | } else if (event_class->name == debug_info->q_statedump_debug_link) { |
| 789 | /* Debug link info */ |
| 790 | handle_statedump_debug_link_event(debug_info, event); |
| 791 | } else if (event_class->name == debug_info->q_statedump_build_id) { |
| 792 | /* Build ID info */ |
| 793 | handle_statedump_build_id_event(debug_info, event); |
| 794 | } else { |
| 795 | /* Other events: register debug infos */ |
| 796 | register_event_debug_infos(debug_info, event); |
| 797 | } |
| 798 | |
| 799 | end: |
| 800 | return; |
| 801 | } |