| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include <assert.h> |
| 5 |
#include <byteswap.h> |
| 6 |
#include <ctype.h> |
| 7 |
#include <dirent.h> |
| 8 |
#include <elf.h> |
| 9 |
#include <elfutils/libdw.h> |
| 10 |
#include <elfutils/libdwelf.h> |
| 11 |
#include <elfutils/version.h> |
| 12 |
#include <errno.h> |
| 13 |
#include <fcntl.h> |
| 14 |
#include <gelf.h> |
| 15 |
#include <signal.h> |
| 16 |
#include <stdarg.h> |
| 17 |
#include <stdio.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <sys/stat.h> |
| 21 |
#include <sys/sysmacros.h> |
| 22 |
#include <sys/types.h> |
| 23 |
#include <termios.h> |
| 24 |
#include <unistd.h> |
| 25 |
#include <wchar.h> |
| 26 |
|
| 27 |
#include "array.h" |
| 28 |
#include "binary_buffer.h" |
| 29 |
#include "binary_search.h" |
| 30 |
#include "bitmap.h" |
| 31 |
#include "cleanup.h" |
| 32 |
#include "crc32.h" |
| 33 |
#include "debug_info.h" |
| 34 |
#include "elf_file.h" |
| 35 |
#include "elf_notes.h" |
| 36 |
#include "error.h" |
| 37 |
#include "hexlify.h" |
| 38 |
#include "io.h" |
| 39 |
#include "linux_kernel.h" |
| 40 |
#include "log.h" |
| 41 |
#include "openmp.h" |
| 42 |
#include "platform.h" |
| 43 |
#include "pp.h" |
| 44 |
#include "program.h" |
| 45 |
#include "serialize.h" |
| 46 |
#include "util.h" |
| 47 |
|
| 48 |
#define _cleanup_elf_end_ _cleanup_(elf_endp) |
| 49 |
static inline void elf_endp(Elf **elfp) |
| 50 |
{ |
| 51 |
elf_end(*elfp); |
| 52 |
} |
| 53 |
|
| 54 |
#if !_ELFUTILS_PREREQ(0, 175) |
| 55 |
// If we don't have dwelf_elf_begin(), this is equivalent except that it doesn't |
| 56 |
// handle compressed files. |
| 57 |
static inline Elf *dwelf_elf_begin(int fd) |
| 58 |
{ |
| 59 |
return elf_begin(fd, ELF_C_READ_MMAP_PRIVATE, NULL); |
| 60 |
} |
| 61 |
#endif |
| 62 |
|
| 63 |
DEFINE_HASH_MAP_FUNCTIONS(drgn_module_section_address_map, |
| 64 |
c_string_key_hash_pair, c_string_key_eq); |
| 65 |
|
| 66 |
// This is currently always DRGN_SUPPLEMENTARY_FILE_GNU_DEBUGALTLINK. |
| 67 |
struct drgn_module_wanted_supplementary_file { |
| 68 |
struct drgn_elf_file *file; |
| 69 |
// supplementary_path and checksum are owned by file. |
| 70 |
const char *supplementary_path; |
| 71 |
const void *checksum; |
| 72 |
size_t checksum_len; |
| 73 |
// checksum_str is a separate allocation. |
| 74 |
char *checksum_str; |
| 75 |
// Used to detect when the wanted supplementary file has changed in |
| 76 |
// order to avoid redundant attempts. |
| 77 |
uint64_t generation; |
| 78 |
}; |
| 79 |
|
| 80 |
#if WITH_DEBUGINFOD |
| 81 |
#if _ELFUTILS_PREREQ(0, 179) |
| 82 |
#define DRGN_DEBUGINFOD_0_179_FUNCTIONS \ |
| 83 |
X(debuginfod_set_user_data) \ |
| 84 |
X(debuginfod_get_user_data) \ |
| 85 |
X(debuginfod_get_url) |
| 86 |
#else |
| 87 |
#define DRGN_DEBUGINFOD_0_179_FUNCTIONS |
| 88 |
#endif |
| 89 |
|
| 90 |
#define DRGN_DEBUGINFOD_FUNCTIONS \ |
| 91 |
X(debuginfod_begin) \ |
| 92 |
X(debuginfod_end) \ |
| 93 |
X(debuginfod_find_debuginfo) \ |
| 94 |
X(debuginfod_find_executable) \ |
| 95 |
X(debuginfod_set_progressfn) \ |
| 96 |
DRGN_DEBUGINFOD_0_179_FUNCTIONS |
| 97 |
|
| 98 |
#if ENABLE_DLOPEN_DEBUGINFOD |
| 99 |
#include <dlfcn.h> |
| 100 |
|
| 101 |
#define X(name) static typeof(&name) drgn_##name; |
| 102 |
DRGN_DEBUGINFOD_FUNCTIONS |
| 103 |
#undef X |
| 104 |
|
| 105 |
__attribute__((__constructor__)) |
| 106 |
static void drgn_dlopen_debuginfod(void) |
| 107 |
{ |
| 108 |
void *handle = dlopen(DEBUGINFOD_SONAME, RTLD_LAZY); |
| 109 |
if (handle) { |
| 110 |
#define X(name) drgn_##name = dlsym(handle, #name); |
| 111 |
DRGN_DEBUGINFOD_FUNCTIONS |
| 112 |
#undef X |
| 113 |
|
| 114 |
#define X(name) || !drgn_##name |
| 115 |
if (0 DRGN_DEBUGINFOD_FUNCTIONS) { |
| 116 |
#undef X |
| 117 |
#define X(name) drgn_##name = NULL; |
| 118 |
DRGN_DEBUGINFOD_FUNCTIONS |
| 119 |
#undef X |
| 120 |
dlclose(handle); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
bool drgn_have_debuginfod(void) |
| 126 |
{ |
| 127 |
return drgn_debuginfod_begin != NULL; |
| 128 |
} |
| 129 |
#else |
| 130 |
// GCC and Clang optimize out the function pointer. |
| 131 |
#define X(name) static const typeof(&name) drgn_##name = name; |
| 132 |
DRGN_DEBUGINFOD_FUNCTIONS |
| 133 |
#undef X |
| 134 |
#endif |
| 135 |
|
| 136 |
#undef DRGN_DEBUGINFOD_FUNCTIONS |
| 137 |
#undef DRGN_DEBUGINFOD_0_179_FUNCTIONS |
| 138 |
#endif |
| 139 |
|
| 140 |
static inline Dwarf *drgn_elf_file_dwarf_key(struct drgn_elf_file * const *entry) |
| 141 |
{ |
| 142 |
return (*entry)->_dwarf; |
| 143 |
} |
| 144 |
DEFINE_HASH_TABLE_FUNCTIONS(drgn_elf_file_dwarf_table, drgn_elf_file_dwarf_key, |
| 145 |
ptr_key_hash_pair, scalar_key_eq); |
| 146 |
DEFINE_VECTOR(drgn_module_vector, struct drgn_module *); |
| 147 |
|
| 148 |
static inline const char *drgn_module_entry_name(struct drgn_module * const *entry) |
| 149 |
{ |
| 150 |
return (*entry)->name; |
| 151 |
} |
| 152 |
|
| 153 |
DEFINE_HASH_TABLE_FUNCTIONS(drgn_module_table, drgn_module_entry_name, |
| 154 |
c_string_key_hash_pair, c_string_key_eq); |
| 155 |
|
| 156 |
static inline uint64_t |
| 157 |
drgn_module_address_range_key(const struct drgn_module_address_range *entry) |
| 158 |
{ |
| 159 |
return entry->start; |
| 160 |
} |
| 161 |
|
| 162 |
DEFINE_BINARY_SEARCH_TREE_FUNCTIONS(drgn_module_address_tree, node, |
| 163 |
drgn_module_address_range_key, |
| 164 |
binary_search_tree_scalar_cmp, splay); |
| 165 |
|
| 166 |
static void drgn_module_free_section_addresses(struct drgn_module *module) |
| 167 |
{ |
| 168 |
hash_table_for_each(drgn_module_section_address_map, it, |
| 169 |
&module->section_addresses) |
| 170 |
free(it.entry->key); |
| 171 |
} |
| 172 |
|
| 173 |
LIBDRGN_PUBLIC |
| 174 |
struct drgn_module *drgn_module_find_by_name(struct drgn_program *prog, |
| 175 |
const char *name) |
| 176 |
{ |
| 177 |
struct drgn_module_table_iterator it = |
| 178 |
drgn_module_table_search(&prog->dbinfo.modules, &name); |
| 179 |
return it.entry ? *it.entry : NULL; |
| 180 |
} |
| 181 |
|
| 182 |
LIBDRGN_PUBLIC |
| 183 |
struct drgn_module *drgn_module_find_by_address(struct drgn_program *prog, |
| 184 |
uint64_t address) |
| 185 |
{ |
| 186 |
struct drgn_module_address_tree_iterator it = |
| 187 |
drgn_module_address_tree_search_le(&prog->dbinfo.modules_by_address, |
| 188 |
&address); |
| 189 |
if (!it.entry || address >= it.entry->end) |
| 190 |
return NULL; |
| 191 |
return it.entry->module; |
| 192 |
} |
| 193 |
|
| 194 |
static struct drgn_module *drgn_module_find(struct drgn_program *prog, |
| 195 |
enum drgn_module_kind kind, |
| 196 |
const char *name, uint64_t info) |
| 197 |
{ |
| 198 |
struct drgn_module_table_iterator it = |
| 199 |
drgn_module_table_search(&prog->dbinfo.modules, &name); |
| 200 |
if (!it.entry) |
| 201 |
return NULL; |
| 202 |
struct drgn_module *module = *it.entry; |
| 203 |
while (module->kind != kind || module->info != info) { |
| 204 |
module = module->next_same_name; |
| 205 |
if (!module) |
| 206 |
break; |
| 207 |
} |
| 208 |
return module; |
| 209 |
} |
| 210 |
|
| 211 |
static struct drgn_error * |
| 212 |
drgn_module_find_or_create(struct drgn_program *prog, |
| 213 |
enum drgn_module_kind kind, const char *name, |
| 214 |
uint64_t info, struct drgn_module **ret, |
| 215 |
bool *new_ret) |
| 216 |
{ |
| 217 |
struct drgn_error *err; |
| 218 |
|
| 219 |
struct hash_pair hp; |
| 220 |
struct drgn_module_table_iterator it; |
| 221 |
if (kind == DRGN_MODULE_MAIN) { |
| 222 |
if (prog->dbinfo.main_module) { |
| 223 |
if (strcmp(prog->dbinfo.main_module->name, name) != 0) { |
| 224 |
return drgn_error_create(DRGN_ERROR_LOOKUP, |
| 225 |
"main module already exists with different name"); |
| 226 |
} |
| 227 |
*ret = prog->dbinfo.main_module; |
| 228 |
if (new_ret) |
| 229 |
*new_ret = false; |
| 230 |
return NULL; |
| 231 |
} |
| 232 |
hp = drgn_module_table_hash(&name); |
| 233 |
it.entry = NULL; |
| 234 |
} else { |
| 235 |
hp = drgn_module_table_hash(&name); |
| 236 |
it = drgn_module_table_search_hashed(&prog->dbinfo.modules, |
| 237 |
&name, hp); |
| 238 |
if (it.entry) { |
| 239 |
struct drgn_module *module = *it.entry; |
| 240 |
do { |
| 241 |
if (module->kind == kind && module->info == info) { |
| 242 |
*ret = module; |
| 243 |
if (new_ret) |
| 244 |
*new_ret = false; |
| 245 |
return NULL; |
| 246 |
} |
| 247 |
module = module->next_same_name; |
| 248 |
} while (module); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
struct drgn_module *module = calloc(1, sizeof(*module)); |
| 253 |
if (!module) |
| 254 |
return &drgn_enomem; |
| 255 |
|
| 256 |
module->prog = prog; |
| 257 |
module->kind = kind; |
| 258 |
module->info = info; |
| 259 |
drgn_object_init(&module->object, prog); |
| 260 |
// Linux userspace core dumps usually filter out file-backed mappings |
| 261 |
// (see coredump_filter in core(5)), so we need the loaded file to read |
| 262 |
// the text. Additionally, .eh_frame is in the loaded file and not the |
| 263 |
// debug file. |
| 264 |
// |
| 265 |
// Linux kernel core dumps preserve the main kernel and kernel module |
| 266 |
// text, and the kernel doesn't use .eh_frame, so we don't need the |
| 267 |
// loaded file for the kernel. |
| 268 |
if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL |
| 269 |
&& (kind == DRGN_MODULE_MAIN || kind == DRGN_MODULE_RELOCATABLE)) |
| 270 |
module->loaded_file_status = DRGN_MODULE_FILE_DONT_NEED; |
| 271 |
else |
| 272 |
module->loaded_file_status = DRGN_MODULE_FILE_WANT; |
| 273 |
module->debug_file_status = DRGN_MODULE_FILE_WANT; |
| 274 |
|
| 275 |
module->name = strdup(name); |
| 276 |
if (!module->name) { |
| 277 |
err = &drgn_enomem; |
| 278 |
goto err_module; |
| 279 |
} |
| 280 |
|
| 281 |
if (it.entry) { |
| 282 |
module->next_same_name = *it.entry; |
| 283 |
*it.entry = module; |
| 284 |
} else if (drgn_module_table_insert_searched(&prog->dbinfo.modules, |
| 285 |
&module, hp, NULL) < 0) { |
| 286 |
err = &drgn_enomem; |
| 287 |
goto err_name; |
| 288 |
} |
| 289 |
if (kind == DRGN_MODULE_MAIN) |
| 290 |
prog->dbinfo.main_module = module; |
| 291 |
prog->dbinfo.modules_generation++; |
| 292 |
|
| 293 |
drgn_elf_file_dwarf_table_init(&module->split_dwarf_files); |
| 294 |
drgn_module_section_address_map_init(&module->section_addresses); |
| 295 |
|
| 296 |
SWITCH_ENUM(module->kind) { |
| 297 |
case DRGN_MODULE_MAIN: |
| 298 |
drgn_log_debug(prog, "created main module %s", module->name); |
| 299 |
break; |
| 300 |
case DRGN_MODULE_SHARED_LIBRARY: |
| 301 |
drgn_log_debug(prog, |
| 302 |
"created shared library module %s@0x%" PRIx64, |
| 303 |
module->name, module->info); |
| 304 |
break; |
| 305 |
case DRGN_MODULE_VDSO: |
| 306 |
drgn_log_debug(prog, "created vDSO module %s@0x%" PRIx64, |
| 307 |
module->name, module->info); |
| 308 |
break; |
| 309 |
case DRGN_MODULE_RELOCATABLE: |
| 310 |
drgn_log_debug(prog, "created relocatable module %s@0x%" PRIx64, |
| 311 |
module->name, module->info); |
| 312 |
break; |
| 313 |
case DRGN_MODULE_EXTRA: |
| 314 |
drgn_log_debug(prog, "created extra module %s 0x%" PRIx64, |
| 315 |
module->name, module->info); |
| 316 |
break; |
| 317 |
default: |
| 318 |
UNREACHABLE(); |
| 319 |
} |
| 320 |
|
| 321 |
*ret = module; |
| 322 |
if (new_ret) |
| 323 |
*new_ret = true; |
| 324 |
return NULL; |
| 325 |
|
| 326 |
err_name: |
| 327 |
free(module->name); |
| 328 |
err_module: |
| 329 |
drgn_object_deinit(&module->object); |
| 330 |
free(module); |
| 331 |
return err; |
| 332 |
} |
| 333 |
|
| 334 |
LIBDRGN_PUBLIC |
| 335 |
struct drgn_module *drgn_module_find_main(struct drgn_program *prog, |
| 336 |
const char *name) |
| 337 |
{ |
| 338 |
if (name && prog->dbinfo.main_module |
| 339 |
&& strcmp(prog->dbinfo.main_module->name, name) != 0) |
| 340 |
return NULL; |
| 341 |
return prog->dbinfo.main_module; |
| 342 |
} |
| 343 |
|
| 344 |
LIBDRGN_PUBLIC |
| 345 |
struct drgn_error *drgn_module_find_or_create_main(struct drgn_program *prog, |
| 346 |
const char *name, |
| 347 |
struct drgn_module **ret, |
| 348 |
bool *new_ret) |
| 349 |
{ |
| 350 |
return drgn_module_find_or_create(prog, DRGN_MODULE_MAIN, name, 0, ret, |
| 351 |
new_ret); |
| 352 |
} |
| 353 |
|
| 354 |
LIBDRGN_PUBLIC |
| 355 |
struct drgn_module *drgn_module_find_shared_library(struct drgn_program *prog, |
| 356 |
const char *name, |
| 357 |
uint64_t dynamic_address) |
| 358 |
{ |
| 359 |
return drgn_module_find(prog, DRGN_MODULE_SHARED_LIBRARY, name, |
| 360 |
dynamic_address); |
| 361 |
} |
| 362 |
|
| 363 |
LIBDRGN_PUBLIC struct drgn_error * |
| 364 |
drgn_module_find_or_create_shared_library(struct drgn_program *prog, |
| 365 |
const char *name, |
| 366 |
uint64_t dynamic_address, |
| 367 |
struct drgn_module **ret, |
| 368 |
bool *new_ret) |
| 369 |
{ |
| 370 |
return drgn_module_find_or_create(prog, DRGN_MODULE_SHARED_LIBRARY, |
| 371 |
name, dynamic_address, ret, new_ret); |
| 372 |
} |
| 373 |
|
| 374 |
LIBDRGN_PUBLIC |
| 375 |
struct drgn_module *drgn_module_find_vdso(struct drgn_program *prog, |
| 376 |
const char *name, |
| 377 |
uint64_t dynamic_address) |
| 378 |
{ |
| 379 |
return drgn_module_find(prog, DRGN_MODULE_VDSO, name, dynamic_address); |
| 380 |
} |
| 381 |
|
| 382 |
LIBDRGN_PUBLIC |
| 383 |
struct drgn_error *drgn_module_find_or_create_vdso(struct drgn_program *prog, |
| 384 |
const char *name, |
| 385 |
uint64_t dynamic_address, |
| 386 |
struct drgn_module **ret, |
| 387 |
bool *new_ret) |
| 388 |
{ |
| 389 |
return drgn_module_find_or_create(prog, DRGN_MODULE_VDSO, name, |
| 390 |
dynamic_address, ret, new_ret); |
| 391 |
} |
| 392 |
|
| 393 |
LIBDRGN_PUBLIC |
| 394 |
struct drgn_module *drgn_module_find_relocatable(struct drgn_program *prog, |
| 395 |
const char *name, |
| 396 |
uint64_t address) |
| 397 |
{ |
| 398 |
return drgn_module_find(prog, DRGN_MODULE_RELOCATABLE, name, address); |
| 399 |
} |
| 400 |
|
| 401 |
LIBDRGN_PUBLIC struct drgn_error * |
| 402 |
drgn_module_find_or_create_relocatable(struct drgn_program *prog, |
| 403 |
const char *name, uint64_t address, |
| 404 |
struct drgn_module **ret, bool *new_ret) |
| 405 |
{ |
| 406 |
return drgn_module_find_or_create(prog, DRGN_MODULE_RELOCATABLE, name, |
| 407 |
address, ret, new_ret); |
| 408 |
} |
| 409 |
|
| 410 |
LIBDRGN_PUBLIC |
| 411 |
struct drgn_module *drgn_module_find_extra(struct drgn_program *prog, |
| 412 |
const char *name, uint64_t id) |
| 413 |
{ |
| 414 |
return drgn_module_find(prog, DRGN_MODULE_EXTRA, name, id); |
| 415 |
} |
| 416 |
|
| 417 |
LIBDRGN_PUBLIC |
| 418 |
struct drgn_error *drgn_module_find_or_create_extra(struct drgn_program *prog, |
| 419 |
const char *name, |
| 420 |
uint64_t id, |
| 421 |
struct drgn_module **ret, |
| 422 |
bool *new_ret) |
| 423 |
{ |
| 424 |
return drgn_module_find_or_create(prog, DRGN_MODULE_EXTRA, name, id, |
| 425 |
ret, new_ret); |
| 426 |
} |
| 427 |
|
| 428 |
static void |
| 429 |
drgn_module_clear_wanted_supplementary_debug_file(struct drgn_module *module) |
| 430 |
{ |
| 431 |
struct drgn_module_wanted_supplementary_file *wanted = |
| 432 |
module->wanted_supplementary_debug_file; |
| 433 |
if (wanted) { |
| 434 |
free(wanted->checksum_str); |
| 435 |
if (wanted->file != module->loaded_file |
| 436 |
&& wanted->file != module->debug_file) |
| 437 |
drgn_elf_file_destroy(wanted->file); |
| 438 |
free(wanted); |
| 439 |
module->wanted_supplementary_debug_file = NULL; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
// Note: this doesn't remove the module from the module tables. |
| 444 |
static void drgn_module_destroy(struct drgn_module *module) |
| 445 |
{ |
| 446 |
drgn_module_free_section_addresses(module); |
| 447 |
drgn_module_section_address_map_deinit(&module->section_addresses); |
| 448 |
drgn_module_orc_info_deinit(module); |
| 449 |
drgn_module_dwarf_info_deinit(module); |
| 450 |
drgn_module_clear_wanted_supplementary_debug_file(module); |
| 451 |
drgn_elf_file_destroy(module->gnu_debugdata_file); |
| 452 |
drgn_elf_file_destroy(module->supplementary_debug_file); |
| 453 |
if (module->debug_file != module->loaded_file) |
| 454 |
drgn_elf_file_destroy(module->debug_file); |
| 455 |
drgn_elf_file_destroy(module->loaded_file); |
| 456 |
if (module->address_ranges != &module->single_address_range) |
| 457 |
free(module->address_ranges); |
| 458 |
free(module->build_id); |
| 459 |
free(module->name); |
| 460 |
drgn_object_deinit(&module->object); |
| 461 |
free(module); |
| 462 |
} |
| 463 |
|
| 464 |
static void drgn_module_delete_address_ranges(struct drgn_module *module) |
| 465 |
{ |
| 466 |
for (size_t i = 0; i < module->num_address_ranges; i++) { |
| 467 |
drgn_module_address_tree_delete_entry(&module->prog->dbinfo.modules_by_address, |
| 468 |
&module->address_ranges[i]); |
| 469 |
} |
| 470 |
if (module->address_ranges != &module->single_address_range) |
| 471 |
free(module->address_ranges); |
| 472 |
} |
| 473 |
|
| 474 |
void drgn_module_delete(struct drgn_module *module) |
| 475 |
{ |
| 476 |
assert(!module->loaded_file); |
| 477 |
assert(!module->debug_file); |
| 478 |
drgn_module_delete_address_ranges(module); |
| 479 |
// So drgn_module_destroy() doesn't free it again. |
| 480 |
module->address_ranges = NULL; |
| 481 |
|
| 482 |
const char *name = module->name; |
| 483 |
struct drgn_module_table_iterator it = |
| 484 |
drgn_module_table_search(&module->prog->dbinfo.modules, &name); |
| 485 |
if (*it.entry == module && !module->next_same_name) { |
| 486 |
drgn_module_table_delete_iterator(&module->prog->dbinfo.modules, |
| 487 |
it); |
| 488 |
} else { |
| 489 |
struct drgn_module **modulep = it.entry; |
| 490 |
while (*modulep != module) |
| 491 |
modulep = &(*modulep)->next_same_name; |
| 492 |
*modulep = module->next_same_name; |
| 493 |
} |
| 494 |
if (module->kind == DRGN_MODULE_MAIN) |
| 495 |
module->prog->dbinfo.main_module = NULL; |
| 496 |
module->prog->dbinfo.modules_generation++; |
| 497 |
|
| 498 |
drgn_module_destroy(module); |
| 499 |
} |
| 500 |
|
| 501 |
LIBDRGN_PUBLIC |
| 502 |
struct drgn_program *drgn_module_program(const struct drgn_module *module) |
| 503 |
{ |
| 504 |
return module->prog; |
| 505 |
} |
| 506 |
|
| 507 |
LIBDRGN_PUBLIC |
| 508 |
enum drgn_module_kind drgn_module_kind(const struct drgn_module *module) |
| 509 |
{ |
| 510 |
return module->kind; |
| 511 |
} |
| 512 |
|
| 513 |
LIBDRGN_PUBLIC const char *drgn_module_name(const struct drgn_module *module) |
| 514 |
{ |
| 515 |
return module->name; |
| 516 |
} |
| 517 |
|
| 518 |
LIBDRGN_PUBLIC uint64_t drgn_module_info(const struct drgn_module *module) |
| 519 |
{ |
| 520 |
return module->info; |
| 521 |
} |
| 522 |
|
| 523 |
LIBDRGN_PUBLIC |
| 524 |
bool drgn_module_num_address_ranges(const struct drgn_module *module, |
| 525 |
size_t *ret) |
| 526 |
{ |
| 527 |
*ret = module->num_address_ranges; |
| 528 |
return module->address_ranges != NULL; |
| 529 |
} |
| 530 |
|
| 531 |
LIBDRGN_PUBLIC bool drgn_module_address_range(const struct drgn_module *module, |
| 532 |
size_t i, uint64_t *start_ret, |
| 533 |
uint64_t *end_ret) |
| 534 |
{ |
| 535 |
if (i >= module->num_address_ranges) |
| 536 |
return false; |
| 537 |
*start_ret = module->address_ranges[i].start; |
| 538 |
*end_ret = module->address_ranges[i].end; |
| 539 |
return true; |
| 540 |
} |
| 541 |
|
| 542 |
static struct drgn_error * |
| 543 |
drgn_check_module_address_range_overlap(struct drgn_module *module, |
| 544 |
uint64_t start, uint64_t end) |
| 545 |
{ |
| 546 |
for (auto it = drgn_module_address_tree_search_le(&module->prog->dbinfo.modules_by_address, |
| 547 |
&start); |
| 548 |
it.entry && it.entry->start < end; |
| 549 |
it = drgn_module_address_tree_next(it)) { |
| 550 |
if (start < it.entry->end && it.entry->module != module) { |
| 551 |
return drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT, |
| 552 |
"%s [0x%" PRIx64 ", 0x%" PRIx64 ") overlaps " |
| 553 |
"%s [0x%" PRIx64 ", 0x%" PRIx64 ")", |
| 554 |
module->name, start, end, |
| 555 |
it.entry->module->name, |
| 556 |
it.entry->start, |
| 557 |
it.entry->end); |
| 558 |
} |
| 559 |
} |
| 560 |
return NULL; |
| 561 |
} |
| 562 |
|
| 563 |
LIBDRGN_PUBLIC |
| 564 |
struct drgn_error *drgn_module_set_address_range(struct drgn_module *module, |
| 565 |
uint64_t start, uint64_t end) |
| 566 |
{ |
| 567 |
struct drgn_error *err; |
| 568 |
|
| 569 |
// This is a special case instead of a wrapper around |
| 570 |
// drgn_module_set_address_ranges() so we can avoid allocating memory. |
| 571 |
// Since the old address range might be module->single_address_range, |
| 572 |
// this has to do things in a different order. |
| 573 |
|
| 574 |
if (start >= end) { |
| 575 |
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT, |
| 576 |
"invalid module address range"); |
| 577 |
} |
| 578 |
|
| 579 |
err = drgn_check_module_address_range_overlap(module, start, end); |
| 580 |
if (err) |
| 581 |
return err; |
| 582 |
|
| 583 |
drgn_module_delete_address_ranges(module); |
| 584 |
|
| 585 |
module->single_address_range.start = start; |
| 586 |
module->single_address_range.end = end; |
| 587 |
module->single_address_range.module = module; |
| 588 |
|
| 589 |
int r = drgn_module_address_tree_insert(&module->prog->dbinfo.modules_by_address, |
| 590 |
&module->single_address_range, |
| 591 |
NULL); |
| 592 |
(void)r; |
| 593 |
assert(r > 0); // We checked for overlap. |
| 594 |
|
| 595 |
module->address_ranges = &module->single_address_range; |
| 596 |
module->num_address_ranges = 1; |
| 597 |
return NULL; |
| 598 |
} |
| 599 |
|
| 600 |
static int drgn_module_address_range_compare(const void *_a, const void *_b) |
| 601 |
{ |
| 602 |
const struct drgn_module_address_range *a = _a; |
| 603 |
const struct drgn_module_address_range *b = _b; |
| 604 |
return (a->start > b->start) - (a->start < b->start); |
| 605 |
} |
| 606 |
|
| 607 |
LIBDRGN_PUBLIC |
| 608 |
struct drgn_error *drgn_module_set_address_ranges(struct drgn_module *module, |
| 609 |
uint64_t ranges[][2], |
| 610 |
size_t num_ranges) |
| 611 |
{ |
| 612 |
struct drgn_error *err; |
| 613 |
|
| 614 |
if (num_ranges == 1) { |
| 615 |
return drgn_module_set_address_range(module, ranges[0][0], |
| 616 |
ranges[0][1]); |
| 617 |
} |
| 618 |
|
| 619 |
_cleanup_free_ struct drgn_module_address_range *address_ranges = NULL; |
| 620 |
if (num_ranges) { |
| 621 |
address_ranges = |
| 622 |
malloc_array(num_ranges, sizeof(*address_ranges)); |
| 623 |
if (!address_ranges) |
| 624 |
return &drgn_enomem; |
| 625 |
for (size_t i = 0; i < num_ranges; i++) { |
| 626 |
if (ranges[i][0] >= ranges[i][1]) { |
| 627 |
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT, |
| 628 |
"invalid module address range"); |
| 629 |
} |
| 630 |
err = drgn_check_module_address_range_overlap(module, |
| 631 |
ranges[i][0], |
| 632 |
ranges[i][1]); |
| 633 |
if (err) |
| 634 |
return err; |
| 635 |
address_ranges[i].start = ranges[i][0]; |
| 636 |
address_ranges[i].end = ranges[i][1]; |
| 637 |
address_ranges[i].module = module; |
| 638 |
} |
| 639 |
|
| 640 |
qsort(address_ranges, num_ranges, sizeof(address_ranges[0]), |
| 641 |
drgn_module_address_range_compare); |
| 642 |
|
| 643 |
for (size_t i = 1; i < num_ranges; i++) { |
| 644 |
if (address_ranges[i].start < address_ranges[i - 1].end) { |
| 645 |
return drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT, |
| 646 |
"%s [0x%" PRIx64 ", 0x%" PRIx64 ") overlaps " |
| 647 |
"[0x%" PRIx64 ", 0x%" PRIx64 ")", |
| 648 |
module->name, |
| 649 |
address_ranges[i - 1].start, |
| 650 |
address_ranges[i - 1].end, |
| 651 |
address_ranges[i].start, |
| 652 |
address_ranges[i].end); |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
drgn_module_delete_address_ranges(module); |
| 658 |
|
| 659 |
for (size_t i = 0; i < num_ranges; i++) { |
| 660 |
int r = drgn_module_address_tree_insert(&module->prog->dbinfo.modules_by_address, |
| 661 |
&address_ranges[i], |
| 662 |
NULL); |
| 663 |
(void)r; |
| 664 |
assert(r > 0); // We checked for overlap. |
| 665 |
} |
| 666 |
|
| 667 |
if (num_ranges) { |
| 668 |
module->address_ranges = no_cleanup_ptr(address_ranges); |
| 669 |
} else { |
| 670 |
// We need a non-NULL pointer to distinguish this from the unset |
| 671 |
// case. |
| 672 |
module->address_ranges = &module->single_address_range; |
| 673 |
} |
| 674 |
module->num_address_ranges = num_ranges; |
| 675 |
return NULL; |
| 676 |
} |
| 677 |
|
| 678 |
LIBDRGN_PUBLIC void drgn_module_unset_address_ranges(struct drgn_module *module) |
| 679 |
{ |
| 680 |
drgn_module_delete_address_ranges(module); |
| 681 |
module->address_ranges = NULL; |
| 682 |
module->num_address_ranges = 0; |
| 683 |
} |
| 684 |
|
| 685 |
LIBDRGN_PUBLIC |
| 686 |
bool drgn_module_contains_address(const struct drgn_module *module, |
| 687 |
uint64_t address) |
| 688 |
{ |
| 689 |
for (size_t i = 0; i < module->num_address_ranges; i++) { |
| 690 |
if (module->address_ranges[i].start <= address |
| 691 |
&& address < module->address_ranges[i].end) |
| 692 |
return true; |
| 693 |
} |
| 694 |
return false; |
| 695 |
} |
| 696 |
|
| 697 |
LIBDRGN_PUBLIC |
| 698 |
const char *drgn_module_build_id(const struct drgn_module *module, |
| 699 |
const void **raw_ret, size_t *raw_len_ret) |
| 700 |
{ |
| 701 |
if (raw_ret) |
| 702 |
*raw_ret = module->build_id; |
| 703 |
if (raw_len_ret) |
| 704 |
*raw_len_ret = module->build_id_len; |
| 705 |
return module->build_id_str; |
| 706 |
} |
| 707 |
|
| 708 |
static void *drgn_module_alloc_build_id(size_t build_id_len) |
| 709 |
{ |
| 710 |
size_t alloc_size; |
| 711 |
if (__builtin_mul_overflow(build_id_len, 3U, &alloc_size) || |
| 712 |
__builtin_add_overflow(alloc_size, 1U, &alloc_size)) |
| 713 |
return NULL; |
| 714 |
return malloc(alloc_size); |
| 715 |
} |
| 716 |
|
| 717 |
static void drgn_module_set_build_id_impl(struct drgn_module *module, |
| 718 |
const void *build_id, |
| 719 |
size_t build_id_len, |
| 720 |
void *build_id_buf) |
| 721 |
{ |
| 722 |
module->build_id = build_id_buf; |
| 723 |
memcpy(module->build_id, build_id, build_id_len); |
| 724 |
|
| 725 |
module->build_id_len = build_id_len; |
| 726 |
|
| 727 |
module->build_id_str = (char *)build_id_buf + build_id_len; |
| 728 |
hexlify(build_id, build_id_len, module->build_id_str); |
| 729 |
module->build_id_str[2 * build_id_len] = '\0'; |
| 730 |
} |
| 731 |
|
| 732 |
LIBDRGN_PUBLIC |
| 733 |
struct drgn_error *drgn_module_set_build_id(struct drgn_module *module, |
| 734 |
const void *build_id, |
| 735 |
size_t build_id_len) |
| 736 |
{ |
| 737 |
if (build_id_len == 0) { |
| 738 |
free(module->build_id); |
| 739 |
module->build_id = NULL; |
| 740 |
module->build_id_len = 0; |
| 741 |
module->build_id_str = NULL; |
| 742 |
return NULL; |
| 743 |
} |
| 744 |
|
| 745 |
char *build_id_buf = drgn_module_alloc_build_id(build_id_len); |
| 746 |
if (!build_id_buf) |
| 747 |
return &drgn_enomem; |
| 748 |
free(module->build_id); |
| 749 |
drgn_module_set_build_id_impl(module, build_id, build_id_len, |
| 750 |
build_id_buf); |
| 751 |
return NULL; |
| 752 |
} |
| 753 |
|
| 754 |
static struct drgn_error * |
| 755 |
drgn_module_section_addresses_allowed(struct drgn_module *module, bool modify) |
| 756 |
{ |
| 757 |
if (module->kind != DRGN_MODULE_RELOCATABLE) { |
| 758 |
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT, |
| 759 |
"section addresses are only supported for relocatable modules"); |
| 760 |
} |
| 761 |
if (modify && (module->loaded_file || module->debug_file)) { |
| 762 |
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT, |
| 763 |
"section addresses cannot be modified after file is set"); |
| 764 |
} |
| 765 |
return NULL; |
| 766 |
} |
| 767 |
|
| 768 |
LIBDRGN_PUBLIC |
| 769 |
struct drgn_error *drgn_module_get_section_address(struct drgn_module *module, |
| 770 |
const char *name, |
| 771 |
uint64_t *ret) |
| 772 |
{ |
| 773 |
struct drgn_error *err = |
| 774 |
drgn_module_section_addresses_allowed(module, false); |
| 775 |
if (err) |
| 776 |
return err; |
| 777 |
struct drgn_module_section_address_map_iterator it = |
| 778 |
drgn_module_section_address_map_search(&module->section_addresses, |
| 779 |
(char **)&name); |
| 780 |
if (!it.entry) |
| 781 |
return &drgn_not_found; |
| 782 |
*ret = it.entry->value; |
| 783 |
return NULL; |
| 784 |
} |
| 785 |
|
| 786 |
LIBDRGN_PUBLIC |
| 787 |
struct drgn_error *drgn_module_set_section_address(struct drgn_module *module, |
| 788 |
const char *name, |
| 789 |
uint64_t address) |
| 790 |
{ |
| 791 |
struct drgn_error *err = |
| 792 |
drgn_module_section_addresses_allowed(module, true); |
| 793 |
if (err) |
| 794 |
return err; |
| 795 |
|
| 796 |
struct hash_pair hp = |
| 797 |
drgn_module_section_address_map_hash((char **)&name); |
| 798 |
struct drgn_module_section_address_map_iterator it = |
| 799 |
drgn_module_section_address_map_search_hashed(&module->section_addresses, |
| 800 |
(char **)&name, |
| 801 |
hp); |
| 802 |
if (it.entry) { |
| 803 |
it.entry->value = address; |
| 804 |
return NULL; |
| 805 |
} |
| 806 |
struct drgn_module_section_address_map_entry entry = { |
| 807 |
.key = strdup(name), |
| 808 |
.value = address, |
| 809 |
}; |
| 810 |
if (!entry.key) |
| 811 |
return &drgn_enomem; |
| 812 |
if (drgn_module_section_address_map_insert_searched(&module->section_addresses, |
| 813 |
&entry, hp, |
| 814 |
NULL) < 0) { |
| 815 |
free(entry.key); |
| 816 |
return &drgn_enomem; |
| 817 |
} |
| 818 |
module->section_addresses_generation++; |
| 819 |
return NULL; |
| 820 |
} |
| 821 |
|
| 822 |
struct drgn_error *drgn_module_delete_section_address(struct drgn_module *module, |
| 823 |
const char *name) |
| 824 |
{ |
| 825 |
struct drgn_error *err = |
| 826 |
drgn_module_section_addresses_allowed(module, true); |
| 827 |
if (err) |
| 828 |
return err; |
| 829 |
|
| 830 |
struct hash_pair hp = |
| 831 |
drgn_module_section_address_map_hash((char **)&name); |
| 832 |
struct drgn_module_section_address_map_iterator it = |
| 833 |
drgn_module_section_address_map_search_hashed(&module->section_addresses, |
| 834 |
(char **)&name, |
| 835 |
hp); |
| 836 |
if (!it.entry) |
| 837 |
return &drgn_not_found; |
| 838 |
|
| 839 |
_cleanup_free_ _unused_ char *key_to_free = it.entry->key; |
| 840 |
drgn_module_section_address_map_delete_iterator_hashed(&module->section_addresses, |
| 841 |
it, hp); |
| 842 |
module->section_addresses_generation++; |
| 843 |
return NULL; |
| 844 |
} |
| 845 |
|
| 846 |
LIBDRGN_PUBLIC |
| 847 |
struct drgn_error *drgn_module_num_section_addresses(struct drgn_module *module, |
| 848 |
size_t *ret) |
| 849 |
{ |
| 850 |
struct drgn_error *err = |
| 851 |
drgn_module_section_addresses_allowed(module, false); |
| 852 |
if (err) |
| 853 |
return err; |
| 854 |
*ret = drgn_module_section_address_map_size(&module->section_addresses); |
| 855 |
return NULL; |
| 856 |
} |
| 857 |
|
| 858 |
struct drgn_module_section_address_iterator { |
| 859 |
struct drgn_module *module; |
| 860 |
struct drgn_module_section_address_map_iterator map_it; |
| 861 |
uint64_t generation; |
| 862 |
}; |
| 863 |
|
| 864 |
LIBDRGN_PUBLIC struct drgn_error * |
| 865 |
drgn_module_section_address_iterator_create(struct drgn_module *module, |
| 866 |
struct drgn_module_section_address_iterator **ret) |
| 867 |
{ |
| 868 |
struct drgn_error *err = |
| 869 |
drgn_module_section_addresses_allowed(module, false); |
| 870 |
if (err) |
| 871 |
return err; |
| 872 |
|
| 873 |
struct drgn_module_section_address_iterator *it = malloc(sizeof(*it)); |
| 874 |
if (!it) |
| 875 |
return &drgn_enomem; |
| 876 |
it->module = module; |
| 877 |
it->map_it = drgn_module_section_address_map_first(&module->section_addresses); |
| 878 |
it->generation = module->section_addresses_generation; |
| 879 |
*ret = it; |
| 880 |
return NULL; |
| 881 |
} |
| 882 |
|
| 883 |
LIBDRGN_PUBLIC void |
| 884 |
drgn_module_section_address_iterator_destroy(struct drgn_module_section_address_iterator *it) |
| 885 |
{ |
| 886 |
free(it); |
| 887 |
} |
| 888 |
|
| 889 |
LIBDRGN_PUBLIC struct drgn_module * |
| 890 |
drgn_module_section_address_iterator_module(struct drgn_module_section_address_iterator *it) |
| 891 |
{ |
| 892 |
return it->module; |
| 893 |
} |
| 894 |
|
| 895 |
LIBDRGN_PUBLIC struct drgn_error * |
| 896 |
drgn_module_section_address_iterator_next(struct drgn_module_section_address_iterator *it, |
| 897 |
const char **name_ret, |
| 898 |
uint64_t *address_ret) |
| 899 |
{ |
| 900 |
if (it->map_it.entry) { |
| 901 |
if (it->generation != it->module->section_addresses_generation) { |
| 902 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 903 |
"section addresses changed during iteration"); |
| 904 |
} |
| 905 |
*name_ret = it->map_it.entry->key; |
| 906 |
if (address_ret) |
| 907 |
*address_ret = it->map_it.entry->value; |
| 908 |
it->map_it = drgn_module_section_address_map_next(it->map_it); |
| 909 |
} else { |
| 910 |
*name_ret = NULL; |
| 911 |
} |
| 912 |
return NULL; |
| 913 |
} |
| 914 |
|
| 915 |
LIBDRGN_PUBLIC enum drgn_module_file_status |
| 916 |
drgn_module_loaded_file_status(const struct drgn_module *module) |
| 917 |
{ |
| 918 |
return module->loaded_file_status; |
| 919 |
} |
| 920 |
|
| 921 |
static bool |
| 922 |
drgn_can_change_module_file_status(enum drgn_module_file_status old_status, |
| 923 |
enum drgn_module_file_status new_status) |
| 924 |
{ |
| 925 |
SWITCH_ENUM(old_status) { |
| 926 |
case DRGN_MODULE_FILE_WANT: |
| 927 |
case DRGN_MODULE_FILE_DONT_WANT: |
| 928 |
case DRGN_MODULE_FILE_DONT_NEED: |
| 929 |
SWITCH_ENUM(new_status) { |
| 930 |
case DRGN_MODULE_FILE_WANT: |
| 931 |
case DRGN_MODULE_FILE_DONT_WANT: |
| 932 |
case DRGN_MODULE_FILE_DONT_NEED: |
| 933 |
return true; |
| 934 |
case DRGN_MODULE_FILE_HAVE: |
| 935 |
case DRGN_MODULE_FILE_WANT_SUPPLEMENTARY: |
| 936 |
default: |
| 937 |
return false; |
| 938 |
} |
| 939 |
case DRGN_MODULE_FILE_HAVE: |
| 940 |
return new_status == DRGN_MODULE_FILE_HAVE; |
| 941 |
case DRGN_MODULE_FILE_WANT_SUPPLEMENTARY: |
| 942 |
SWITCH_ENUM(new_status) { |
| 943 |
case DRGN_MODULE_FILE_WANT: |
| 944 |
case DRGN_MODULE_FILE_DONT_WANT: |
| 945 |
case DRGN_MODULE_FILE_DONT_NEED: |
| 946 |
case DRGN_MODULE_FILE_WANT_SUPPLEMENTARY: |
| 947 |
return true; |
| 948 |
case DRGN_MODULE_FILE_HAVE: |
| 949 |
default: |
| 950 |
return false; |
| 951 |
} |
| 952 |
default: |
| 953 |
UNREACHABLE(); |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
LIBDRGN_PUBLIC |
| 958 |
bool drgn_module_set_loaded_file_status(struct drgn_module *module, |
| 959 |
enum drgn_module_file_status status) |
| 960 |
{ |
| 961 |
if (!drgn_can_change_module_file_status(module->loaded_file_status, |
| 962 |
status)) |
| 963 |
return false; |
| 964 |
module->loaded_file_status = status; |
| 965 |
return true; |
| 966 |
} |
| 967 |
|
| 968 |
LIBDRGN_PUBLIC |
| 969 |
bool drgn_module_wants_loaded_file(const struct drgn_module *module) |
| 970 |
{ |
| 971 |
SWITCH_ENUM(module->loaded_file_status) { |
| 972 |
case DRGN_MODULE_FILE_WANT: |
| 973 |
return true; |
| 974 |
case DRGN_MODULE_FILE_HAVE: |
| 975 |
case DRGN_MODULE_FILE_DONT_WANT: |
| 976 |
case DRGN_MODULE_FILE_DONT_NEED: |
| 977 |
return false; |
| 978 |
case DRGN_MODULE_FILE_WANT_SUPPLEMENTARY: |
| 979 |
default: |
| 980 |
UNREACHABLE(); |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
LIBDRGN_PUBLIC enum drgn_module_file_status |
| 985 |
drgn_module_debug_file_status(const struct drgn_module *module) |
| 986 |
{ |
| 987 |
return module->debug_file_status; |
| 988 |
} |
| 989 |
|
| 990 |
LIBDRGN_PUBLIC |
| 991 |
bool drgn_module_set_debug_file_status(struct drgn_module *module, |
| 992 |
enum drgn_module_file_status status) |
| 993 |
{ |
| 994 |
if (!drgn_can_change_module_file_status(module->debug_file_status, |
| 995 |
status)) |
| 996 |
return false; |
| 997 |
if (module->debug_file_status == DRGN_MODULE_FILE_WANT_SUPPLEMENTARY |
| 998 |
&& status != DRGN_MODULE_FILE_WANT_SUPPLEMENTARY) |
| 999 |
drgn_module_clear_wanted_supplementary_debug_file(module); |
| 1000 |
module->debug_file_status = status; |
| 1001 |
return true; |
| 1002 |
} |
| 1003 |
|
| 1004 |
LIBDRGN_PUBLIC |
| 1005 |
bool drgn_module_wants_debug_file(const struct drgn_module *module) |
| 1006 |
{ |
| 1007 |
SWITCH_ENUM(module->debug_file_status) { |
| 1008 |
case DRGN_MODULE_FILE_WANT: |
| 1009 |
case DRGN_MODULE_FILE_WANT_SUPPLEMENTARY: |
| 1010 |
return true; |
| 1011 |
case DRGN_MODULE_FILE_HAVE: |
| 1012 |
case DRGN_MODULE_FILE_DONT_WANT: |
| 1013 |
case DRGN_MODULE_FILE_DONT_NEED: |
| 1014 |
return false; |
| 1015 |
default: |
| 1016 |
UNREACHABLE(); |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
LIBDRGN_PUBLIC |
| 1021 |
const char *drgn_module_loaded_file_path(const struct drgn_module *module) |
| 1022 |
{ |
| 1023 |
return module->loaded_file ? module->loaded_file->path : NULL; |
| 1024 |
} |
| 1025 |
|
| 1026 |
LIBDRGN_PUBLIC |
| 1027 |
uint64_t drgn_module_loaded_file_bias(const struct drgn_module *module) |
| 1028 |
{ |
| 1029 |
return module->loaded_file_bias; |
| 1030 |
} |
| 1031 |
|
| 1032 |
LIBDRGN_PUBLIC |
| 1033 |
const char *drgn_module_debug_file_path(const struct drgn_module *module) |
| 1034 |
{ |
| 1035 |
return module->debug_file ? module->debug_file->path : NULL; |
| 1036 |
} |
| 1037 |
|
| 1038 |
LIBDRGN_PUBLIC |
| 1039 |
uint64_t drgn_module_debug_file_bias(const struct drgn_module *module) |
| 1040 |
{ |
| 1041 |
return module->debug_file_bias; |
| 1042 |
} |
| 1043 |
|
| 1044 |
LIBDRGN_PUBLIC enum drgn_supplementary_file_kind |
| 1045 |
drgn_module_supplementary_debug_file_kind(const struct drgn_module *module) |
| 1046 |
{ |
| 1047 |
return module->supplementary_debug_file |
| 1048 |
? DRGN_SUPPLEMENTARY_FILE_GNU_DEBUGALTLINK |
| 1049 |
: DRGN_SUPPLEMENTARY_FILE_NONE; |
| 1050 |
} |
| 1051 |
|
| 1052 |
LIBDRGN_PUBLIC const char * |
| 1053 |
drgn_module_supplementary_debug_file_path(const struct drgn_module *module) |
| 1054 |
{ |
| 1055 |
return module->supplementary_debug_file |
| 1056 |
? module->supplementary_debug_file->path : NULL; |
| 1057 |
} |
| 1058 |
|
| 1059 |
LIBDRGN_PUBLIC enum drgn_supplementary_file_kind |
| 1060 |
drgn_module_wanted_supplementary_debug_file(struct drgn_module *module, |
| 1061 |
const char **debug_file_path_ret, |
| 1062 |
const char **supplementary_path_ret, |
| 1063 |
const void **checksum_ret, |
| 1064 |
size_t *checksum_len_ret) |
| 1065 |
{ |
| 1066 |
struct drgn_module_wanted_supplementary_file *wanted = |
| 1067 |
module->wanted_supplementary_debug_file; |
| 1068 |
if (debug_file_path_ret) |
| 1069 |
*debug_file_path_ret = wanted ? wanted->file->path : NULL; |
| 1070 |
if (supplementary_path_ret) |
| 1071 |
*supplementary_path_ret = wanted ? wanted->supplementary_path : NULL; |
| 1072 |
if (checksum_ret) |
| 1073 |
*checksum_ret = wanted ? wanted->checksum : NULL; |
| 1074 |
if (checksum_len_ret) |
| 1075 |
*checksum_len_ret = wanted ? wanted->checksum_len : 0; |
| 1076 |
return wanted |
| 1077 |
? DRGN_SUPPLEMENTARY_FILE_GNU_DEBUGALTLINK |
| 1078 |
: DRGN_SUPPLEMENTARY_FILE_NONE; |
| 1079 |
} |
| 1080 |
|
| 1081 |
LIBDRGN_PUBLIC struct drgn_error * |
| 1082 |
drgn_module_object(const struct drgn_module *module, struct drgn_object *ret) |
| 1083 |
{ |
| 1084 |
return drgn_object_copy(ret, &module->object); |
| 1085 |
} |
| 1086 |
|
| 1087 |
LIBDRGN_PUBLIC struct drgn_error * |
| 1088 |
drgn_module_set_object(struct drgn_module *module, const struct drgn_object *obj) |
| 1089 |
{ |
| 1090 |
return drgn_object_copy(&module->object, obj); |
| 1091 |
} |
| 1092 |
|
| 1093 |
static struct drgn_error * |
| 1094 |
drgn_program_register_debug_info_finder_impl(struct drgn_program *prog, |
| 1095 |
struct drgn_debug_info_finder *finder, |
| 1096 |
const char *name, |
| 1097 |
const struct drgn_debug_info_finder_ops *ops, |
| 1098 |
void *arg, size_t enable_index) |
| 1099 |
{ |
| 1100 |
struct drgn_error *err; |
| 1101 |
bool should_free = !finder; |
| 1102 |
if (finder) { |
| 1103 |
finder->handler.name = name; |
| 1104 |
} else { |
| 1105 |
finder = malloc(sizeof(*finder)); |
| 1106 |
if (!finder) |
| 1107 |
return &drgn_enomem; |
| 1108 |
finder->handler.name = strdup(name); |
| 1109 |
if (!finder->handler.name) { |
| 1110 |
free(finder); |
| 1111 |
return &drgn_enomem; |
| 1112 |
} |
| 1113 |
} |
| 1114 |
finder->handler.free = should_free; |
| 1115 |
finder->ops = *ops; |
| 1116 |
finder->arg = arg; |
| 1117 |
err = drgn_handler_list_register(&prog->dbinfo.debug_info_finders, |
| 1118 |
&finder->handler, enable_index, |
| 1119 |
"module debug info finder"); |
| 1120 |
if (err && should_free) { |
| 1121 |
free((char *)finder->handler.name); |
| 1122 |
free(finder); |
| 1123 |
} |
| 1124 |
return err; |
| 1125 |
} |
| 1126 |
|
| 1127 |
LIBDRGN_PUBLIC struct drgn_error * |
| 1128 |
drgn_program_register_debug_info_finder(struct drgn_program *prog, |
| 1129 |
const char *name, |
| 1130 |
const struct drgn_debug_info_finder_ops *ops, |
| 1131 |
void *arg, size_t enable_index) |
| 1132 |
{ |
| 1133 |
return drgn_program_register_debug_info_finder_impl(prog, NULL, name, |
| 1134 |
ops, arg, |
| 1135 |
enable_index); |
| 1136 |
} |
| 1137 |
|
| 1138 |
LIBDRGN_PUBLIC struct drgn_error * |
| 1139 |
drgn_program_registered_debug_info_finders(struct drgn_program *prog, |
| 1140 |
const char ***names_ret, |
| 1141 |
size_t *count_ret) |
| 1142 |
{ |
| 1143 |
return drgn_handler_list_registered(&prog->dbinfo.debug_info_finders, |
| 1144 |
names_ret, count_ret); |
| 1145 |
} |
| 1146 |
|
| 1147 |
LIBDRGN_PUBLIC struct drgn_error * |
| 1148 |
drgn_program_set_enabled_debug_info_finders(struct drgn_program *prog, |
| 1149 |
const char * const *names, |
| 1150 |
size_t count) |
| 1151 |
{ |
| 1152 |
return drgn_handler_list_set_enabled(&prog->dbinfo.debug_info_finders, |
| 1153 |
names, count, |
| 1154 |
"module debug info finder"); |
| 1155 |
} |
| 1156 |
|
| 1157 |
LIBDRGN_PUBLIC struct drgn_error * |
| 1158 |
drgn_program_enabled_debug_info_finders(struct drgn_program *prog, |
| 1159 |
const char ***names_ret, |
| 1160 |
size_t *count_ret) |
| 1161 |
{ |
| 1162 |
return drgn_handler_list_enabled(&prog->dbinfo.debug_info_finders, |
| 1163 |
names_ret, count_ret); |
| 1164 |
} |
| 1165 |
|
| 1166 |
LIBDRGN_PUBLIC struct drgn_debug_info_options * |
| 1167 |
drgn_program_debug_info_options(struct drgn_program *prog) |
| 1168 |
{ |
| 1169 |
return &prog->dbinfo.options; |
| 1170 |
} |
| 1171 |
|
| 1172 |
static struct drgn_error * |
| 1173 |
drgn_module_set_wanted_gnu_debugaltlink(struct drgn_module *module, |
| 1174 |
struct drgn_elf_file *file) |
| 1175 |
{ |
| 1176 |
struct drgn_error *err; |
| 1177 |
struct drgn_program *prog = module->prog; |
| 1178 |
|
| 1179 |
// We don't cache .gnu_debugaltlink, and it doesn't need relocation, so |
| 1180 |
// don't use drgn_elf_file_read_section(). |
| 1181 |
Elf_Data *data; |
| 1182 |
err = read_elf_section(file->scns[DRGN_SCN_GNU_DEBUGALTLINK], &data); |
| 1183 |
if (err) { |
| 1184 |
if (!drgn_error_is_fatal(err)) { |
| 1185 |
drgn_error_log_debug(prog, err, |
| 1186 |
"%s: couldn't read .gnu_debugaltlink; ignoring debug info: ", |
| 1187 |
file->path); |
| 1188 |
drgn_error_destroy(err); |
| 1189 |
err = NULL; |
| 1190 |
} |
| 1191 |
return err; |
| 1192 |
} |
| 1193 |
|
| 1194 |
const char *debugaltlink = data->d_buf; |
| 1195 |
const char *nul = memchr(debugaltlink, 0, data->d_size); |
| 1196 |
if (!nul || nul + 1 == debugaltlink + data->d_size) { |
| 1197 |
drgn_log_debug(prog, |
| 1198 |
"%s: couldn't parse .gnu_debugaltlink; ignoring debug info", |
| 1199 |
file->path); |
| 1200 |
return NULL; |
| 1201 |
} |
| 1202 |
const void *build_id = nul + 1; |
| 1203 |
size_t build_id_len = debugaltlink + data->d_size - (nul + 1); |
| 1204 |
_cleanup_free_ char *build_id_str = ahexlify(build_id, build_id_len); |
| 1205 |
if (!build_id_str) |
| 1206 |
return &drgn_enomem; |
| 1207 |
drgn_log_debug(prog, "%s has gnu_debugaltlink %s build ID %s", |
| 1208 |
file->path, debugaltlink, build_id_str); |
| 1209 |
|
| 1210 |
struct drgn_module_wanted_supplementary_file *wanted = |
| 1211 |
malloc(sizeof(*wanted)); |
| 1212 |
if (!wanted) |
| 1213 |
return &drgn_enomem; |
| 1214 |
*wanted = (struct drgn_module_wanted_supplementary_file){ |
| 1215 |
.file = file, |
| 1216 |
.supplementary_path = debugaltlink, |
| 1217 |
.checksum = build_id, |
| 1218 |
.checksum_len = build_id_len, |
| 1219 |
.checksum_str = no_cleanup_ptr(build_id_str), |
| 1220 |
.generation = ++prog->dbinfo.supplementary_file_generation, |
| 1221 |
}; |
| 1222 |
drgn_module_clear_wanted_supplementary_debug_file(module); |
| 1223 |
module->wanted_supplementary_debug_file = wanted; |
| 1224 |
module->debug_file_status = DRGN_MODULE_FILE_WANT_SUPPLEMENTARY; |
| 1225 |
return NULL; |
| 1226 |
} |
| 1227 |
|
| 1228 |
static struct drgn_error * |
| 1229 |
drgn_module_copy_section_addresses(struct drgn_module *module, |
| 1230 |
struct drgn_elf_file *file) |
| 1231 |
{ |
| 1232 |
if (!file->is_relocatable) |
| 1233 |
return NULL; |
| 1234 |
|
| 1235 |
size_t shdrnum; |
| 1236 |
if (elf_getshdrnum(file->elf, &shdrnum)) |
| 1237 |
return drgn_error_libelf(); |
| 1238 |
|
| 1239 |
file->sections_with_address = drgn_bitmap_create(shdrnum); |
| 1240 |
if (!file->sections_with_address) |
| 1241 |
return &drgn_enomem; |
| 1242 |
|
| 1243 |
if (drgn_module_section_address_map_empty(&module->section_addresses)) |
| 1244 |
return NULL; |
| 1245 |
|
| 1246 |
size_t shstrndx; |
| 1247 |
if (elf_getshdrstrndx(file->elf, &shstrndx)) |
| 1248 |
return drgn_error_libelf(); |
| 1249 |
|
| 1250 |
Elf_Scn *scn = NULL; |
| 1251 |
while ((scn = elf_nextscn(file->elf, scn))) { |
| 1252 |
GElf_Shdr *shdr, shdr_mem; |
| 1253 |
shdr = gelf_getshdr(scn, &shdr_mem); |
| 1254 |
if (!shdr) |
| 1255 |
return drgn_error_libelf(); |
| 1256 |
|
| 1257 |
char *scnname = elf_strptr(file->elf, shstrndx, shdr->sh_name); |
| 1258 |
if (!scnname) |
| 1259 |
return drgn_error_libelf(); |
| 1260 |
|
| 1261 |
struct drgn_module_section_address_map_iterator it = |
| 1262 |
drgn_module_section_address_map_search(&module->section_addresses, |
| 1263 |
&scnname); |
| 1264 |
if (!it.entry) |
| 1265 |
continue; |
| 1266 |
|
| 1267 |
shdr->sh_addr = it.entry->value; |
| 1268 |
if (!gelf_update_shdr(scn, shdr)) |
| 1269 |
return drgn_error_libelf(); |
| 1270 |
drgn_bitmap_set_bit(file->sections_with_address, |
| 1271 |
elf_ndxscn(scn)); |
| 1272 |
} |
| 1273 |
return NULL; |
| 1274 |
} |
| 1275 |
|
| 1276 |
static bool elf_main_bias(struct drgn_program *prog, Elf *elf, uint64_t *ret) |
| 1277 |
{ |
| 1278 |
GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr(elf, &ehdr_mem); |
| 1279 |
if (!ehdr) { |
| 1280 |
drgn_log_debug(prog, "gelf_getehdr: %s", elf_errmsg(-1)); |
| 1281 |
return false; |
| 1282 |
} |
| 1283 |
|
| 1284 |
size_t phnum; |
| 1285 |
if (elf_getphdrnum(elf, &phnum) != 0) { |
| 1286 |
drgn_log_debug(prog, "elf_getphdrnum: %s", elf_errmsg(-1)); |
| 1287 |
return false; |
| 1288 |
} |
| 1289 |
|
| 1290 |
uint64_t phdr_vaddr; |
| 1291 |
bool have_phdr_vaddr = false; |
| 1292 |
for (size_t i = 0; i < phnum; i++) { |
| 1293 |
GElf_Phdr phdr_mem, *phdr = gelf_getphdr(elf, i, &phdr_mem); |
| 1294 |
if (!phdr) { |
| 1295 |
drgn_log_debug(prog, "gelf_getphdr: %s", |
| 1296 |
elf_errmsg(-1)); |
| 1297 |
return false; |
| 1298 |
} |
| 1299 |
if (phdr->p_type == PT_LOAD && |
| 1300 |
phdr->p_offset <= ehdr->e_phoff && |
| 1301 |
ehdr->e_phoff < phdr->p_offset + phdr->p_filesz) { |
| 1302 |
phdr_vaddr = ehdr->e_phoff - phdr->p_offset + phdr->p_vaddr; |
| 1303 |
have_phdr_vaddr = true; |
| 1304 |
} |
| 1305 |
} |
| 1306 |
if (!have_phdr_vaddr) { |
| 1307 |
drgn_log_debug(prog, |
| 1308 |
"file does not have loadable segment containing e_phoff"); |
| 1309 |
return false; |
| 1310 |
} |
| 1311 |
*ret = prog->auxv.at_phdr - phdr_vaddr; |
| 1312 |
return true; |
| 1313 |
} |
| 1314 |
|
| 1315 |
static bool elf_dso_bias(struct drgn_program *prog, Elf *elf, |
| 1316 |
uint64_t dynamic_address, uint64_t *ret) |
| 1317 |
{ |
| 1318 |
size_t phnum; |
| 1319 |
if (elf_getphdrnum(elf, &phnum) != 0) { |
| 1320 |
drgn_log_debug(prog, "elf_getphdrnum: %s", elf_errmsg(-1)); |
| 1321 |
return false; |
| 1322 |
} |
| 1323 |
|
| 1324 |
for (size_t i = 0; i < phnum; i++) { |
| 1325 |
GElf_Phdr phdr_mem, *phdr = gelf_getphdr(elf, i, &phdr_mem); |
| 1326 |
if (!phdr) { |
| 1327 |
drgn_log_debug(prog, "gelf_getphdr: %s", |
| 1328 |
elf_errmsg(-1)); |
| 1329 |
return false; |
| 1330 |
} |
| 1331 |
if (phdr->p_type == PT_DYNAMIC) { |
| 1332 |
*ret = dynamic_address - phdr->p_vaddr; |
| 1333 |
drgn_log_debug(prog, |
| 1334 |
"got bias 0x%" PRIx64 " from PT_DYNAMIC program header", |
| 1335 |
*ret); |
| 1336 |
return true; |
| 1337 |
} |
| 1338 |
} |
| 1339 |
drgn_log_debug(prog, "file does not have PT_DYNAMIC program header"); |
| 1340 |
return false; |
| 1341 |
} |
| 1342 |
|
| 1343 |
static bool drgn_module_elf_file_bias(struct drgn_module *module, |
| 1344 |
struct drgn_elf_file *file, uint64_t *ret) |
| 1345 |
{ |
| 1346 |
struct drgn_program *prog = module->prog; |
| 1347 |
SWITCH_ENUM(module->kind) { |
| 1348 |
case DRGN_MODULE_MAIN: |
| 1349 |
if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) { |
| 1350 |
*ret = prog->vmcoreinfo.kaslr_offset; |
| 1351 |
drgn_log_debug(prog, |
| 1352 |
"got bias 0x%" PRIx64 " from VMCOREINFO", |
| 1353 |
*ret); |
| 1354 |
return true; |
| 1355 |
} else { |
| 1356 |
return elf_main_bias(prog, file->elf, ret); |
| 1357 |
} |
| 1358 |
case DRGN_MODULE_SHARED_LIBRARY: |
| 1359 |
case DRGN_MODULE_VDSO: |
| 1360 |
return elf_dso_bias(prog, file->elf, module->info, ret); |
| 1361 |
case DRGN_MODULE_EXTRA: { |
| 1362 |
size_t num_address_ranges; |
| 1363 |
if (drgn_module_num_address_ranges(module, &num_address_ranges) |
| 1364 |
&& num_address_ranges == 1) { |
| 1365 |
uint64_t start, end; |
| 1366 |
drgn_module_address_range(module, 0, &start, &end); |
| 1367 |
uint64_t elf_start, elf_end; |
| 1368 |
if (!drgn_elf_file_address_range(file, &elf_start, |
| 1369 |
&elf_end)) |
| 1370 |
return false; |
| 1371 |
if (elf_start < elf_end) { |
| 1372 |
*ret = start - elf_start; |
| 1373 |
drgn_log_debug(prog, |
| 1374 |
"got bias 0x%" PRIx64 " from ELF start address", |
| 1375 |
*ret); |
| 1376 |
return true; |
| 1377 |
} |
| 1378 |
} |
| 1379 |
fallthrough; |
| 1380 |
} |
| 1381 |
case DRGN_MODULE_RELOCATABLE: |
| 1382 |
default: |
| 1383 |
*ret = 0; |
| 1384 |
return true; |
| 1385 |
} |
| 1386 |
} |
| 1387 |
|
| 1388 |
static bool |
| 1389 |
drgn_module_should_set_address_range_from_elf_file(struct drgn_module *module) |
| 1390 |
{ |
| 1391 |
if (module->address_ranges) |
| 1392 |
return false; |
| 1393 |
|
| 1394 |
SWITCH_ENUM(module->kind) { |
| 1395 |
case DRGN_MODULE_MAIN: |
| 1396 |
case DRGN_MODULE_SHARED_LIBRARY: |
| 1397 |
case DRGN_MODULE_VDSO: |
| 1398 |
return true; |
| 1399 |
case DRGN_MODULE_RELOCATABLE: |
| 1400 |
case DRGN_MODULE_EXTRA: |
| 1401 |
default: |
| 1402 |
return false; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
|
| 1406 |
// Takes ownership of file unless it is already owned by module. |
| 1407 |
static struct drgn_error * |
| 1408 |
drgn_module_maybe_use_elf_file(struct drgn_module *module, |
| 1409 |
struct drgn_elf_file *file, |
| 1410 |
bool is_gnu_debugaltlink_file) |
| 1411 |
{ |
| 1412 |
struct drgn_error *err; |
| 1413 |
struct drgn_program *prog = module->prog; |
| 1414 |
struct drgn_elf_file *gnu_debugaltlink_file = NULL; |
| 1415 |
|
| 1416 |
bool use_loaded, has_dwarf, use_debug; |
| 1417 |
if (is_gnu_debugaltlink_file) { |
| 1418 |
assert(module->debug_file_status |
| 1419 |
== DRGN_MODULE_FILE_WANT_SUPPLEMENTARY); |
| 1420 |
gnu_debugaltlink_file = file; |
| 1421 |
file = module->wanted_supplementary_debug_file->file; |
| 1422 |
use_loaded = false; |
| 1423 |
has_dwarf = use_debug = true; |
| 1424 |
} else { |
| 1425 |
// We should only be here if we want a file. |
| 1426 |
assert(drgn_module_wants_file(module)); |
| 1427 |
use_loaded = module->loaded_file_status == DRGN_MODULE_FILE_WANT |
| 1428 |
&& file->is_loadable; |
| 1429 |
has_dwarf = drgn_elf_file_has_dwarf(file); |
| 1430 |
use_debug = drgn_module_wants_debug_file(module) && has_dwarf; |
| 1431 |
} |
| 1432 |
|
| 1433 |
_cleanup_free_ void *build_id_buf = NULL; |
| 1434 |
|
| 1435 |
if (!is_gnu_debugaltlink_file |
| 1436 |
&& use_debug && file->scns[DRGN_SCN_GNU_DEBUGALTLINK]) { |
| 1437 |
// If we're trying to reuse a debug file that wants a |
| 1438 |
// supplementary file, then don't reset it, otherwise we'll free |
| 1439 |
// the file that we're trying to reuse. |
| 1440 |
if (!module->wanted_supplementary_debug_file |
| 1441 |
|| module->wanted_supplementary_debug_file->file != file) { |
| 1442 |
err = drgn_module_set_wanted_gnu_debugaltlink(module, file); |
| 1443 |
if (err) |
| 1444 |
goto unused; |
| 1445 |
} |
| 1446 |
if (!use_loaded && module->wanted_supplementary_debug_file |
| 1447 |
&& module->wanted_supplementary_debug_file->file == file) |
| 1448 |
return NULL; |
| 1449 |
use_debug = false; |
| 1450 |
} |
| 1451 |
|
| 1452 |
if (!use_loaded && !use_debug) { |
| 1453 |
if (file->is_loadable) { |
| 1454 |
drgn_log_debug(prog, |
| 1455 |
"%s is loadable, but don't want loaded file; ignoring", |
| 1456 |
file->path); |
| 1457 |
} else if (has_dwarf) { |
| 1458 |
drgn_log_debug(prog, |
| 1459 |
"%s has debug info, but don't want debug info; ignoring", |
| 1460 |
file->path); |
| 1461 |
} else { |
| 1462 |
drgn_log_debug(prog, |
| 1463 |
"%s is not loadable and no debug info; ignoring", |
| 1464 |
file->path); |
| 1465 |
} |
| 1466 |
err = NULL; |
| 1467 |
goto unused; |
| 1468 |
} |
| 1469 |
|
| 1470 |
// Get everything that might fail before we commit to using the file. |
| 1471 |
const void *elf_build_id; |
| 1472 |
ssize_t elf_build_id_len = 0; |
| 1473 |
if (module->build_id_len == 0) { |
| 1474 |
elf_build_id_len = drgn_elf_gnu_build_id(file->elf, |
| 1475 |
&elf_build_id); |
| 1476 |
if (elf_build_id_len < 0) { |
| 1477 |
drgn_log_debug(prog, "%s: %s", file->path, |
| 1478 |
elf_errmsg(-1)); |
| 1479 |
err = NULL; |
| 1480 |
goto unused; |
| 1481 |
} |
| 1482 |
if (elf_build_id_len > 0) { |
| 1483 |
build_id_buf = |
| 1484 |
drgn_module_alloc_build_id(elf_build_id_len); |
| 1485 |
if (!build_id_buf) { |
| 1486 |
err = &drgn_enomem; |
| 1487 |
goto unused; |
| 1488 |
} |
| 1489 |
} |
| 1490 |
} |
| 1491 |
|
| 1492 |
if (file != module->loaded_file && file != module->debug_file) { |
| 1493 |
err = drgn_module_copy_section_addresses(module, file); |
| 1494 |
if (err) { |
| 1495 |
if (!drgn_error_is_fatal(err)) { |
| 1496 |
drgn_error_log_debug(prog, err, "%s: ", |
| 1497 |
file->path); |
| 1498 |
drgn_error_destroy(err); |
| 1499 |
err = NULL; |
| 1500 |
} |
| 1501 |
goto unused; |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
uint64_t bias; |
| 1506 |
if (!drgn_module_elf_file_bias(module, file, &bias)) { |
| 1507 |
err = NULL; |
| 1508 |
goto unused; |
| 1509 |
} |
| 1510 |
uint64_t elf_start = 0, elf_end = 0; |
| 1511 |
if (drgn_module_should_set_address_range_from_elf_file(module)) { |
| 1512 |
if (!drgn_elf_file_address_range(file, &elf_start, &elf_end)) { |
| 1513 |
drgn_log_debug(prog, "%s: %s", file->path, |
| 1514 |
elf_errmsg(-1)); |
| 1515 |
err = NULL; |
| 1516 |
goto unused; |
| 1517 |
} |
| 1518 |
elf_start += bias; |
| 1519 |
elf_end += bias; |
| 1520 |
if (elf_start >= elf_end) { |
| 1521 |
drgn_log_debug(prog, "%s: address range is invalid", |
| 1522 |
file->path); |
| 1523 |
} |
| 1524 |
} |
| 1525 |
|
| 1526 |
// At this point, we've committed to using the file. Nothing after this |
| 1527 |
// is allowed to fail. |
| 1528 |
|
| 1529 |
if (use_loaded && use_debug) { |
| 1530 |
drgn_log_info(prog, |
| 1531 |
"%s: using loadable file with debug info %s", |
| 1532 |
module->name, file->path); |
| 1533 |
} else if (use_loaded) { |
| 1534 |
drgn_log_info(prog, "%s: using loadable file %s", module->name, |
| 1535 |
file->path); |
| 1536 |
} else if (is_gnu_debugaltlink_file) { |
| 1537 |
drgn_log_info(prog, |
| 1538 |
"%s: using debug info file %s with supplementary file %s", |
| 1539 |
module->name, file->path, gnu_debugaltlink_file->path); |
| 1540 |
} else { |
| 1541 |
drgn_log_info(prog, "%s: using debug info file %s", |
| 1542 |
module->name, file->path); |
| 1543 |
} |
| 1544 |
|
| 1545 |
// If we got a build ID or address range earlier, install them. |
| 1546 |
if (elf_build_id_len > 0) { |
| 1547 |
drgn_module_set_build_id_impl(module, elf_build_id, |
| 1548 |
elf_build_id_len, |
| 1549 |
no_cleanup_ptr(build_id_buf)); |
| 1550 |
drgn_log_debug(prog, "%s: set build ID %s from file", |
| 1551 |
module->name, module->build_id_str); |
| 1552 |
} |
| 1553 |
if (elf_start < elf_end) { |
| 1554 |
drgn_log_debug(prog, |
| 1555 |
"%s: set address range 0x%" PRIx64 |
| 1556 |
"-0x%" PRIx64 " from file", module->name, |
| 1557 |
elf_start, elf_end); |
| 1558 |
err = drgn_module_set_address_range(module, elf_start, elf_end); |
| 1559 |
// This can only fail if the address range is invalid, which we |
| 1560 |
// just checked for. |
| 1561 |
assert(!err); |
| 1562 |
} |
| 1563 |
|
| 1564 |
if (use_loaded) { |
| 1565 |
module->loaded_file = file; |
| 1566 |
module->loaded_file_bias = bias; |
| 1567 |
module->loaded_file_status = DRGN_MODULE_FILE_HAVE; |
| 1568 |
module->elf_symtab_pending_files |= |
| 1569 |
DRGN_MODULE_FILE_MASK_LOADED; |
| 1570 |
} |
| 1571 |
if (use_debug) { |
| 1572 |
module->debug_file = file; |
| 1573 |
module->debug_file_bias = bias; |
| 1574 |
module->supplementary_debug_file = gnu_debugaltlink_file; |
| 1575 |
drgn_module_clear_wanted_supplementary_debug_file(module); |
| 1576 |
module->debug_file_status = DRGN_MODULE_FILE_HAVE; |
| 1577 |
module->pending_indexing_next = |
| 1578 |
prog->dbinfo.modules_pending_indexing; |
| 1579 |
prog->dbinfo.modules_pending_indexing = module; |
| 1580 |
prog->tried_main_language = false; |
| 1581 |
module->elf_symtab_pending_files |= |
| 1582 |
DRGN_MODULE_FILE_MASK_DEBUG; |
| 1583 |
// In case primitive types have been created by drgn before |
| 1584 |
// loading the main debug file (e.g. by reading VMCOREINFO), |
| 1585 |
// clear this cache so we can look them up from the debug info. |
| 1586 |
// See https://github.com/osandov/drgn/issues/569 for more |
| 1587 |
// details. |
| 1588 |
if (module->kind == DRGN_MODULE_MAIN) |
| 1589 |
memset(module->prog->primitive_types, 0, |
| 1590 |
sizeof(module->prog->primitive_types)); |
| 1591 |
} |
| 1592 |
if (!prog->has_platform) { |
| 1593 |
drgn_log_debug(prog, "setting program platform from %s", |
| 1594 |
file->path); |
| 1595 |
drgn_program_set_platform(prog, &file->platform); |
| 1596 |
} |
| 1597 |
return NULL; |
| 1598 |
|
| 1599 |
unused: |
| 1600 |
drgn_elf_file_destroy(gnu_debugaltlink_file); |
| 1601 |
if (module->wanted_supplementary_debug_file |
| 1602 |
&& file == module->wanted_supplementary_debug_file->file) { |
| 1603 |
module->wanted_supplementary_debug_file->file = NULL; |
| 1604 |
drgn_module_clear_wanted_supplementary_debug_file(module); |
| 1605 |
module->debug_file_status = DRGN_MODULE_FILE_WANT; |
| 1606 |
} |
| 1607 |
if (file != module->loaded_file && file != module->debug_file) |
| 1608 |
drgn_elf_file_destroy(file); |
| 1609 |
return err; |
| 1610 |
} |
| 1611 |
|
| 1612 |
// Always takes ownership of fd_. Attempts to resolve the real path of path. |
| 1613 |
static struct drgn_error * |
| 1614 |
drgn_module_try_file_internal(struct drgn_module *module, const char *path, |
| 1615 |
int fd_, bool check_build_id, |
| 1616 |
const uint32_t *expected_crc) |
| 1617 |
{ |
| 1618 |
struct drgn_error *err; |
| 1619 |
struct drgn_program *prog = module->prog; |
| 1620 |
|
| 1621 |
_cleanup_close_ int fd = fd_; |
| 1622 |
if (fd >= 0) { |
| 1623 |
if (path) { |
| 1624 |
drgn_log_debug(prog, "%s: trying %s with fd %d", |
| 1625 |
module->name, path, fd); |
| 1626 |
} else { |
| 1627 |
drgn_log_debug(prog, "%s: trying fd %d", module->name, |
| 1628 |
fd); |
| 1629 |
} |
| 1630 |
} else { |
| 1631 |
fd = open(path, O_RDONLY); |
| 1632 |
if (fd < 0) { |
| 1633 |
drgn_log_debug(prog, "%s: %m", path); |
| 1634 |
return NULL; |
| 1635 |
} |
| 1636 |
drgn_log_debug(prog, "%s: trying %s", module->name, path); |
| 1637 |
} |
| 1638 |
|
| 1639 |
_cleanup_free_ char *canonical_path = fd_canonical_path(fd, path); |
| 1640 |
if (!canonical_path) |
| 1641 |
return &drgn_enomem; |
| 1642 |
if (drgn_log_is_enabled(prog, DRGN_LOG_DEBUG) |
| 1643 |
&& (!path || strcmp(path, canonical_path) != 0)) |
| 1644 |
drgn_log_debug(prog, "canonical path is %s", canonical_path); |
| 1645 |
path = canonical_path; |
| 1646 |
|
| 1647 |
_cleanup_elf_end_ Elf *elf = dwelf_elf_begin(fd); |
| 1648 |
if (!elf) { |
| 1649 |
drgn_log_debug(prog, "%s: %s", path, elf_errmsg(-1)); |
| 1650 |
return NULL; |
| 1651 |
} |
| 1652 |
if (elf_kind(elf) != ELF_K_ELF) { |
| 1653 |
drgn_log_debug(prog, "%s: not an ELF file", path); |
| 1654 |
return NULL; |
| 1655 |
} |
| 1656 |
|
| 1657 |
// This code assumes that DRGN_SUPPLEMENTARY_FILE_GNU_DEBUGALTLINK is |
| 1658 |
// the only kind of supplementary file, which is currently true. |
| 1659 |
bool log_build_id = check_build_id |
| 1660 |
|| drgn_log_is_enabled(prog, DRGN_LOG_DEBUG); |
| 1661 |
const void *elf_build_id; |
| 1662 |
ssize_t elf_build_id_len; |
| 1663 |
if (module->debug_file_status == DRGN_MODULE_FILE_WANT_SUPPLEMENTARY |
| 1664 |
|| (log_build_id && module->build_id_len > 0)) { |
| 1665 |
elf_build_id_len = drgn_elf_gnu_build_id(elf, &elf_build_id); |
| 1666 |
if (elf_build_id_len < 0) { |
| 1667 |
drgn_log_debug(prog, "%s: %s%s", path, elf_errmsg(-1), |
| 1668 |
check_build_id ? "" : "; ignoring build ID"); |
| 1669 |
} |
| 1670 |
} |
| 1671 |
|
| 1672 |
bool is_gnu_debugaltlink_file = false; |
| 1673 |
if (module->debug_file_status == DRGN_MODULE_FILE_WANT_SUPPLEMENTARY |
| 1674 |
&& elf_build_id_len >= 0 |
| 1675 |
&& elf_build_id_len |
| 1676 |
== module->wanted_supplementary_debug_file->checksum_len |
| 1677 |
&& memcmp(elf_build_id, |
| 1678 |
module->wanted_supplementary_debug_file->checksum, |
| 1679 |
elf_build_id_len) == 0) { |
| 1680 |
drgn_log_debug(prog, "%s: %s build ID matches gnu_debugaltlink", |
| 1681 |
module->name, path); |
| 1682 |
is_gnu_debugaltlink_file = true; |
| 1683 |
} else if (log_build_id && module->build_id_len > 0) { |
| 1684 |
if (elf_build_id_len < 0) { |
| 1685 |
if (check_build_id) |
| 1686 |
return NULL; |
| 1687 |
} else if (elf_build_id_len == module->build_id_len |
| 1688 |
&& memcmp(elf_build_id, module->build_id, |
| 1689 |
elf_build_id_len) == 0) { |
| 1690 |
drgn_log_debug(prog, "%s: %s build ID matches", |
| 1691 |
module->name, path); |
| 1692 |
} else { |
| 1693 |
if (elf_build_id_len == 0) { |
| 1694 |
drgn_log_debug(prog, |
| 1695 |
"%s: %s is missing build ID%s", |
| 1696 |
module->name, path, |
| 1697 |
check_build_id ? "" : "; forcing"); |
| 1698 |
} else { |
| 1699 |
drgn_log_debug(prog, |
| 1700 |
"%s: %s build ID does not match%s", |
| 1701 |
module->name, path, |
| 1702 |
check_build_id ? "" : "; forcing"); |
| 1703 |
} |
| 1704 |
if (check_build_id) |
| 1705 |
return NULL; |
| 1706 |
} |
| 1707 |
} |
| 1708 |
if (expected_crc) { |
| 1709 |
size_t size; |
| 1710 |
const void *rawfile = elf_rawfile(elf, &size); |
| 1711 |
if (!rawfile) { |
| 1712 |
drgn_log_debug(prog, "%s: %s", path, elf_errmsg(-1)); |
| 1713 |
return NULL; |
| 1714 |
} |
| 1715 |
uint32_t crc = ~crc32_update(-1, rawfile, size); |
| 1716 |
if (crc != *expected_crc) { |
| 1717 |
drgn_log_debug(prog, |
| 1718 |
"%s: %s CRC 0x%08" PRIx32 " does not match", |
| 1719 |
module->name, path, crc); |
| 1720 |
return NULL; |
| 1721 |
} |
| 1722 |
drgn_log_debug(prog, "%s: %s CRC matches", module->name, path); |
| 1723 |
} |
| 1724 |
|
| 1725 |
struct drgn_elf_file *file; |
| 1726 |
err = drgn_elf_file_create(module, path, fd, NULL, elf, &file); |
| 1727 |
if (err) { |
| 1728 |
if (!drgn_error_is_fatal(err)) { |
| 1729 |
drgn_error_log_debug(prog, err, ""); |
| 1730 |
drgn_error_destroy(err); |
| 1731 |
err = NULL; |
| 1732 |
} |
| 1733 |
return err; |
| 1734 |
} |
| 1735 |
// fd and elf are owned by the drgn_elf_file now. |
| 1736 |
fd = -1; |
| 1737 |
elf = NULL; |
| 1738 |
return drgn_module_maybe_use_elf_file(module, file, |
| 1739 |
is_gnu_debugaltlink_file); |
| 1740 |
} |
| 1741 |
|
| 1742 |
// Arbitrary limit on the number of bytes we'll allocate and read from the |
| 1743 |
// program's memory at once when finding modules/debug info. |
| 1744 |
static const uint64_t MAX_MEMORY_READ_FOR_DEBUG_INFO = UINT64_C(1048576); |
| 1745 |
|
| 1746 |
#define drgn_module_try_files_log(module, how_format, ...) \ |
| 1747 |
({ \ |
| 1748 |
struct drgn_module *_module = (module); \ |
| 1749 |
bool _want_loaded = _module->loaded_file_status == DRGN_MODULE_FILE_WANT;\ |
| 1750 |
bool _want_debug = _module->debug_file_status == DRGN_MODULE_FILE_WANT; \ |
| 1751 |
bool _want_supplementary_debug = _module->debug_file_status \ |
| 1752 |
== DRGN_MODULE_FILE_WANT_SUPPLEMENTARY;\ |
| 1753 |
drgn_log_debug(_module->prog, \ |
| 1754 |
"%s (%s%s): " how_format " %s%s%s file%s", _module->name,\ |
| 1755 |
_module->build_id_str ? "build ID " : "no build ID", \ |
| 1756 |
_module->build_id_str ?: "", \ |
| 1757 |
## __VA_ARGS__, \ |
| 1758 |
_want_loaded ? "loaded" : "", \ |
| 1759 |
_want_loaded && (_want_debug || _want_supplementary_debug)\ |
| 1760 |
? " and " : "", \ |
| 1761 |
_want_debug ? "debug" \ |
| 1762 |
: _want_supplementary_debug ? "supplementary debug" : "",\ |
| 1763 |
_want_loaded && (_want_debug || _want_supplementary_debug)\ |
| 1764 |
? "s" : ""); \ |
| 1765 |
}) |
| 1766 |
|
| 1767 |
static struct drgn_error * |
| 1768 |
drgn_module_try_vdso_in_core(struct drgn_module *module, |
| 1769 |
const struct drgn_debug_info_options *options) |
| 1770 |
{ |
| 1771 |
struct drgn_error *err; |
| 1772 |
struct drgn_program *prog = module->prog; |
| 1773 |
|
| 1774 |
if (!options->try_embedded_vdso) |
| 1775 |
return NULL; |
| 1776 |
|
| 1777 |
// The Linux kernel has included the entire vDSO in core dumps since |
| 1778 |
// Linux kernel commit f47aef55d9a1 ("[PATCH] i386 vDSO: use |
| 1779 |
// VM_ALWAYSDUMP") (in v2.6.20). Try to read it from program memory. |
| 1780 |
|
| 1781 |
// The vDSO in memory is always stripped. |
| 1782 |
if (module->loaded_file_status != DRGN_MODULE_FILE_WANT) |
| 1783 |
return NULL; |
| 1784 |
|
| 1785 |
size_t num_address_ranges; |
| 1786 |
if (!drgn_module_num_address_ranges(module, &num_address_ranges)) { |
| 1787 |
drgn_log_debug(prog, |
| 1788 |
"vDSO address range is not known; " |
| 1789 |
"can't read from program"); |
| 1790 |
return NULL; |
| 1791 |
} |
| 1792 |
if (num_address_ranges != 1) { |
| 1793 |
drgn_log_debug(prog, "vDSO has %s; can't read from program", |
| 1794 |
num_address_ranges |
| 1795 |
? "multiple address ranges" |
| 1796 |
: "empty address range"); |
| 1797 |
return NULL; |
| 1798 |
} |
| 1799 |
uint64_t start, end; |
| 1800 |
drgn_module_address_range(module, 0, &start, &end); |
| 1801 |
uint64_t size = end - start; |
| 1802 |
if (size > MAX_MEMORY_READ_FOR_DEBUG_INFO) { |
| 1803 |
drgn_log_debug(prog, |
| 1804 |
"vDSO is unreasonably large (%" PRIu64 " bytes); " |
| 1805 |
"not reading from program", |
| 1806 |
size); |
| 1807 |
return NULL; |
| 1808 |
} |
| 1809 |
|
| 1810 |
_cleanup_free_ char *image = malloc(size); |
| 1811 |
if (!image) |
| 1812 |
return &drgn_enomem; |
| 1813 |
err = drgn_program_read_memory(prog, image, start, size, false); |
| 1814 |
if (err) { |
| 1815 |
if (!drgn_error_is_fatal(err)) { |
| 1816 |
drgn_error_log_debug(prog, err, "couldn't read vDSO: "); |
| 1817 |
drgn_error_destroy(err); |
| 1818 |
err = NULL; |
| 1819 |
} |
| 1820 |
return err; |
| 1821 |
} |
| 1822 |
|
| 1823 |
_cleanup_elf_end_ Elf *elf = elf_memory(image, size); |
| 1824 |
if (!elf) { |
| 1825 |
drgn_log_debug(prog, "couldn't read vDSO: %s", elf_errmsg(-1)); |
| 1826 |
return NULL; |
| 1827 |
} |
| 1828 |
struct drgn_elf_file *file; |
| 1829 |
err = drgn_elf_file_create(module, "[vdso]", -1, image, elf, &file); |
| 1830 |
if (err) { |
| 1831 |
if (!drgn_error_is_fatal(err)) { |
| 1832 |
drgn_error_log_debug(prog, err, ""); |
| 1833 |
drgn_error_destroy(err); |
| 1834 |
err = NULL; |
| 1835 |
} |
| 1836 |
return err; |
| 1837 |
} |
| 1838 |
// image and elf are owned by the drgn_elf_file now. |
| 1839 |
image = NULL; |
| 1840 |
elf = NULL; |
| 1841 |
|
| 1842 |
drgn_log_debug(prog, "trying vDSO in %s", |
| 1843 |
(module->prog->flags & DRGN_PROGRAM_IS_LIVE) |
| 1844 |
? "memory" : "core"); |
| 1845 |
return drgn_module_maybe_use_elf_file(module, file, false); |
| 1846 |
} |
| 1847 |
|
| 1848 |
static void |
| 1849 |
drgn_module_try_supplementary_debug_file_log(struct drgn_module *module, |
| 1850 |
const char *how) |
| 1851 |
{ |
| 1852 |
const char *debug_file_path; |
| 1853 |
const char *debugaltlink_path; |
| 1854 |
if (drgn_module_wanted_supplementary_debug_file(module, |
| 1855 |
&debug_file_path, |
| 1856 |
&debugaltlink_path, |
| 1857 |
NULL, NULL) |
| 1858 |
!= DRGN_SUPPLEMENTARY_FILE_GNU_DEBUGALTLINK) |
| 1859 |
return; |
| 1860 |
const char *debugaltlink_build_id_str = |
| 1861 |
module->wanted_supplementary_debug_file->checksum_str; |
| 1862 |
drgn_log_debug(module->prog, |
| 1863 |
"%s: %s gnu_debugaltlink %s build ID %s in file %s", |
| 1864 |
module->name, how, debugaltlink_path, |
| 1865 |
debugaltlink_build_id_str, debug_file_path); |
| 1866 |
} |
| 1867 |
|
| 1868 |
static struct drgn_error * |
| 1869 |
drgn_module_try_standard_supplementary_files(struct drgn_module *module, |
| 1870 |
const struct drgn_debug_info_options *options) |
| 1871 |
{ |
| 1872 |
struct drgn_error *err; |
| 1873 |
|
| 1874 |
if (!options->try_supplementary) |
| 1875 |
return NULL; |
| 1876 |
|
| 1877 |
const char *debug_file_path; |
| 1878 |
const char *debugaltlink_path; |
| 1879 |
if (drgn_module_wanted_supplementary_debug_file(module, |
| 1880 |
&debug_file_path, |
| 1881 |
&debugaltlink_path, |
| 1882 |
NULL, NULL) |
| 1883 |
!= DRGN_SUPPLEMENTARY_FILE_GNU_DEBUGALTLINK) |
| 1884 |
return NULL; |
| 1885 |
|
| 1886 |
drgn_module_try_supplementary_debug_file_log(module, |
| 1887 |
"trying standard paths for"); |
| 1888 |
|
| 1889 |
STRING_BUILDER(sb); |
| 1890 |
const char *slash; |
| 1891 |
if (debugaltlink_path[0] == '/' |
| 1892 |
|| !(slash = strrchr(debug_file_path, '/'))) { |
| 1893 |
// debugaltlink is absolute, or the debug file doesn't have a |
| 1894 |
// directory component and is therefore in the current working |
| 1895 |
// directory. Try debugaltlink directly. |
| 1896 |
err = drgn_module_try_file_internal(module, debugaltlink_path, |
| 1897 |
-1, true, NULL); |
| 1898 |
} else { |
| 1899 |
// Try $(dirname $path)/$debugaltlink. |
| 1900 |
if (!string_builder_appendn(&sb, debug_file_path, |
| 1901 |
slash + 1 - debug_file_path) |
| 1902 |
|| !string_builder_append(&sb, debugaltlink_path) |
| 1903 |
|| !string_builder_null_terminate(&sb)) |
| 1904 |
return &drgn_enomem; |
| 1905 |
err = drgn_module_try_file_internal(module, sb.str, -1, true, |
| 1906 |
NULL); |
| 1907 |
} |
| 1908 |
if (err |
| 1909 |
|| module->debug_file_status != DRGN_MODULE_FILE_WANT_SUPPLEMENTARY) |
| 1910 |
return err; |
| 1911 |
|
| 1912 |
// All of the Linux distributions that use gnu_debugaltlink that I'm |
| 1913 |
// aware of (Debian, Fedora, SUSE, and their derivatives) put |
| 1914 |
// gnu_debugaltlink files in a ".dwz" subdirectory under the debug |
| 1915 |
// directory (e.g., "/usr/lib/debug/.dwz"). Try the path starting with |
| 1916 |
// the ".dwz" directory under all of the configured debug directories. |
| 1917 |
// This can help in a couple of cases: |
| 1918 |
// |
| 1919 |
// 1. When the gnu_debugaltlink path is absolute (which is the case on |
| 1920 |
// Debian and its derivatives as of Debian 12/Ubuntu 23.10) and the |
| 1921 |
// debug directory has been copied to a different path. See |
| 1922 |
// https://bugs.launchpad.net/ubuntu/+source/gdb/+bug/1818918. |
| 1923 |
// 2. When the gnu_debugaltlink path is relative (which is the case on |
| 1924 |
// Fedora, SUSE, and their derivatives) and the debug file was found |
| 1925 |
// outside of the debug directory. |
| 1926 |
const char *dwz = strstr(debugaltlink_path, "/.dwz/"); |
| 1927 |
if (dwz) { |
| 1928 |
for (size_t i = 0; options->directories[i]; i++) { |
| 1929 |
const char *debug_dir = options->directories[i]; |
| 1930 |
|
| 1931 |
sb.len = 0; |
| 1932 |
if (!string_builder_append(&sb, debug_dir) |
| 1933 |
|| !string_builder_append(&sb, dwz) |
| 1934 |
|| !string_builder_null_terminate(&sb)) |
| 1935 |
return &drgn_enomem; |
| 1936 |
|
| 1937 |
// Don't bother trying debugaltlink directly again. |
| 1938 |
if (strcmp(sb.str, debugaltlink_path) == 0) |
| 1939 |
continue; |
| 1940 |
|
| 1941 |
err = drgn_module_try_file_internal(module, sb.str, -1, |
| 1942 |
true, NULL); |
| 1943 |
if (err |
| 1944 |
|| module->debug_file_status |
| 1945 |
!= DRGN_MODULE_FILE_WANT_SUPPLEMENTARY) |
| 1946 |
return err; |
| 1947 |
} |
| 1948 |
} |
| 1949 |
return NULL; |
| 1950 |
} |
| 1951 |
|
| 1952 |
static bool |
| 1953 |
drgn_module_wanted_supplementary_debug_file_is_new(struct drgn_module *module, |
| 1954 |
uint64_t orig_supplementary_file_generation) |
| 1955 |
{ |
| 1956 |
return module->wanted_supplementary_debug_file |
| 1957 |
&& module->wanted_supplementary_debug_file->generation |
| 1958 |
> orig_supplementary_file_generation; |
| 1959 |
} |
| 1960 |
|
| 1961 |
struct drgn_error * |
| 1962 |
drgn_module_try_standard_file(struct drgn_module *module, |
| 1963 |
const struct drgn_debug_info_options *options, |
| 1964 |
const char *path, int fd, bool check_build_id, |
| 1965 |
const uint32_t *expected_crc) |
| 1966 |
{ |
| 1967 |
struct drgn_error *err; |
| 1968 |
uint64_t orig_supplementary_file_generation = |
| 1969 |
module->prog->dbinfo.supplementary_file_generation; |
| 1970 |
err = drgn_module_try_file_internal(module, path, fd, check_build_id, |
| 1971 |
expected_crc); |
| 1972 |
if (err) |
| 1973 |
return err; |
| 1974 |
// If the wanted supplementary debug file changed, try finding it again. |
| 1975 |
if (drgn_module_wanted_supplementary_debug_file_is_new(module, |
| 1976 |
orig_supplementary_file_generation)) { |
| 1977 |
err = drgn_module_try_standard_supplementary_files(module, |
| 1978 |
options); |
| 1979 |
if (err) |
| 1980 |
return err; |
| 1981 |
} |
| 1982 |
return NULL; |
| 1983 |
} |
| 1984 |
|
| 1985 |
// An entry in /proc/$pid/map_files. |
| 1986 |
struct drgn_map_files_segment { |
| 1987 |
uint64_t start; |
| 1988 |
uint64_t end; |
| 1989 |
}; |
| 1990 |
|
| 1991 |
DEFINE_VECTOR(drgn_map_files_segment_vector, struct drgn_map_files_segment); |
| 1992 |
|
| 1993 |
static inline int drgn_map_files_segment_compare(const void *_a, const void *_b) |
| 1994 |
{ |
| 1995 |
const struct drgn_map_files_segment *a = _a; |
| 1996 |
const struct drgn_map_files_segment *b = _b; |
| 1997 |
return (a->start > b->start) - (a->start < b->start); |
| 1998 |
} |
| 1999 |
|
| 2000 |
static void |
| 2001 |
drgn_debug_info_set_map_files_segments(struct drgn_debug_info *dbinfo, |
| 2002 |
struct drgn_map_files_segment_vector *segments, |
| 2003 |
bool sorted) |
| 2004 |
{ |
| 2005 |
free(dbinfo->map_files_segments); |
| 2006 |
drgn_map_files_segment_vector_shrink_to_fit(segments); |
| 2007 |
drgn_map_files_segment_vector_steal(segments, |
| 2008 |
&dbinfo->map_files_segments, |
| 2009 |
&dbinfo->num_map_files_segments); |
| 2010 |
// The Linux kernel always returns these entries in order, but sort it |
| 2011 |
// just in case. |
| 2012 |
if (!sorted) { |
| 2013 |
qsort(dbinfo->map_files_segments, |
| 2014 |
dbinfo->num_map_files_segments, |
| 2015 |
sizeof(dbinfo->map_files_segments[0]), |
| 2016 |
drgn_map_files_segment_compare); |
| 2017 |
} |
| 2018 |
} |
| 2019 |
|
| 2020 |
static struct drgn_error * |
| 2021 |
drgn_module_try_proc_files_for_shared_library(struct drgn_module *module, |
| 2022 |
const struct drgn_debug_info_options *options, |
| 2023 |
bool *tried) |
| 2024 |
{ |
| 2025 |
struct drgn_error *err; |
| 2026 |
struct drgn_program *prog = module->prog; |
| 2027 |
const uint64_t address = module->info; |
| 2028 |
|
| 2029 |
#define DIR_FORMAT "/proc/%ld/map_files" |
| 2030 |
#define ENTRY_FORMAT "/%" PRIx64 "-%" PRIx64 |
| 2031 |
char path[sizeof(DIR_FORMAT ENTRY_FORMAT) |
| 2032 |
- (sizeof("%ld") - 1) |
| 2033 |
+ max_decimal_length(long) |
| 2034 |
- 2 * (sizeof("%" PRIx64) - 1) |
| 2035 |
+ 2 * 16]; |
| 2036 |
int dir_len = sprintf(path, DIR_FORMAT, (long)prog->pid); |
| 2037 |
|
| 2038 |
// Check the cache first. |
| 2039 |
#define less_than_start(a, b) (*(a) < (b)->start) |
| 2040 |
size_t cache_index = binary_search_gt(prog->dbinfo.map_files_segments, |
| 2041 |
prog->dbinfo.num_map_files_segments, |
| 2042 |
&address, less_than_start); |
| 2043 |
#undef less_than_start |
| 2044 |
if (cache_index > 0 |
| 2045 |
&& address < prog->dbinfo.map_files_segments[cache_index - 1].end) { |
| 2046 |
struct drgn_map_files_segment *cache = |
| 2047 |
&prog->dbinfo.map_files_segments[cache_index - 1]; |
| 2048 |
sprintf(path + dir_len, ENTRY_FORMAT, cache->start, cache->end); |
| 2049 |
drgn_log_debug(prog, |
| 2050 |
"found %s containing dynamic section 0x%" PRIx64 " in map_files cache", |
| 2051 |
path, address); |
| 2052 |
int fd = open(path, O_RDONLY); |
| 2053 |
if (fd >= 0) { |
| 2054 |
*tried = true; |
| 2055 |
return drgn_module_try_standard_file(module, options, |
| 2056 |
path, fd, false, |
| 2057 |
NULL); |
| 2058 |
} else { |
| 2059 |
// We found a match in the cache, but we couldn't open |
| 2060 |
// it. If it doesn't exist anymore, then we need to |
| 2061 |
// rebuild the cache. If it failed for any other reason, |
| 2062 |
// ignore it like we do in the cache miss case. |
| 2063 |
bool rebuild_cache = errno == ENOENT; |
| 2064 |
drgn_log_debug(prog, "%s: %m", path); |
| 2065 |
if (!rebuild_cache) |
| 2066 |
return NULL; |
| 2067 |
} |
| 2068 |
drgn_log_debug(prog, "rebuilding map_files cache"); |
| 2069 |
path[dir_len] = '\0'; |
| 2070 |
} |
| 2071 |
#undef ENTRY_FORMAT |
| 2072 |
#undef DIR_FORMAT |
| 2073 |
|
| 2074 |
// Walk /proc/$pid/map_files, caching it while looking for a match. |
| 2075 |
_cleanup_closedir_ DIR *dir = opendir(path); |
| 2076 |
if (!dir) { |
| 2077 |
if (errno != ENOENT) |
| 2078 |
return drgn_error_create_os("opendir", errno, path); |
| 2079 |
drgn_log_debug(prog, "%s: %m", path); |
| 2080 |
return NULL; |
| 2081 |
} |
| 2082 |
VECTOR(drgn_map_files_segment_vector, segments); |
| 2083 |
bool sorted = true; |
| 2084 |
bool found = false; |
| 2085 |
struct dirent *ent; |
| 2086 |
while ((errno = 0, ent = readdir(dir))) { |
| 2087 |
struct drgn_map_files_segment segment; |
| 2088 |
if (sscanf(ent->d_name, "%" SCNx64 "-%" SCNx64, &segment.start, |
| 2089 |
&segment.end) != 2) |
| 2090 |
continue; |
| 2091 |
|
| 2092 |
if (!drgn_map_files_segment_vector_empty(&segments) |
| 2093 |
&& segment.start |
| 2094 |
< drgn_map_files_segment_vector_last(&segments)->start) |
| 2095 |
sorted = false; |
| 2096 |
if (!drgn_map_files_segment_vector_append(&segments, &segment)) |
| 2097 |
return &drgn_enomem; |
| 2098 |
|
| 2099 |
if (segment.start <= address && address < segment.end |
| 2100 |
&& !found |
| 2101 |
&& strlen(ent->d_name) + 1 < sizeof(path) - dir_len) { |
| 2102 |
found = true; |
| 2103 |
path[dir_len] = '/'; |
| 2104 |
memcpy(path + dir_len + 1, ent->d_name, |
| 2105 |
strlen(ent->d_name) + 1); |
| 2106 |
drgn_log_debug(prog, |
| 2107 |
"found %s containing dynamic section 0x%" PRIx64, |
| 2108 |
path, address); |
| 2109 |
int fd = openat(dirfd(dir), ent->d_name, O_RDONLY); |
| 2110 |
if (fd >= 0) { |
| 2111 |
*tried = true; |
| 2112 |
err = drgn_module_try_standard_file(module, |
| 2113 |
options, |
| 2114 |
path, fd, |
| 2115 |
false, |
| 2116 |
NULL); |
| 2117 |
if (err) |
| 2118 |
return err; |
| 2119 |
} else { |
| 2120 |
drgn_log_debug(prog, "%s: %m", path); |
| 2121 |
} |
| 2122 |
path[dir_len] = '\0'; |
| 2123 |
} |
| 2124 |
} |
| 2125 |
if (errno) |
| 2126 |
return drgn_error_create_os("readdir", errno, path); |
| 2127 |
|
| 2128 |
drgn_debug_info_set_map_files_segments(&prog->dbinfo, &segments, |
| 2129 |
sorted); |
| 2130 |
|
| 2131 |
if (!found) { |
| 2132 |
drgn_log_debug(prog, |
| 2133 |
"didn't find entry in %s containing dynamic section 0x%" PRIx64, |
| 2134 |
path, address); |
| 2135 |
} |
| 2136 |
return NULL; |
| 2137 |
} |
| 2138 |
|
| 2139 |
static struct drgn_error *drgn_module_try_proc_files(struct drgn_module *module, |
| 2140 |
const struct drgn_debug_info_options *options, |
| 2141 |
bool *tried) |
| 2142 |
{ |
| 2143 |
struct drgn_program *prog = module->prog; |
| 2144 |
|
| 2145 |
if (!options->try_procfs) |
| 2146 |
return NULL; |
| 2147 |
|
| 2148 |
*tried = false; |
| 2149 |
if (module->kind == DRGN_MODULE_MAIN) { |
| 2150 |
#define FORMAT "/proc/%ld/exe" |
| 2151 |
char path[sizeof(FORMAT) |
| 2152 |
- (sizeof("%ld") - 1) |
| 2153 |
+ max_decimal_length(long)]; |
| 2154 |
snprintf(path, sizeof(path), FORMAT, (long)prog->pid); |
| 2155 |
#undef FORMAT |
| 2156 |
int fd = open(path, O_RDONLY); |
| 2157 |
if (fd < 0) { |
| 2158 |
drgn_log_debug(prog, "%s: %m", path); |
| 2159 |
return NULL; |
| 2160 |
} |
| 2161 |
*tried = true; |
| 2162 |
return drgn_module_try_standard_file(module, options, path, fd, |
| 2163 |
false, NULL); |
| 2164 |
} else if (module->kind == DRGN_MODULE_SHARED_LIBRARY) { |
| 2165 |
return drgn_module_try_proc_files_for_shared_library(module, |
| 2166 |
options, |
| 2167 |
tried); |
| 2168 |
} else { |
| 2169 |
return NULL; |
| 2170 |
} |
| 2171 |
} |
| 2172 |
|
| 2173 |
static struct drgn_error * |
| 2174 |
drgn_module_try_files_by_build_id(struct drgn_module *module, |
| 2175 |
const struct drgn_debug_info_options *options) |
| 2176 |
{ |
| 2177 |
struct drgn_error *err; |
| 2178 |
|
| 2179 |
if (!options->try_build_id) |
| 2180 |
return NULL; |
| 2181 |
|
| 2182 |
size_t build_id_len; |
| 2183 |
const char *build_id_str = |
| 2184 |
drgn_module_build_id(module, NULL, &build_id_len); |
| 2185 |
// We need at least 2 bytes (4 hex characters) to build the paths. |
| 2186 |
if (build_id_len < 2) |
| 2187 |
return NULL; |
| 2188 |
|
| 2189 |
STRING_BUILDER(sb); |
| 2190 |
for (size_t i = 0; options->directories[i]; i++) { |
| 2191 |
const char *debug_dir = options->directories[i]; |
| 2192 |
if (!string_builder_append(&sb, debug_dir) |
| 2193 |
|| !string_builder_appendf(&sb, "/.build-id/%c%c/%s.debug", |
| 2194 |
build_id_str[0], build_id_str[1], |
| 2195 |
&build_id_str[2]) |
| 2196 |
|| !string_builder_null_terminate(&sb)) |
| 2197 |
return &drgn_enomem; |
| 2198 |
// We trust the build ID encoded in the path and don't check it |
| 2199 |
// again. |
| 2200 |
if (module->debug_file_status == DRGN_MODULE_FILE_WANT) { |
| 2201 |
err = drgn_module_try_standard_file(module, options, |
| 2202 |
sb.str, -1, false, |
| 2203 |
NULL); |
| 2204 |
if (err || !drgn_module_wants_file(module)) |
| 2205 |
return err; |
| 2206 |
} |
| 2207 |
if (module->loaded_file_status == DRGN_MODULE_FILE_WANT) { |
| 2208 |
// Remove the ".debug" extension. |
| 2209 |
sb.str[sb.len - sizeof(".debug") + 1] = '\0'; |
| 2210 |
err = drgn_module_try_standard_file(module, options, |
| 2211 |
sb.str, -1, false, |
| 2212 |
NULL); |
| 2213 |
if (err || !drgn_module_wants_file(module)) |
| 2214 |
return err; |
| 2215 |
} |
| 2216 |
sb.len = 0; |
| 2217 |
} |
| 2218 |
return NULL; |
| 2219 |
} |
| 2220 |
|
| 2221 |
// Return the first occurrence of either $ORIGIN followed by a word boundary or |
| 2222 |
// ${ORIGIN}, and set *end_ret to the character after that occurrence. Return |
| 2223 |
// NULL if not found (and *end_ret is not modified). |
| 2224 |
static const char *find_dollar_origin(const char *s, const char **end_ret) |
| 2225 |
{ |
| 2226 |
const char *dollar; |
| 2227 |
while ((dollar = strchr(s, '$'))) { |
| 2228 |
if (strstartswith(dollar + 1, "ORIGIN")) { |
| 2229 |
s = dollar + (sizeof("$ORIGIN") - 1); |
| 2230 |
// Skip it if it doesn't end at a word boundary. |
| 2231 |
if (*s == '_' || isalnum(*s)) |
| 2232 |
continue; |
| 2233 |
*end_ret = s; |
| 2234 |
break; |
| 2235 |
} else if (strstartswith(dollar + 1, "{ORIGIN}")) { |
| 2236 |
*end_ret = dollar + (sizeof("${ORIGIN}") - 1); |
| 2237 |
break; |
| 2238 |
} else { |
| 2239 |
s = dollar + 1; |
| 2240 |
} |
| 2241 |
} |
| 2242 |
return dollar; |
| 2243 |
} |
| 2244 |
|
| 2245 |
static struct drgn_error * |
| 2246 |
drgn_module_try_files_by_gnu_debuglink(struct drgn_module *module, |
| 2247 |
const struct drgn_debug_info_options *options) |
| 2248 |
{ |
| 2249 |
struct drgn_error *err; |
| 2250 |
struct drgn_program *prog = module->prog; |
| 2251 |
|
| 2252 |
if (!options->try_debug_link) |
| 2253 |
return NULL; |
| 2254 |
|
| 2255 |
struct drgn_elf_file *file = module->loaded_file; |
| 2256 |
if (!file || !file->scns[DRGN_SCN_GNU_DEBUGLINK]) |
| 2257 |
return NULL; |
| 2258 |
// We don't cache .gnu_debuglink, and it doesn't need relocation, so |
| 2259 |
// don't use drgn_elf_file_read_section(). |
| 2260 |
Elf_Data *data; |
| 2261 |
err = read_elf_section(file->scns[DRGN_SCN_GNU_DEBUGLINK], &data); |
| 2262 |
if (err) { |
| 2263 |
if (!drgn_error_is_fatal(err)) { |
| 2264 |
drgn_error_log_debug(prog, err, |
| 2265 |
"%s: couldn't read .gnu_debuglink: ", |
| 2266 |
file->path); |
| 2267 |
drgn_error_destroy(err); |
| 2268 |
err = NULL; |
| 2269 |
} |
| 2270 |
return err; |
| 2271 |
} |
| 2272 |
|
| 2273 |
struct drgn_elf_file_section_buffer buffer; |
| 2274 |
drgn_elf_file_section_buffer_init(&buffer, file, |
| 2275 |
file->scns[DRGN_SCN_GNU_DEBUGLINK], |
| 2276 |
data); |
| 2277 |
const char *debuglink; |
| 2278 |
size_t debuglink_len; |
| 2279 |
uint32_t crc; |
| 2280 |
if ((err = binary_buffer_next_string(&buffer.bb, &debuglink, |
| 2281 |
&debuglink_len)) |
| 2282 |
// Align up to 4-byte boundary. |
| 2283 |
|| (err = binary_buffer_skip(&buffer.bb, -(debuglink_len + 1) & 3)) |
| 2284 |
|| (err = binary_buffer_next_u32(&buffer.bb, &crc))) { |
| 2285 |
if (!drgn_error_is_fatal(err)) { |
| 2286 |
drgn_error_log_debug(prog, err, ""); |
| 2287 |
drgn_error_destroy(err); |
| 2288 |
err = NULL; |
| 2289 |
} |
| 2290 |
return err; |
| 2291 |
} |
| 2292 |
drgn_log_debug(prog, "%s has debuglink %s CRC 0x%08" PRIx32, file->path, |
| 2293 |
debuglink, crc); |
| 2294 |
|
| 2295 |
STRING_BUILDER(sb); |
| 2296 |
if (debuglink[0] == '/') { |
| 2297 |
// debuglink is absolute. Try it directly. |
| 2298 |
return drgn_module_try_standard_file(module, options, debuglink, |
| 2299 |
-1, false, &crc); |
| 2300 |
} |
| 2301 |
|
| 2302 |
if (!debuglink[0] || file->path[0] != '/') { |
| 2303 |
// debuglink is empty or file path is not absolute. Ignore it. |
| 2304 |
return NULL; |
| 2305 |
} |
| 2306 |
|
| 2307 |
// debuglink is relative. Try it in the debug link directories. |
| 2308 |
const char *slash = strrchr(file->path, '/'); |
| 2309 |
// We just checked that the file path is absolute, so there must be a |
| 2310 |
// slash. Also trim extra slashes just in case. |
| 2311 |
while (slash != file->path && slash[-1] == '/') |
| 2312 |
slash--; |
| 2313 |
size_t dir_len = slash - file->path; |
| 2314 |
const char * const *next_debug_link_dir = |
| 2315 |
options->debug_link_directories; |
| 2316 |
const char * const *next_debug_dir = NULL; |
| 2317 |
for (;;) { |
| 2318 |
if (next_debug_dir) { |
| 2319 |
const char *debug_dir = *next_debug_dir++; |
| 2320 |
if (!debug_dir) { |
| 2321 |
next_debug_dir = NULL; |
| 2322 |
continue; |
| 2323 |
} |
| 2324 |
if (!string_builder_append(&sb, debug_dir) |
| 2325 |
|| !string_builder_appendn(&sb, file->path, dir_len)) |
| 2326 |
return &drgn_enomem; |
| 2327 |
} else { |
| 2328 |
const char *debug_link_dir = *next_debug_link_dir++; |
| 2329 |
if (!debug_link_dir) |
| 2330 |
return NULL; |
| 2331 |
if (!debug_link_dir[0]) { |
| 2332 |
// Empty path. Try under the debug directories. |
| 2333 |
next_debug_dir = options->directories; |
| 2334 |
continue; |
| 2335 |
} |
| 2336 |
const char *s = debug_link_dir; |
| 2337 |
const char *dollar, *end; |
| 2338 |
while ((dollar = find_dollar_origin(s, &end))) { |
| 2339 |
if (!string_builder_appendn(&sb, s, dollar - s) |
| 2340 |
|| !string_builder_appendn(&sb, file->path, |
| 2341 |
dir_len)) |
| 2342 |
return &drgn_enomem; |
| 2343 |
s = end; |
| 2344 |
} |
| 2345 |
if (!string_builder_append(&sb, s)) |
| 2346 |
return &drgn_enomem; |
| 2347 |
} |
| 2348 |
if (!string_builder_appendc(&sb, '/') |
| 2349 |
|| !string_builder_appendn(&sb, debuglink, debuglink_len) |
| 2350 |
|| !string_builder_null_terminate(&sb)) |
| 2351 |
return &drgn_enomem; |
| 2352 |
err = drgn_module_try_standard_file(module, options, sb.str, -1, |
| 2353 |
false, &crc); |
| 2354 |
if (err || !drgn_module_wants_file(module)) |
| 2355 |
return err; |
| 2356 |
sb.len = 0; |
| 2357 |
} |
| 2358 |
} |
| 2359 |
|
| 2360 |
static struct drgn_error * |
| 2361 |
drgn_module_try_standard_files(struct drgn_module *module, |
| 2362 |
const struct drgn_debug_info_options *options, |
| 2363 |
struct drgn_standard_debug_info_find_state *state) |
| 2364 |
{ |
| 2365 |
struct drgn_error *err; |
| 2366 |
struct drgn_program *prog = module->prog; |
| 2367 |
|
| 2368 |
// This can't happen when called from the standard debug info finder, |
| 2369 |
// but it can from drgn_find_standard_debug_info(). |
| 2370 |
if (!drgn_module_wants_file(module)) |
| 2371 |
return NULL; |
| 2372 |
|
| 2373 |
drgn_module_try_files_log(module, "trying standard paths for"); |
| 2374 |
|
| 2375 |
// If we need a supplementary file, try that first. |
| 2376 |
err = drgn_module_try_standard_supplementary_files(module, options); |
| 2377 |
if (err || !drgn_module_wants_file(module)) |
| 2378 |
return err; |
| 2379 |
|
| 2380 |
// If a previous attempt used a loadable file with debug info but didn't |
| 2381 |
// want both, we might be able to reuse it. |
| 2382 |
if (options->try_reuse |
| 2383 |
&& module->loaded_file_status == DRGN_MODULE_FILE_WANT) { |
| 2384 |
struct drgn_elf_file *reuse_file = NULL; |
| 2385 |
if (module->debug_file && module->debug_file->is_loadable) |
| 2386 |
reuse_file = module->debug_file; |
| 2387 |
else if (module->wanted_supplementary_debug_file |
| 2388 |
&& module->wanted_supplementary_debug_file->file->is_loadable) |
| 2389 |
reuse_file = module->wanted_supplementary_debug_file->file; |
| 2390 |
if (reuse_file) { |
| 2391 |
drgn_log_debug(prog, |
| 2392 |
"reusing loadable debug file %s as loaded file", |
| 2393 |
reuse_file->path); |
| 2394 |
err = drgn_module_maybe_use_elf_file(module, reuse_file, |
| 2395 |
false); |
| 2396 |
if (err || !drgn_module_wants_file(module)) |
| 2397 |
return err; |
| 2398 |
} |
| 2399 |
} |
| 2400 |
if (options->try_reuse |
| 2401 |
&& module->debug_file_status == DRGN_MODULE_FILE_WANT |
| 2402 |
&& module->loaded_file |
| 2403 |
&& drgn_elf_file_has_dwarf(module->loaded_file)) { |
| 2404 |
drgn_log_debug(prog, |
| 2405 |
"reusing loaded file with debug info %s as debug file", |
| 2406 |
module->loaded_file->path); |
| 2407 |
err = drgn_module_maybe_use_elf_file(module, |
| 2408 |
module->loaded_file, |
| 2409 |
false); |
| 2410 |
if (err || !drgn_module_wants_file(module)) |
| 2411 |
return err; |
| 2412 |
} |
| 2413 |
|
| 2414 |
// First, try methods that are guaranteed to find the right file: |
| 2415 |
// reading a vDSO from the core dump and opening a file via a magic |
| 2416 |
// symlink in /proc. |
| 2417 |
bool tried_proc_symlink = false; |
| 2418 |
if (module->kind == DRGN_MODULE_VDSO) { |
| 2419 |
err = drgn_module_try_vdso_in_core(module, options); |
| 2420 |
if (err || !drgn_module_wants_file(module)) |
| 2421 |
return err; |
| 2422 |
} else if (drgn_program_is_userspace_process(prog)) { |
| 2423 |
err = drgn_module_try_proc_files(module, options, |
| 2424 |
&tried_proc_symlink); |
| 2425 |
if (err || !drgn_module_wants_file(module)) |
| 2426 |
return err; |
| 2427 |
} |
| 2428 |
|
| 2429 |
// If we already have the build ID, try it now before wasting time with |
| 2430 |
// the expected paths. If this is a Linux kernel loadable module, this |
| 2431 |
// can save us from needing the depmod index. If not, it can still save |
| 2432 |
// us from trying a file with the wrong build ID. |
| 2433 |
const bool had_build_id = module->build_id_len > 0; |
| 2434 |
if (had_build_id) { |
| 2435 |
err = drgn_module_try_files_by_build_id(module, options); |
| 2436 |
if (err || !drgn_module_wants_file(module)) |
| 2437 |
return err; |
| 2438 |
} |
| 2439 |
|
| 2440 |
// Next, try opening things at their expected paths. If this is the |
| 2441 |
// Linux kernel or a Linux kernel loadable module, try some well-known |
| 2442 |
// paths. |
| 2443 |
if (module->kind == DRGN_MODULE_MAIN |
| 2444 |
&& (module->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL)) { |
| 2445 |
err = drgn_module_try_vmlinux_files(module, options); |
| 2446 |
if (err || !drgn_module_wants_file(module)) |
| 2447 |
return err; |
| 2448 |
} else if (module->kind == DRGN_MODULE_RELOCATABLE |
| 2449 |
&& (module->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL)) { |
| 2450 |
err = drgn_module_try_linux_kmod_files(module, options, state); |
| 2451 |
if (err || !drgn_module_wants_file(module)) |
| 2452 |
return err; |
| 2453 |
// Otherwise, if the module name looks like a path (i.e., it contains a |
| 2454 |
// slash), try it. The vDSO is embedded in the kernel and isn't on disk, |
| 2455 |
// so there's no point in trying it. Additionally, if we already tried a |
| 2456 |
// /proc symlink, then we already tried the file that the path is |
| 2457 |
// supposed to refer to, so don't try again. |
| 2458 |
} else if (module->kind != DRGN_MODULE_VDSO |
| 2459 |
&& options->try_module_name |
| 2460 |
&& !tried_proc_symlink |
| 2461 |
&& strchr(module->name, '/')) { |
| 2462 |
err = drgn_module_try_standard_file(module, options, |
| 2463 |
module->name, -1, true, |
| 2464 |
NULL); |
| 2465 |
if (err || !drgn_module_wants_file(module)) |
| 2466 |
return err; |
| 2467 |
} |
| 2468 |
|
| 2469 |
// If we didn't have the build ID before, we might have found the loaded |
| 2470 |
// file and gotten a build ID from it. Try to find the debug file by |
| 2471 |
// build ID now. |
| 2472 |
if (!had_build_id) { |
| 2473 |
err = drgn_module_try_files_by_build_id(module, options); |
| 2474 |
if (err || !drgn_module_wants_file(module)) |
| 2475 |
return err; |
| 2476 |
} |
| 2477 |
|
| 2478 |
// We might have a loaded file with a .gnu_debuglink. Try to find the |
| 2479 |
// corresponding debug file. |
| 2480 |
return drgn_module_try_files_by_gnu_debuglink(module, options); |
| 2481 |
} |
| 2482 |
|
| 2483 |
static struct drgn_error * |
| 2484 |
drgn_standard_debug_info_find(struct drgn_module * const *modules, |
| 2485 |
size_t num_modules, void *arg) |
| 2486 |
{ |
| 2487 |
struct drgn_error *err; |
| 2488 |
struct drgn_debug_info_options *options = arg; |
| 2489 |
|
| 2490 |
if (drgn_log_is_enabled(modules[0]->prog, DRGN_LOG_DEBUG)) { |
| 2491 |
_cleanup_free_ char *options_str = |
| 2492 |
drgn_format_debug_info_options(options); |
| 2493 |
if (!options_str) |
| 2494 |
return &drgn_enomem; |
| 2495 |
drgn_log_debug(modules[0]->prog, |
| 2496 |
"trying standard debug info finder with %s%s", |
| 2497 |
options == &modules[0]->prog->dbinfo.options |
| 2498 |
? "" : "given ", |
| 2499 |
options_str); |
| 2500 |
} |
| 2501 |
|
| 2502 |
_cleanup_(drgn_standard_debug_info_find_state_deinit) |
| 2503 |
struct drgn_standard_debug_info_find_state state = { |
| 2504 |
.modules = modules, |
| 2505 |
.num_modules = num_modules, |
| 2506 |
.kmod_walk = { |
| 2507 |
.modules = HASH_TABLE_INIT, |
| 2508 |
.stack = VECTOR_INIT, |
| 2509 |
.path = STRING_BUILDER_INIT, |
| 2510 |
.visited_dirs = HASH_TABLE_INIT, |
| 2511 |
.next_kernel_dir = options->kernel_directories, |
| 2512 |
}, |
| 2513 |
}; |
| 2514 |
for (size_t i = 0; i < num_modules; i++) { |
| 2515 |
err = drgn_module_try_standard_files(modules[i], options, |
| 2516 |
&state); |
| 2517 |
if (err) |
| 2518 |
return err; |
| 2519 |
} |
| 2520 |
return NULL; |
| 2521 |
} |
| 2522 |
|
| 2523 |
LIBDRGN_PUBLIC struct drgn_error * |
| 2524 |
drgn_find_standard_debug_info(struct drgn_module * const *modules, |
| 2525 |
size_t num_modules, |
| 2526 |
struct drgn_debug_info_options *options) |
| 2527 |
{ |
| 2528 |
if (num_modules == 0) |
| 2529 |
return NULL; |
| 2530 |
|
| 2531 |
struct drgn_program *prog = modules[0]->prog; |
| 2532 |
for (size_t i = 0; i < num_modules; i++) { |
| 2533 |
if (modules[i]->prog != prog) { |
| 2534 |
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT, |
| 2535 |
"modules are from different programs"); |
| 2536 |
} |
| 2537 |
} |
| 2538 |
|
| 2539 |
if (!options) |
| 2540 |
options = &modules[0]->prog->dbinfo.options; |
| 2541 |
return drgn_standard_debug_info_find(modules, num_modules, options); |
| 2542 |
} |
| 2543 |
|
| 2544 |
#if WITH_DEBUGINFOD |
| 2545 |
static int count_columns(const char *s, size_t n) |
| 2546 |
{ |
| 2547 |
int columns = 0; |
| 2548 |
while (n > 0) { |
| 2549 |
mbstate_t ps; |
| 2550 |
memset(&ps, 0, sizeof(ps)); |
| 2551 |
do { |
| 2552 |
wchar_t wc; |
| 2553 |
size_t r = mbrtowc(&wc, s, n, &ps); |
| 2554 |
if (r == (size_t)-1) // Invalid multibyte sequence. |
| 2555 |
return -1; |
| 2556 |
if (r == (size_t)-2) // Incomplete multibyte character. |
| 2557 |
return -2; |
| 2558 |
if (r == 0) // Null wide character. |
| 2559 |
r = 1; |
| 2560 |
|
| 2561 |
int w = wcwidth(wc); |
| 2562 |
if (w < 0) // Nonprintable wide character. |
| 2563 |
return -3; |
| 2564 |
s += r; |
| 2565 |
n -= r; |
| 2566 |
columns += w; |
| 2567 |
} while (!mbsinit(&ps)); |
| 2568 |
} |
| 2569 |
return columns; |
| 2570 |
} |
| 2571 |
|
| 2572 |
static int truncate_columns(struct string_builder *sb, size_t start, size_t end, |
| 2573 |
int max_columns) |
| 2574 |
{ |
| 2575 |
int columns = 0; |
| 2576 |
|
| 2577 |
size_t truncate_len = start; |
| 2578 |
int truncate_column = 0; |
| 2579 |
mbstate_t truncate_ps; |
| 2580 |
memset(&truncate_ps, 0, sizeof(truncate_ps)); |
| 2581 |
|
| 2582 |
while (start < end) { |
| 2583 |
mbstate_t ps; |
| 2584 |
memset(&ps, 0, sizeof(ps)); |
| 2585 |
do { |
| 2586 |
wchar_t wc; |
| 2587 |
size_t r = mbrtowc(&wc, &sb->str[start], end - start, |
| 2588 |
&ps); |
| 2589 |
if (r == (size_t)-1) // Invalid multibyte sequence. |
| 2590 |
return -1; |
| 2591 |
if (r == (size_t)-2) // Incomplete multibyte character. |
| 2592 |
return -2; |
| 2593 |
if (r == 0) // Null wide character. |
| 2594 |
r = 1; |
| 2595 |
|
| 2596 |
int w = wcwidth(wc); |
| 2597 |
if (w < 0) // Nonprintable wide character. |
| 2598 |
return -3; |
| 2599 |
|
| 2600 |
if (w > max_columns - columns) { |
| 2601 |
int dots = min(max_columns, 3); |
| 2602 |
char reset[MB_LEN_MAX]; |
| 2603 |
size_t reset_len = 0; |
| 2604 |
if (!mbsinit(&truncate_ps)) { |
| 2605 |
reset_len = wcrtomb(reset, L'\0', |
| 2606 |
&truncate_ps) - 1; |
| 2607 |
} |
| 2608 |
size_t new_len = (truncate_len |
| 2609 |
+ reset_len |
| 2610 |
+ dots |
| 2611 |
+ (sb->len - end)); |
| 2612 |
if (!string_builder_reserve(sb, new_len)) |
| 2613 |
return INT_MIN; |
| 2614 |
memmove(&sb->str[truncate_len + reset_len + dots], |
| 2615 |
&sb->str[end], sb->len - end); |
| 2616 |
memset(&sb->str[truncate_len + reset_len], '.', |
| 2617 |
dots); |
| 2618 |
memcpy(&sb->str[truncate_len], reset, |
| 2619 |
reset_len); |
| 2620 |
sb->len = new_len; |
| 2621 |
return truncate_column + dots; |
| 2622 |
} |
| 2623 |
|
| 2624 |
start += r; |
| 2625 |
columns += w; |
| 2626 |
if (columns <= max_columns - 3) { |
| 2627 |
truncate_len = start; |
| 2628 |
truncate_column = columns; |
| 2629 |
memcpy(&truncate_ps, &ps, sizeof(ps)); |
| 2630 |
} |
| 2631 |
} while (!mbsinit(&ps)); |
| 2632 |
} |
| 2633 |
return columns; |
| 2634 |
} |
| 2635 |
|
| 2636 |
static void reset_shift_state(struct string_builder *sb, mbstate_t *ps) |
| 2637 |
{ |
| 2638 |
if (!mbsinit(ps)) |
| 2639 |
sb->len += wcrtomb(&sb->str[sb->len], L'\0', ps) - 1; |
| 2640 |
} |
| 2641 |
|
| 2642 |
static bool write_unicode_progress_bar(struct string_builder *sb, int columns, |
| 2643 |
double ratio) |
| 2644 |
{ |
| 2645 |
size_t orig_len = sb->len; |
| 2646 |
|
| 2647 |
mbstate_t ps; |
| 2648 |
memset(&ps, 0, sizeof(ps)); |
| 2649 |
|
| 2650 |
// "Right one eighth block" character. |
| 2651 |
size_t r = wcrtomb(&sb->str[sb->len], L'\u2595', &ps); |
| 2652 |
if (r == (size_t)-1) |
| 2653 |
return false; |
| 2654 |
sb->len += r; |
| 2655 |
|
| 2656 |
// + 0.25 so that we round up if the piece would be at least 75% full. |
| 2657 |
int eighths = columns * ratio * 8.0 + 0.25; |
| 2658 |
int blocks = eighths / 8; |
| 2659 |
int i; |
| 2660 |
for (i = 0; i < blocks; i++) { |
| 2661 |
// "Full block" character. |
| 2662 |
r = wcrtomb(&sb->str[sb->len], L'\u2588', &ps); |
| 2663 |
if (r == (size_t)-1) |
| 2664 |
goto undo; |
| 2665 |
sb->len += r; |
| 2666 |
} |
| 2667 |
// "Left one eighth block" through "left seven eighths block" |
| 2668 |
// characters. |
| 2669 |
static const wchar_t eighths_blocks[7] = |
| 2670 |
L"\u258f\u258e\u258d\u258c\u258b\u258a\u2589"; |
| 2671 |
if (eighths % 8 != 0) { |
| 2672 |
r = wcrtomb(&sb->str[sb->len], eighths_blocks[eighths % 8 - 1], |
| 2673 |
&ps); |
| 2674 |
if (r == (size_t)-1) |
| 2675 |
goto undo; |
| 2676 |
sb->len += r; |
| 2677 |
i++; |
| 2678 |
} |
| 2679 |
|
| 2680 |
for (; i < columns; i++) { |
| 2681 |
r = wcrtomb(&sb->str[sb->len], L' ', &ps); |
| 2682 |
if (r == (size_t)-1) |
| 2683 |
goto undo; |
| 2684 |
sb->len += r; |
| 2685 |
} |
| 2686 |
|
| 2687 |
// "Left one eighth block" character. |
| 2688 |
r = wcrtomb(&sb->str[sb->len], L'\u258f', &ps); |
| 2689 |
if (r == (size_t)-1) |
| 2690 |
goto undo; |
| 2691 |
sb->len += r; |
| 2692 |
|
| 2693 |
reset_shift_state(sb, &ps); |
| 2694 |
return true; |
| 2695 |
|
| 2696 |
undo: |
| 2697 |
sb->len = orig_len; |
| 2698 |
return false; |
| 2699 |
} |
| 2700 |
|
| 2701 |
static void write_ascii_progress_bar(struct string_builder *sb, int columns, |
| 2702 |
double ratio) |
| 2703 |
{ |
| 2704 |
sb->str[sb->len++] = '['; |
| 2705 |
// + 0.25 so that we round up if the block would be at least 75% full. |
| 2706 |
int blocks = columns * ratio + 0.25; |
| 2707 |
memset(&sb->str[sb->len], '#', blocks); |
| 2708 |
sb->len += blocks; |
| 2709 |
memset(&sb->str[sb->len], ' ', columns - blocks); |
| 2710 |
sb->len += columns - blocks; |
| 2711 |
sb->str[sb->len++] = ']'; |
| 2712 |
} |
| 2713 |
|
| 2714 |
static bool write_unicode_spinner(struct string_builder *sb, int pos) |
| 2715 |
{ |
| 2716 |
static const wchar_t spinner[] = { |
| 2717 |
L'\u2596', // Quadrant lower left |
| 2718 |
L'\u2598', // Quadrant upper left |
| 2719 |
L'\u259d', // Quadrant upper right |
| 2720 |
L'\u2597', // Quadrant lower right |
| 2721 |
}; |
| 2722 |
mbstate_t ps; |
| 2723 |
memset(&ps, 0, sizeof(ps)); |
| 2724 |
size_t r = wcrtomb(&sb->str[sb->len], |
| 2725 |
spinner[pos % array_size(spinner)], &ps); |
| 2726 |
if (r == (size_t)-1) |
| 2727 |
return false; |
| 2728 |
sb->len += r; |
| 2729 |
reset_shift_state(sb, &ps); |
| 2730 |
return true; |
| 2731 |
} |
| 2732 |
|
| 2733 |
static void write_ascii_spinner(struct string_builder *sb, int pos) |
| 2734 |
{ |
| 2735 |
static const char spinner[] = { '|', '/', '-', '\\' }; |
| 2736 |
sb->str[sb->len++] = spinner[pos % array_size(spinner)]; |
| 2737 |
} |
| 2738 |
|
| 2739 |
// debuginfod_set_user_data() and debuginfod_get_user_data() were added in |
| 2740 |
// elfutils 0.179. Before that, we emulate them with a thread-local variable. |
| 2741 |
#if !_ELFUTILS_PREREQ(0, 179) |
| 2742 |
static _Thread_local void *drgn_debuginfod_user_data; |
| 2743 |
#endif |
| 2744 |
|
| 2745 |
// This is called with: |
| 2746 |
// - a >= 0 && b == 0 while cleaning the debuginfod cache, where a is the number |
| 2747 |
// of files in the cache that have been checked. |
| 2748 |
// - a >= 0 && b == 0 while waiting to read the first chunk of data from a |
| 2749 |
// debuginfod server, where a is an increasing counter. Note that this cannot |
| 2750 |
// be distinguished from the previous case. |
| 2751 |
// - a >= 0 && b > 0 while downloading, where a is the number of bytes |
| 2752 |
// downloaded and b is the total size to download in bytes. |
| 2753 |
// - a >= 0 && b <= 0 while downloading, where a is the number of bytes |
| 2754 |
// downloaded and the total size is not known. This can be distinguished from |
| 2755 |
// the first two cases because debuginfod_get_url() will return non-NULL. |
| 2756 |
// - a < 0 && b >= 0 when the download has finished successfully. b is the |
| 2757 |
// downloaded file descriptor. |
| 2758 |
// - a < 0 && b < 0 when the download failed. b is a negative errno. |
| 2759 |
static void drgn_log_debuginfod_progress(debuginfod_client *client, long a, |
| 2760 |
long b) |
| 2761 |
{ |
| 2762 |
#if _ELFUTILS_PREREQ(0, 179) |
| 2763 |
struct drgn_program *prog = drgn_debuginfod_get_user_data(client); |
| 2764 |
#else |
| 2765 |
struct drgn_program *prog = drgn_debuginfod_user_data; |
| 2766 |
#endif |
| 2767 |
|
| 2768 |
const bool done = a < 0; |
| 2769 |
|
| 2770 |
// If we already started logging progress for this download when it |
| 2771 |
// failed, we log the error like progress below. Otherwise, the download |
| 2772 |
// failed very early, so we only log a debug message. |
| 2773 |
if (done && b < 0 && !prog->dbinfo.logged_debuginfod_progress) { |
| 2774 |
if (b != -ENOSYS) { |
| 2775 |
errno = -b; |
| 2776 |
drgn_log_debug(prog, |
| 2777 |
"%s: couldn't download%s from debuginfod: %m", |
| 2778 |
prog->dbinfo.debuginfod_current_name, |
| 2779 |
prog->dbinfo.debuginfod_current_type); |
| 2780 |
} else if (!prog->dbinfo.logged_no_debuginfod) { |
| 2781 |
drgn_log_debug(prog, |
| 2782 |
"no debuginfod servers configured; " |
| 2783 |
"try setting the DEBUGINFOD_URLS environment variable"); |
| 2784 |
prog->dbinfo.logged_no_debuginfod = true; |
| 2785 |
} |
| 2786 |
return; |
| 2787 |
} |
| 2788 |
prog->dbinfo.logged_debuginfod_progress = true; |
| 2789 |
|
| 2790 |
int columns; |
| 2791 |
FILE *file = drgn_program_get_progress_file(prog, &columns); |
| 2792 |
|
| 2793 |
// ANSI escape sequence to clear the current line and return the cursor |
| 2794 |
// to the beginning of the line. |
| 2795 |
static const char ansi_erase_line[] = "\33[2K\r"; |
| 2796 |
|
| 2797 |
// Once we know what URL we are downloading from, log it. |
| 2798 |
if (!prog->dbinfo.debuginfod_have_url) { |
| 2799 |
// debuginfod_get_url() was added in elfutils 0.179. Before |
| 2800 |
// that, we have to assume that we have a URL. |
| 2801 |
#if _ELFUTILS_PREREQ(0, 179) |
| 2802 |
const char *url = drgn_debuginfod_get_url(client); |
| 2803 |
if (url) { |
| 2804 |
prog->dbinfo.debuginfod_have_url = true; |
| 2805 |
// Erase the current line since we may have logged |
| 2806 |
// progress. |
| 2807 |
if (columns >= 0) { |
| 2808 |
fwrite(ansi_erase_line, 1, |
| 2809 |
sizeof(ansi_erase_line) - 1, file); |
| 2810 |
fflush(file); |
| 2811 |
} |
| 2812 |
drgn_log_debug(prog, "downloading from debuginfod at %s", url); |
| 2813 |
} |
| 2814 |
#else |
| 2815 |
prog->dbinfo.debuginfod_have_url = true; |
| 2816 |
#endif |
| 2817 |
} |
| 2818 |
|
| 2819 |
// If we succeeded without ever getting a URL, it must have been cached. |
| 2820 |
if (done && b >= 0 && !prog->dbinfo.debuginfod_have_url) { |
| 2821 |
// We may have logged download progress when we were actually |
| 2822 |
// cleaning the cache. Clear it to avoid confusion. |
| 2823 |
if (columns >= 0) { |
| 2824 |
fwrite(ansi_erase_line, 1, sizeof(ansi_erase_line) - 1, |
| 2825 |
file); |
| 2826 |
fflush(file); |
| 2827 |
} |
| 2828 |
drgn_log_debug(prog, "%s: found%s in debuginfod cache", |
| 2829 |
prog->dbinfo.debuginfod_current_name, |
| 2830 |
prog->dbinfo.debuginfod_current_type); |
| 2831 |
return; |
| 2832 |
} |
| 2833 |
|
| 2834 |
if (!file) |
| 2835 |
return; |
| 2836 |
|
| 2837 |
// We only do the progress animation if we would have at least one |
| 2838 |
// column for a progress bar. Using the calculation for bar_columns |
| 2839 |
// below: |
| 2840 |
// |
| 2841 |
// columns - (floor(columns / 2) - 10) - 2 - 4 >= 1 |
| 2842 |
// => columns - floor(columns / 2) >= 17 |
| 2843 |
// => ceil(columns / 2) >= 17 |
| 2844 |
// => columns >= 33 |
| 2845 |
bool animate = columns >= 33; |
| 2846 |
const bool orig_animate = animate; |
| 2847 |
|
| 2848 |
STRING_BUILDER(sb); |
| 2849 |
|
| 2850 |
if (animate && !string_builder_appendc(&sb, '\r')) |
| 2851 |
return; |
| 2852 |
|
| 2853 |
int fill_columns = 0; |
| 2854 |
int bar_columns = 0; |
| 2855 |
if (animate) { |
| 2856 |
if (done) { |
| 2857 |
// We need to erase anything left in the line with |
| 2858 |
// spaces. |
| 2859 |
fill_columns = columns; |
| 2860 |
} else if (b > 0) { |
| 2861 |
// Use half of the line plus a bit for the name and |
| 2862 |
// download size so that it doesn't get too short in |
| 2863 |
// small terminals. |
| 2864 |
fill_columns = columns / 2 + 10; |
| 2865 |
// Use the rest for the progress bar. |
| 2866 |
bar_columns = (columns - fill_columns |
| 2867 |
- 2 // Ends of progress bar |
| 2868 |
- 4 // " XX%" |
| 2869 |
); |
| 2870 |
} else { |
| 2871 |
// Use the whole line, minus the spinner, for the name |
| 2872 |
// and download size |
| 2873 |
fill_columns = columns - 1; |
| 2874 |
} |
| 2875 |
} |
| 2876 |
|
| 2877 |
if (!string_builder_append(&sb, |
| 2878 |
done && b >= 0 |
| 2879 |
? "Downloaded " : "Downloading ") |
| 2880 |
|| !string_builder_append(&sb, |
| 2881 |
prog->dbinfo.debuginfod_current_name) |
| 2882 |
|| !string_builder_append(&sb, |
| 2883 |
prog->dbinfo.debuginfod_current_type)) |
| 2884 |
return; |
| 2885 |
|
| 2886 |
size_t download_size_start = sb.len; |
| 2887 |
if (done && b < 0) { |
| 2888 |
errno = -b; |
| 2889 |
if (!string_builder_appendf(&sb, " failed: %m")) |
| 2890 |
return; |
| 2891 |
} else if (prog->dbinfo.debuginfod_have_url) { |
| 2892 |
intmax_t download_size; |
| 2893 |
if (done) { |
| 2894 |
struct stat st; |
| 2895 |
if (fstat(b, &st) < 0) { |
| 2896 |
drgn_log_warning(prog, "fstat: %m"); |
| 2897 |
return; |
| 2898 |
} |
| 2899 |
download_size = st.st_size; |
| 2900 |
} else { |
| 2901 |
download_size = a; |
| 2902 |
} |
| 2903 |
if (download_size < 2048) { |
| 2904 |
if (!string_builder_appendf(&sb, " (%" PRIdMAX " B)", |
| 2905 |
download_size)) |
| 2906 |
return; |
| 2907 |
} else { |
| 2908 |
static const char prefixes[] = "KMGTPEZY"; |
| 2909 |
int i = 1; |
| 2910 |
while (i < sizeof(prefixes) - 1 |
| 2911 |
&& (download_size >> (10 * i)) >= 2048) |
| 2912 |
i++; |
| 2913 |
double unit = INTMAX_C(1) << (10 * i); |
| 2914 |
if (!string_builder_appendf(&sb, " (%.1f %ciB)", |
| 2915 |
download_size / unit, |
| 2916 |
prefixes[i - 1])) |
| 2917 |
return; |
| 2918 |
} |
| 2919 |
} |
| 2920 |
|
| 2921 |
if (animate) { |
| 2922 |
int current_column; |
| 2923 |
if (done) { |
| 2924 |
// Start at byte 1 to skip the "\r". |
| 2925 |
current_column = count_columns(&sb.str[1], sb.len - 1); |
| 2926 |
} else { |
| 2927 |
int download_size_len = sb.len - download_size_start; |
| 2928 |
// Leave room for the download size and an extra space. |
| 2929 |
int max_columns = |
| 2930 |
max(fill_columns - download_size_len - 1, 0); |
| 2931 |
// Start at byte 1 to skip the "\r". |
| 2932 |
current_column = truncate_columns(&sb, 1, |
| 2933 |
download_size_start, |
| 2934 |
max_columns); |
| 2935 |
if (current_column == INT_MIN) |
| 2936 |
return; // Memory allocation failed. |
| 2937 |
if (current_column >= 0) |
| 2938 |
current_column += download_size_len; |
| 2939 |
} |
| 2940 |
if (current_column < 0) { |
| 2941 |
// We either couldn't decode the string or the string |
| 2942 |
// contained a nonprintable character. Give up on the |
| 2943 |
// animation. |
| 2944 |
animate = false; |
| 2945 |
} else if (current_column < fill_columns) { |
| 2946 |
if (!string_builder_reserve_for_append(&sb, |
| 2947 |
fill_columns |
| 2948 |
- current_column)) |
| 2949 |
return; |
| 2950 |
memset(&sb.str[sb.len], ' ', |
| 2951 |
fill_columns - current_column); |
| 2952 |
sb.len += fill_columns - current_column; |
| 2953 |
} |
| 2954 |
} |
| 2955 |
|
| 2956 |
// If we can't encode any of the following Unicode characters in the |
| 2957 |
// current locale, we fall back to ASCII. |
| 2958 |
if (!done && b > 0) { |
| 2959 |
// Clamp the ratio in case we get bogus sizes. |
| 2960 |
double ratio = a < b ? (double)a / (double)b : 1.0; |
| 2961 |
if (animate) { |
| 2962 |
// One multibyte character for each bar column, one for |
| 2963 |
// each end, and one to reset the shift state. |
| 2964 |
if (!string_builder_reserve_for_append(&sb, |
| 2965 |
(bar_columns + 3) |
| 2966 |
* MB_CUR_MAX)) |
| 2967 |
return; |
| 2968 |
if (!write_unicode_progress_bar(&sb, bar_columns, |
| 2969 |
ratio)) { |
| 2970 |
write_ascii_progress_bar(&sb, bar_columns, |
| 2971 |
ratio); |
| 2972 |
} |
| 2973 |
} |
| 2974 |
unsigned int percent = 100.0 * ratio; |
| 2975 |
// We're not 100% done until we're called with done = true. |
| 2976 |
if (percent > 99) |
| 2977 |
percent = 99; |
| 2978 |
if (!string_builder_appendf(&sb, " %*u%%", animate ? 2 : 0, |
| 2979 |
percent)) |
| 2980 |
return; |
| 2981 |
} else if (!done && animate) { |
| 2982 |
// One multibyte character for the spinner, one to reset the |
| 2983 |
// shift state. |
| 2984 |
if (!string_builder_reserve_for_append(&sb, 2 * MB_CUR_MAX)) |
| 2985 |
return; |
| 2986 |
unsigned int pos = prog->dbinfo.debuginfod_spinner_position++; |
| 2987 |
if (!write_unicode_spinner(&sb, pos)) |
| 2988 |
write_ascii_spinner(&sb, pos); |
| 2989 |
} |
| 2990 |
|
| 2991 |
if ((done || !animate) && !string_builder_appendc(&sb, '\n')) |
| 2992 |
return; |
| 2993 |
|
| 2994 |
// If we were originally animating but gave up, we need to skip the |
| 2995 |
// "\r". |
| 2996 |
fwrite(sb.str + (orig_animate && !animate ? 1 : 0), 1, |
| 2997 |
sb.len - (orig_animate && !animate ? 1 : 0), file); |
| 2998 |
} |
| 2999 |
|
| 3000 |
static struct sigaction drgn_cancel_debuginfod_oldact; |
| 3001 |
static volatile sig_atomic_t drgn_cancel_debuginfod; |
| 3002 |
static void drgn_cancel_debuginfod_handler(int sig) |
| 3003 |
{ |
| 3004 |
drgn_cancel_debuginfod = 1; |
| 3005 |
drgn_cancel_debuginfod_oldact.sa_handler(sig); |
| 3006 |
} |
| 3007 |
static void drgn_cancel_debuginfod_sigaction(int sig, siginfo_t *info, |
| 3008 |
void *ucontext) |
| 3009 |
{ |
| 3010 |
drgn_cancel_debuginfod = 1; |
| 3011 |
drgn_cancel_debuginfod_oldact.sa_sigaction(sig, info, ucontext); |
| 3012 |
} |
| 3013 |
static bool drgn_prepare_debuginfod_find(struct drgn_program *prog) |
| 3014 |
{ |
| 3015 |
#if !_ELFUTILS_PREREQ(0, 179) |
| 3016 |
drgn_debuginfod_user_data = prog; |
| 3017 |
#endif |
| 3018 |
// If the application has a signal handler for SIGINT, temporarily wrap |
| 3019 |
// it with our own signal handler that sets a flag for the debuginfod |
| 3020 |
// progressfn. This allows Ctrl+C to interrupt a download in |
| 3021 |
// applications that handle SIGINT (like the Python interpreter). |
| 3022 |
drgn_cancel_debuginfod = 0; |
| 3023 |
if (sigaction(SIGINT, NULL, &drgn_cancel_debuginfod_oldact) != 0) |
| 3024 |
return false; |
| 3025 |
struct sigaction act = drgn_cancel_debuginfod_oldact; |
| 3026 |
if ((act.sa_flags & SA_SIGINFO) |
| 3027 |
// SIG_DFL and SIG_IGN are meant to be assigned to sa_handler, but |
| 3028 |
// the Linux kernel treats them the same for sa_sigaction. |
| 3029 |
&& act.sa_sigaction != (void *)SIG_DFL |
| 3030 |
&& act.sa_sigaction != (void *)SIG_IGN) |
| 3031 |
act.sa_sigaction = drgn_cancel_debuginfod_sigaction; |
| 3032 |
else if (!(act.sa_flags & SA_SIGINFO) |
| 3033 |
&& act.sa_handler != SIG_DFL && act.sa_handler != SIG_IGN) |
| 3034 |
act.sa_handler = drgn_cancel_debuginfod_handler; |
| 3035 |
else |
| 3036 |
return false; |
| 3037 |
return sigaction(SIGINT, &act, NULL) == 0; |
| 3038 |
} |
| 3039 |
static void drgn_finish_debuginfod_find(bool restore_sigaction) |
| 3040 |
{ |
| 3041 |
if (restore_sigaction) |
| 3042 |
sigaction(SIGINT, &drgn_cancel_debuginfod_oldact, NULL); |
| 3043 |
} |
| 3044 |
|
| 3045 |
static int drgn_debuginfod_progressfn(debuginfod_client *client, long a, long b) |
| 3046 |
{ |
| 3047 |
if (drgn_cancel_debuginfod) |
| 3048 |
return 1; |
| 3049 |
if (a >= 0) |
| 3050 |
drgn_log_debuginfod_progress(client, a, b); |
| 3051 |
return 0; |
| 3052 |
} |
| 3053 |
|
| 3054 |
static struct drgn_error * |
| 3055 |
drgn_module_try_file_from_debuginfod(struct drgn_module *module, |
| 3056 |
const char *build_id_str, |
| 3057 |
bool debug, bool supplementary, |
| 3058 |
struct string_builder *cache_sb) |
| 3059 |
{ |
| 3060 |
struct drgn_program *prog = module->prog; |
| 3061 |
|
| 3062 |
if (!string_builder_appendf(cache_sb, "/%s/%s", build_id_str, |
| 3063 |
debug ? "debuginfo" : "executable") |
| 3064 |
|| !string_builder_null_terminate(cache_sb)) |
| 3065 |
return &drgn_enomem; |
| 3066 |
|
| 3067 |
prog->dbinfo.debuginfod_current_name = module->name; |
| 3068 |
if (supplementary) |
| 3069 |
prog->dbinfo.debuginfod_current_type = " supplementary debug info"; |
| 3070 |
else if (debug) |
| 3071 |
prog->dbinfo.debuginfod_current_type = " debug info"; |
| 3072 |
else |
| 3073 |
prog->dbinfo.debuginfod_current_type = ""; |
| 3074 |
prog->dbinfo.debuginfod_have_url = false; |
| 3075 |
prog->dbinfo.logged_debuginfod_progress = false; |
| 3076 |
bool restore_sigaction = drgn_prepare_debuginfod_find(prog); |
| 3077 |
char *path; |
| 3078 |
auto find = debug |
| 3079 |
? drgn_debuginfod_find_debuginfo |
| 3080 |
: drgn_debuginfod_find_executable; |
| 3081 |
int fd = find(prog->dbinfo.debuginfod_client, |
| 3082 |
(const unsigned char *)build_id_str, 0, &path); |
| 3083 |
drgn_finish_debuginfod_find(restore_sigaction); |
| 3084 |
if (fd == -ENOENT && drgn_cancel_debuginfod) { |
| 3085 |
// Before elfutils commit 5527216460c6 ("debuginfod-client.c: |
| 3086 |
// Skip empty file creation for cancelled queries") (in elfutils |
| 3087 |
// 0.190), libdebuginfod has a nasty bug that causes it to cache |
| 3088 |
// a cancelled download as a negative hit. Work around it by |
| 3089 |
// deleting the cache file. |
| 3090 |
unlink(cache_sb->str); |
| 3091 |
return drgn_error_create_os("download cancelled", EINTR, NULL); |
| 3092 |
} |
| 3093 |
drgn_log_debuginfod_progress(prog->dbinfo.debuginfod_client, -1, fd); |
| 3094 |
if (fd >= 0) { |
| 3095 |
struct drgn_error *err = |
| 3096 |
drgn_module_try_file(module, path, fd, true); |
| 3097 |
free(path); |
| 3098 |
if (err) |
| 3099 |
return err; |
| 3100 |
} |
| 3101 |
return NULL; |
| 3102 |
} |
| 3103 |
|
| 3104 |
static struct drgn_error * |
| 3105 |
drgn_module_try_supplementary_file_from_debuginfod(struct drgn_module *module, |
| 3106 |
struct string_builder *cache_sb) |
| 3107 |
{ |
| 3108 |
if (drgn_module_wanted_supplementary_debug_file(module, NULL, NULL, |
| 3109 |
NULL, NULL) |
| 3110 |
!= DRGN_SUPPLEMENTARY_FILE_GNU_DEBUGALTLINK) |
| 3111 |
return NULL; |
| 3112 |
const char *gnu_debugaltlink_build_id_str = |
| 3113 |
module->wanted_supplementary_debug_file->checksum_str; |
| 3114 |
return drgn_module_try_file_from_debuginfod(module, |
| 3115 |
gnu_debugaltlink_build_id_str, |
| 3116 |
true, true, cache_sb); |
| 3117 |
} |
| 3118 |
|
| 3119 |
static struct drgn_error * |
| 3120 |
drgn_debuginfod_find(struct drgn_module * const *modules, size_t num_modules, |
| 3121 |
void *arg) |
| 3122 |
{ |
| 3123 |
struct drgn_error *err; |
| 3124 |
struct drgn_program *prog = arg; |
| 3125 |
|
| 3126 |
if (!prog->dbinfo.debuginfod_client) { |
| 3127 |
prog->dbinfo.debuginfod_client = drgn_debuginfod_begin(); |
| 3128 |
if (!prog->dbinfo.debuginfod_client) { |
| 3129 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3130 |
"couldn't create debuginfod client session"); |
| 3131 |
} |
| 3132 |
drgn_debuginfod_set_progressfn(prog->dbinfo.debuginfod_client, |
| 3133 |
drgn_debuginfod_progressfn); |
| 3134 |
#if _ELFUTILS_PREREQ(0, 179) |
| 3135 |
drgn_debuginfod_set_user_data(prog->dbinfo.debuginfod_client, |
| 3136 |
prog); |
| 3137 |
#endif |
| 3138 |
} |
| 3139 |
|
| 3140 |
STRING_BUILDER(sb); |
| 3141 |
const char *env; |
| 3142 |
if ((env = getenv("DEBUGINFOD_CACHE_PATH"))) { |
| 3143 |
if (!string_builder_append(&sb, env)) |
| 3144 |
return &drgn_enomem; |
| 3145 |
} else { |
| 3146 |
env = getenv("HOME") ?: "/"; |
| 3147 |
if (!string_builder_append(&sb, env) |
| 3148 |
|| !string_builder_append(&sb, "/.debuginfod_client_cache") |
| 3149 |
|| !string_builder_null_terminate(&sb)) |
| 3150 |
return &drgn_enomem; |
| 3151 |
struct stat st; |
| 3152 |
if (stat(sb.str, &st) < 0) { |
| 3153 |
sb.len = 0; |
| 3154 |
if ((env = getenv("XDG_CACHE_HOME"))) { |
| 3155 |
if (!string_builder_append(&sb, env) |
| 3156 |
|| !string_builder_append(&sb, |
| 3157 |
"/debuginfod_client")) |
| 3158 |
return &drgn_enomem; |
| 3159 |
} else if (!string_builder_append(&sb, |
| 3160 |
getenv("HOME") ?: "/") |
| 3161 |
|| !string_builder_append(&sb, |
| 3162 |
"/.cache/debuginfod_client")) { |
| 3163 |
return &drgn_enomem; |
| 3164 |
} |
| 3165 |
} |
| 3166 |
} |
| 3167 |
|
| 3168 |
size_t cache_dir_len = sb.len; |
| 3169 |
for (size_t i = 0; i < num_modules; i++) { |
| 3170 |
struct drgn_module *module = modules[i]; |
| 3171 |
const char *build_id_str = |
| 3172 |
drgn_module_build_id(module, NULL, NULL); |
| 3173 |
if (!build_id_str) { |
| 3174 |
drgn_module_try_files_log(module, "can't query debuginfod for"); |
| 3175 |
continue; |
| 3176 |
} |
| 3177 |
|
| 3178 |
drgn_module_try_files_log(module, "querying debuginfod for"); |
| 3179 |
|
| 3180 |
// If we need a supplementary file, try that first. |
| 3181 |
err = drgn_module_try_supplementary_file_from_debuginfod(module, |
| 3182 |
&sb); |
| 3183 |
if (err) |
| 3184 |
return err; |
| 3185 |
sb.len = cache_dir_len; |
| 3186 |
|
| 3187 |
// If we need the debug file (including if we needed a |
| 3188 |
// gnu_debugaltlink file and didn't find it), try that next. |
| 3189 |
if (drgn_module_wants_debug_file(module)) { |
| 3190 |
uint64_t orig_supplementary_file_generation = |
| 3191 |
prog->dbinfo.supplementary_file_generation; |
| 3192 |
err = drgn_module_try_file_from_debuginfod(module, |
| 3193 |
build_id_str, |
| 3194 |
true, false, |
| 3195 |
&sb); |
| 3196 |
if (err) |
| 3197 |
return err; |
| 3198 |
sb.len = cache_dir_len; |
| 3199 |
// If the wanted supplementary debug file changed, try |
| 3200 |
// finding it again. |
| 3201 |
if (drgn_module_wanted_supplementary_debug_file_is_new(module, |
| 3202 |
orig_supplementary_file_generation)) { |
| 3203 |
err = drgn_module_try_supplementary_file_from_debuginfod(module, |
| 3204 |
&sb); |
| 3205 |
if (err) |
| 3206 |
return err; |
| 3207 |
sb.len = cache_dir_len; |
| 3208 |
} |
| 3209 |
} |
| 3210 |
|
| 3211 |
if (drgn_module_wants_loaded_file(module)) { |
| 3212 |
err = drgn_module_try_file_from_debuginfod(module, |
| 3213 |
build_id_str, |
| 3214 |
false, false, |
| 3215 |
&sb); |
| 3216 |
if (err) |
| 3217 |
return err; |
| 3218 |
sb.len = cache_dir_len; |
| 3219 |
} |
| 3220 |
} |
| 3221 |
return NULL; |
| 3222 |
} |
| 3223 |
#endif // WITH_DEBUGINFOD |
| 3224 |
|
| 3225 |
LIBDRGN_PUBLIC |
| 3226 |
struct drgn_error *drgn_module_try_file(struct drgn_module *module, |
| 3227 |
const char *path, int fd, bool force) |
| 3228 |
{ |
| 3229 |
if (!drgn_module_wants_file(module)) { |
| 3230 |
drgn_log_debug(module->prog, "%s: ignoring unwanted file %s", |
| 3231 |
module->name, path); |
| 3232 |
if (fd >= 0) |
| 3233 |
close(fd); |
| 3234 |
return NULL; |
| 3235 |
} |
| 3236 |
drgn_module_try_files_log(module, "trying provided file as"); |
| 3237 |
return drgn_module_try_file_internal(module, path, fd, !force, NULL); |
| 3238 |
} |
| 3239 |
|
| 3240 |
LIBDRGN_PUBLIC |
| 3241 |
void drgn_module_iterator_destroy(struct drgn_module_iterator *it) |
| 3242 |
{ |
| 3243 |
if (it) { |
| 3244 |
if (it->destroy) |
| 3245 |
it->destroy(it); |
| 3246 |
else |
| 3247 |
free(it); |
| 3248 |
} |
| 3249 |
} |
| 3250 |
|
| 3251 |
LIBDRGN_PUBLIC struct drgn_program * |
| 3252 |
drgn_module_iterator_program(const struct drgn_module_iterator *it) |
| 3253 |
{ |
| 3254 |
return it->prog; |
| 3255 |
} |
| 3256 |
|
| 3257 |
LIBDRGN_PUBLIC |
| 3258 |
struct drgn_error *drgn_module_iterator_next(struct drgn_module_iterator *it, |
| 3259 |
struct drgn_module **ret, |
| 3260 |
bool *new_ret) |
| 3261 |
{ |
| 3262 |
if (!it->next) { |
| 3263 |
*ret = NULL; |
| 3264 |
return NULL; |
| 3265 |
} |
| 3266 |
struct drgn_error *err = it->next(it, ret, new_ret); |
| 3267 |
if (err || !*ret) |
| 3268 |
it->next = NULL; |
| 3269 |
return err; |
| 3270 |
} |
| 3271 |
|
| 3272 |
struct drgn_created_module_iterator { |
| 3273 |
struct drgn_module_iterator it; |
| 3274 |
struct drgn_module_table_iterator table_it; |
| 3275 |
struct drgn_module *next_module; |
| 3276 |
uint64_t generation; |
| 3277 |
bool yielded_main; |
| 3278 |
}; |
| 3279 |
|
| 3280 |
static struct drgn_error * |
| 3281 |
drgn_created_module_iterator_next(struct drgn_module_iterator *_it, |
| 3282 |
struct drgn_module **ret, |
| 3283 |
bool *new_ret) |
| 3284 |
{ |
| 3285 |
struct drgn_created_module_iterator *it = |
| 3286 |
container_of(_it, struct drgn_created_module_iterator, it); |
| 3287 |
struct drgn_debug_info *dbinfo = &it->it.prog->dbinfo; |
| 3288 |
|
| 3289 |
if (!it->yielded_main) { |
| 3290 |
it->yielded_main = true; |
| 3291 |
it->table_it = drgn_module_table_first(&dbinfo->modules); |
| 3292 |
it->generation = dbinfo->modules_generation; |
| 3293 |
if (dbinfo->main_module) { |
| 3294 |
*ret = dbinfo->main_module; |
| 3295 |
if (new_ret) |
| 3296 |
*new_ret = false; |
| 3297 |
return NULL; |
| 3298 |
} |
| 3299 |
} |
| 3300 |
|
| 3301 |
if (it->generation != dbinfo->modules_generation) { |
| 3302 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3303 |
"modules changed during iteration"); |
| 3304 |
} |
| 3305 |
|
| 3306 |
for (;;) { |
| 3307 |
if (!it->next_module) { |
| 3308 |
if (it->table_it.entry) { |
| 3309 |
it->next_module = *it->table_it.entry; |
| 3310 |
it->table_it = drgn_module_table_next(it->table_it); |
| 3311 |
} else { |
| 3312 |
*ret = NULL; |
| 3313 |
return NULL; |
| 3314 |
} |
| 3315 |
} |
| 3316 |
if (it->next_module == dbinfo->main_module) { |
| 3317 |
it->next_module = it->next_module->next_same_name; |
| 3318 |
} else { |
| 3319 |
*ret = it->next_module; |
| 3320 |
if (new_ret) |
| 3321 |
*new_ret = false; |
| 3322 |
it->next_module = it->next_module->next_same_name; |
| 3323 |
return NULL; |
| 3324 |
} |
| 3325 |
} |
| 3326 |
} |
| 3327 |
|
| 3328 |
LIBDRGN_PUBLIC struct drgn_error * |
| 3329 |
drgn_created_module_iterator_create(struct drgn_program *prog, |
| 3330 |
struct drgn_module_iterator **ret) |
| 3331 |
{ |
| 3332 |
struct drgn_created_module_iterator *it = calloc(1, sizeof(*it)); |
| 3333 |
if (!it) |
| 3334 |
return &drgn_enomem; |
| 3335 |
drgn_module_iterator_init(&it->it, prog, NULL, |
| 3336 |
drgn_created_module_iterator_next); |
| 3337 |
*ret = &it->it; |
| 3338 |
return NULL; |
| 3339 |
} |
| 3340 |
|
| 3341 |
struct drgn_mapped_file { |
| 3342 |
const char *path; |
| 3343 |
// Mapped address range containing file offset 0. This is used to find |
| 3344 |
// the file header. |
| 3345 |
uint64_t offset0_vaddr, offset0_size; |
| 3346 |
}; |
| 3347 |
|
| 3348 |
static struct drgn_mapped_file *drgn_mapped_file_create(const char *path) |
| 3349 |
{ |
| 3350 |
struct drgn_mapped_file *file = calloc(1, sizeof(*file)); |
| 3351 |
if (file) |
| 3352 |
file->path = path; |
| 3353 |
return file; |
| 3354 |
} |
| 3355 |
|
| 3356 |
static void drgn_mapped_file_destroy(struct drgn_mapped_file *file) |
| 3357 |
{ |
| 3358 |
free(file); |
| 3359 |
} |
| 3360 |
|
| 3361 |
struct drgn_mapped_file_segment { |
| 3362 |
uint64_t start; |
| 3363 |
uint64_t end; |
| 3364 |
uint64_t file_offset; |
| 3365 |
struct drgn_mapped_file *file; |
| 3366 |
}; |
| 3367 |
|
| 3368 |
DEFINE_VECTOR(drgn_mapped_file_segment_vector, struct drgn_mapped_file_segment); |
| 3369 |
|
| 3370 |
struct drgn_mapped_file_segments { |
| 3371 |
struct drgn_mapped_file_segment_vector vector; |
| 3372 |
// Whether the segments are already sorted by start address. This should |
| 3373 |
// always be true for both /proc/$pid/maps and NT_FILE, but we check and |
| 3374 |
// sort afterwards if not just in case. |
| 3375 |
bool sorted; |
| 3376 |
}; |
| 3377 |
|
| 3378 |
#define DRGN_MAPPED_FILE_SEGMENTS_INIT { VECTOR_INIT, true } |
| 3379 |
|
| 3380 |
static void drgn_mapped_file_segments_abort(struct drgn_mapped_file_segments *segments) |
| 3381 |
{ |
| 3382 |
drgn_mapped_file_segment_vector_deinit(&segments->vector); |
| 3383 |
} |
| 3384 |
|
| 3385 |
static struct drgn_error * |
| 3386 |
drgn_add_mapped_file_segment(struct drgn_mapped_file_segments *segments, |
| 3387 |
uint64_t start, uint64_t end, uint64_t file_offset, |
| 3388 |
struct drgn_mapped_file *file) |
| 3389 |
{ |
| 3390 |
assert(start < end); |
| 3391 |
if (file_offset == 0 && file->offset0_size == 0) { |
| 3392 |
file->offset0_vaddr = start; |
| 3393 |
file->offset0_size = end - start; |
| 3394 |
} |
| 3395 |
if (!drgn_mapped_file_segment_vector_empty(&segments->vector)) { |
| 3396 |
struct drgn_mapped_file_segment *last = |
| 3397 |
drgn_mapped_file_segment_vector_last(&segments->vector); |
| 3398 |
// If the last segment is from the same file and contiguous with |
| 3399 |
// this one, merge into that one. |
| 3400 |
if (file == last->file && start == last->end |
| 3401 |
&& file_offset == last->file_offset + (last->end - last->start)) { |
| 3402 |
last->end = end; |
| 3403 |
return NULL; |
| 3404 |
} |
| 3405 |
if (start < last->start) |
| 3406 |
segments->sorted = false; |
| 3407 |
} |
| 3408 |
struct drgn_mapped_file_segment *entry = |
| 3409 |
drgn_mapped_file_segment_vector_append_entry(&segments->vector); |
| 3410 |
if (!entry) |
| 3411 |
return &drgn_enomem; |
| 3412 |
entry->start = start; |
| 3413 |
entry->end = end; |
| 3414 |
entry->file_offset = file_offset; |
| 3415 |
entry->file = file; |
| 3416 |
return NULL; |
| 3417 |
} |
| 3418 |
|
| 3419 |
enum { |
| 3420 |
// Yield main module next. |
| 3421 |
USERSPACE_LOADED_MODULE_ITERATOR_STATE_MAIN, |
| 3422 |
// Yield vDSO module next. |
| 3423 |
USERSPACE_LOADED_MODULE_ITERATOR_STATE_VDSO, |
| 3424 |
// Get first link_map from r_debug next. |
| 3425 |
USERSPACE_LOADED_MODULE_ITERATOR_STATE_R_DEBUG, |
| 3426 |
// Yield module from link_map list next. |
| 3427 |
USERSPACE_LOADED_MODULE_ITERATOR_STATE_LINK_MAP, |
| 3428 |
// States after this are the same as |
| 3429 |
// USERSPACE_LOADED_MODULE_ITERATOR_STATE_LINK_MAP but also count how |
| 3430 |
// many link_map entries we've iterated. |
| 3431 |
}; |
| 3432 |
|
| 3433 |
// Arbitrary limit on the number iterations to make through the link_map list in |
| 3434 |
// order to avoid getting stuck in a cycle. |
| 3435 |
static const int MAX_LINK_MAP_LIST_ITERATIONS = 10000; |
| 3436 |
|
| 3437 |
struct userspace_loaded_module_iterator { |
| 3438 |
struct drgn_module_iterator it; |
| 3439 |
int state; |
| 3440 |
bool read_main_phdrs; |
| 3441 |
bool have_main_dyn; |
| 3442 |
bool have_vdso_dyn; |
| 3443 |
|
| 3444 |
struct drgn_mapped_file_segment *file_segments; |
| 3445 |
size_t num_file_segments; |
| 3446 |
|
| 3447 |
uint64_t main_phoff; |
| 3448 |
uint64_t main_bias; |
| 3449 |
uint64_t main_dyn_vaddr; |
| 3450 |
uint64_t main_dyn_memsz; |
| 3451 |
uint64_t vdso_dyn_vaddr; |
| 3452 |
uint64_t link_map; |
| 3453 |
|
| 3454 |
// Temporary buffer for reading program headers. |
| 3455 |
void *phdrs_buf; |
| 3456 |
size_t phdrs_buf_capacity; |
| 3457 |
|
| 3458 |
// Temporary buffer for reading segment contents. |
| 3459 |
void *segment_buf; |
| 3460 |
size_t segment_buf_capacity; |
| 3461 |
}; |
| 3462 |
|
| 3463 |
static void |
| 3464 |
userspace_loaded_module_iterator_deinit(struct userspace_loaded_module_iterator *it) |
| 3465 |
{ |
| 3466 |
free(it->segment_buf); |
| 3467 |
free(it->phdrs_buf); |
| 3468 |
free(it->file_segments); |
| 3469 |
} |
| 3470 |
|
| 3471 |
static inline int drgn_mapped_file_segment_compare(const void *_a, |
| 3472 |
const void *_b) |
| 3473 |
{ |
| 3474 |
const struct drgn_mapped_file_segment *a = _a; |
| 3475 |
const struct drgn_mapped_file_segment *b = _b; |
| 3476 |
return (a->start > b->start) - (a->start < b->start); |
| 3477 |
} |
| 3478 |
|
| 3479 |
static void |
| 3480 |
userspace_loaded_module_iterator_set_file_segments(struct userspace_loaded_module_iterator *it, |
| 3481 |
struct drgn_mapped_file_segments *segments) |
| 3482 |
{ |
| 3483 |
// Don't bother shrinking to fit since this is short-lived. |
| 3484 |
drgn_mapped_file_segment_vector_steal(&segments->vector, |
| 3485 |
&it->file_segments, |
| 3486 |
&it->num_file_segments); |
| 3487 |
if (!segments->sorted) { |
| 3488 |
qsort(it->file_segments, it->num_file_segments, |
| 3489 |
sizeof(it->file_segments[0]), |
| 3490 |
drgn_mapped_file_segment_compare); |
| 3491 |
} |
| 3492 |
} |
| 3493 |
|
| 3494 |
static struct drgn_mapped_file_segment * |
| 3495 |
find_mapped_file_segment(struct userspace_loaded_module_iterator *it, |
| 3496 |
uint64_t address) |
| 3497 |
{ |
| 3498 |
#define less_than_start(a, b) (*(a) < (b)->start) |
| 3499 |
size_t i = binary_search_gt(it->file_segments, it->num_file_segments, |
| 3500 |
&address, less_than_start); |
| 3501 |
#undef less_than_start |
| 3502 |
if (i == 0 || address >= it->file_segments[i - 1].end) |
| 3503 |
return NULL; |
| 3504 |
return &it->file_segments[i - 1]; |
| 3505 |
} |
| 3506 |
|
| 3507 |
static struct drgn_error * |
| 3508 |
userspace_loaded_module_iterator_read_ehdr(struct userspace_loaded_module_iterator *it, |
| 3509 |
uint64_t address, GElf_Ehdr *ret) |
| 3510 |
{ |
| 3511 |
struct drgn_error *err; |
| 3512 |
struct drgn_program *prog = it->it.prog; |
| 3513 |
|
| 3514 |
err = drgn_program_read_memory(prog, ret, address, sizeof(*ret), false); |
| 3515 |
if (err && err->code == DRGN_ERROR_FAULT) { |
| 3516 |
drgn_log_debug(prog, |
| 3517 |
"couldn't read ELF header at 0x%" PRIx64 ": %s", |
| 3518 |
err->address, err->message); |
| 3519 |
drgn_error_destroy(err); |
| 3520 |
return &drgn_not_found; |
| 3521 |
} else if (err) { |
| 3522 |
return err; |
| 3523 |
} |
| 3524 |
if (memcmp(ret->e_ident, ELFMAG, SELFMAG) != 0) { |
| 3525 |
drgn_log_debug(prog, "invalid ELF header magic"); |
| 3526 |
return &drgn_not_found; |
| 3527 |
} |
| 3528 |
if (ret->e_ident[EI_CLASS] != |
| 3529 |
(drgn_platform_is_64_bit(&prog->platform) |
| 3530 |
? ELFCLASS64 : ELFCLASS32)) { |
| 3531 |
drgn_log_debug(prog, |
| 3532 |
"ELF header class (%u) does not match program", |
| 3533 |
ret->e_ident[EI_CLASS]); |
| 3534 |
return &drgn_not_found; |
| 3535 |
} |
| 3536 |
if (ret->e_ident[EI_DATA] != |
| 3537 |
(drgn_platform_is_little_endian(&prog->platform) |
| 3538 |
? ELFDATA2LSB : ELFDATA2MSB)) { |
| 3539 |
drgn_log_debug(prog, |
| 3540 |
"ELF header data encoding (%u) does not match program", |
| 3541 |
ret->e_ident[EI_DATA]); |
| 3542 |
return &drgn_not_found; |
| 3543 |
} |
| 3544 |
#define visit_elf_ehdr_members(visit_scalar_member, visit_raw_member) do { \ |
| 3545 |
visit_raw_member(e_ident); \ |
| 3546 |
visit_scalar_member(e_type); \ |
| 3547 |
visit_scalar_member(e_machine); \ |
| 3548 |
visit_scalar_member(e_version); \ |
| 3549 |
visit_scalar_member(e_entry); \ |
| 3550 |
visit_scalar_member(e_phoff); \ |
| 3551 |
visit_scalar_member(e_shoff); \ |
| 3552 |
visit_scalar_member(e_flags); \ |
| 3553 |
visit_scalar_member(e_ehsize); \ |
| 3554 |
visit_scalar_member(e_phentsize); \ |
| 3555 |
visit_scalar_member(e_phnum); \ |
| 3556 |
visit_scalar_member(e_shentsize); \ |
| 3557 |
visit_scalar_member(e_shnum); \ |
| 3558 |
visit_scalar_member(e_shstrndx); \ |
| 3559 |
} while (0) |
| 3560 |
deserialize_struct64_inplace(ret, Elf32_Ehdr, visit_elf_ehdr_members, |
| 3561 |
drgn_platform_is_64_bit(&prog->platform), |
| 3562 |
drgn_platform_bswap(&prog->platform)); |
| 3563 |
#undef visit_elf_ehdr_members |
| 3564 |
if (ret->e_phentsize != |
| 3565 |
(drgn_platform_is_64_bit(&prog->platform) |
| 3566 |
? sizeof(Elf64_Phdr) : sizeof(Elf32_Phdr))) { |
| 3567 |
drgn_log_debug(prog, |
| 3568 |
"ELF program header entry size (%u) does not match class", |
| 3569 |
ret->e_phentsize); |
| 3570 |
return &drgn_not_found; |
| 3571 |
} |
| 3572 |
return NULL; |
| 3573 |
} |
| 3574 |
|
| 3575 |
static struct drgn_error * |
| 3576 |
userspace_loaded_module_iterator_read_phdrs(struct userspace_loaded_module_iterator *it, |
| 3577 |
uint64_t address, uint16_t phnum) |
| 3578 |
{ |
| 3579 |
struct drgn_error *err; |
| 3580 |
struct drgn_program *prog = it->it.prog; |
| 3581 |
uint32_t phentsize = |
| 3582 |
(drgn_platform_is_64_bit(&prog->platform) |
| 3583 |
? sizeof(Elf64_Phdr) : sizeof(Elf32_Phdr)); |
| 3584 |
uint32_t phdrs_size = (uint32_t)phnum * phentsize; |
| 3585 |
if (phdrs_size > MAX_MEMORY_READ_FOR_DEBUG_INFO) { |
| 3586 |
drgn_log_debug(prog, |
| 3587 |
"program header table is unreasonably large (%" PRIu32 " bytes); ignoring", |
| 3588 |
phdrs_size); |
| 3589 |
return &drgn_not_found; |
| 3590 |
} |
| 3591 |
if (!alloc_or_reuse(&it->phdrs_buf, &it->phdrs_buf_capacity, |
| 3592 |
phdrs_size)) |
| 3593 |
return &drgn_enomem; |
| 3594 |
err = drgn_program_read_memory(prog, it->phdrs_buf, address, phdrs_size, |
| 3595 |
false); |
| 3596 |
if (err && err->code == DRGN_ERROR_FAULT) { |
| 3597 |
drgn_log_debug(prog, |
| 3598 |
"couldn't read program header table at 0x%" PRIx64 ": %s", |
| 3599 |
err->address, err->message); |
| 3600 |
drgn_error_destroy(err); |
| 3601 |
return &drgn_not_found; |
| 3602 |
} |
| 3603 |
return err; |
| 3604 |
} |
| 3605 |
|
| 3606 |
static void |
| 3607 |
userspace_loaded_module_iterator_phdr(struct userspace_loaded_module_iterator *it, |
| 3608 |
size_t i, GElf_Phdr *ret) |
| 3609 |
{ |
| 3610 |
struct drgn_program *prog = it->it.prog; |
| 3611 |
size_t phentsize = |
| 3612 |
(drgn_platform_is_64_bit(&prog->platform) |
| 3613 |
? sizeof(Elf64_Phdr) : sizeof(Elf32_Phdr)); |
| 3614 |
#define visit_phdr_members(visit_scalar_member, visit_raw_member) do { \ |
| 3615 |
visit_scalar_member(p_type); \ |
| 3616 |
visit_scalar_member(p_flags); \ |
| 3617 |
visit_scalar_member(p_offset); \ |
| 3618 |
visit_scalar_member(p_vaddr); \ |
| 3619 |
visit_scalar_member(p_paddr); \ |
| 3620 |
visit_scalar_member(p_filesz); \ |
| 3621 |
visit_scalar_member(p_memsz); \ |
| 3622 |
visit_scalar_member(p_align); \ |
| 3623 |
} while (0) |
| 3624 |
deserialize_struct64(ret, Elf32_Phdr, visit_phdr_members, |
| 3625 |
(char *)it->phdrs_buf + i * phentsize, |
| 3626 |
drgn_platform_is_64_bit(&prog->platform), |
| 3627 |
drgn_platform_bswap(&prog->platform)); |
| 3628 |
#undef visit_phdr_members |
| 3629 |
} |
| 3630 |
|
| 3631 |
static struct drgn_error * |
| 3632 |
userspace_loaded_module_iterator_read_dynamic(struct userspace_loaded_module_iterator *it, |
| 3633 |
uint64_t address, uint64_t size, |
| 3634 |
size_t *num_dyn_ret) |
| 3635 |
{ |
| 3636 |
struct drgn_error *err; |
| 3637 |
struct drgn_program *prog = it->it.prog; |
| 3638 |
|
| 3639 |
if (size > MAX_MEMORY_READ_FOR_DEBUG_INFO) { |
| 3640 |
drgn_log_debug(prog, |
| 3641 |
"dynamic section is unreasonably large (%" PRIu64 " bytes); ignoring", |
| 3642 |
size); |
| 3643 |
return &drgn_not_found; |
| 3644 |
} |
| 3645 |
size_t dyn_size = |
| 3646 |
(drgn_platform_is_64_bit(&prog->platform) |
| 3647 |
? sizeof(Elf64_Dyn) : sizeof(Elf32_Dyn)); |
| 3648 |
uint64_t num_dyn = size / dyn_size; |
| 3649 |
*num_dyn_ret = num_dyn; |
| 3650 |
if (num_dyn == 0) |
| 3651 |
return NULL; |
| 3652 |
|
| 3653 |
if (!alloc_or_reuse(&it->segment_buf, &it->segment_buf_capacity, |
| 3654 |
num_dyn * dyn_size)) |
| 3655 |
return &drgn_enomem; |
| 3656 |
err = drgn_program_read_memory(prog, it->segment_buf, address, |
| 3657 |
num_dyn * dyn_size, false); |
| 3658 |
if (err && err->code == DRGN_ERROR_FAULT) { |
| 3659 |
drgn_log_debug(prog, |
| 3660 |
"couldn't read dynamic section at 0x%" PRIx64 ": %s", |
| 3661 |
err->address, err->message); |
| 3662 |
drgn_error_destroy(err); |
| 3663 |
return &drgn_not_found; |
| 3664 |
} |
| 3665 |
return err; |
| 3666 |
} |
| 3667 |
|
| 3668 |
static void |
| 3669 |
userspace_loaded_module_iterator_dyn(struct userspace_loaded_module_iterator *it, |
| 3670 |
size_t i, GElf_Dyn *ret) |
| 3671 |
{ |
| 3672 |
struct drgn_program *prog = it->it.prog; |
| 3673 |
size_t dyn_size = |
| 3674 |
(drgn_platform_is_64_bit(&prog->platform) |
| 3675 |
? sizeof(Elf64_Dyn) : sizeof(Elf32_Dyn)); |
| 3676 |
#define visit_elf_dyn_members(visit_scalar_member, visit_raw_member) do { \ |
| 3677 |
visit_scalar_member(d_tag); \ |
| 3678 |
visit_scalar_member(d_un.d_val); \ |
| 3679 |
} while (0) |
| 3680 |
deserialize_struct64(ret, Elf32_Dyn, visit_elf_dyn_members, |
| 3681 |
(char *)it->segment_buf + i * dyn_size, |
| 3682 |
drgn_platform_is_64_bit(&prog->platform), |
| 3683 |
drgn_platform_bswap(&prog->platform)); |
| 3684 |
#undef visit_elf_dyn_members |
| 3685 |
} |
| 3686 |
|
| 3687 |
static struct drgn_error * |
| 3688 |
userspace_loaded_module_iterator_read_main_phdrs(struct userspace_loaded_module_iterator *it) |
| 3689 |
{ |
| 3690 |
struct drgn_error *err; |
| 3691 |
struct drgn_program *prog = it->it.prog; |
| 3692 |
|
| 3693 |
// The main bias is the difference between AT_PHDR and the virtual |
| 3694 |
// address of the program headers in the ELF file. We determine the |
| 3695 |
// latter by finding the PT_LOAD segment containing e_phoff. We would |
| 3696 |
// use PT_PHDR instead, but static binaries usually don't have it, and |
| 3697 |
// we can't assume a bias of 0 for static PIE binaries. |
| 3698 |
// |
| 3699 |
// If we couldn't find the file offset of the program headers, we can't |
| 3700 |
// find anything else. |
| 3701 |
if (it->main_phoff == 0) |
| 3702 |
return NULL; |
| 3703 |
|
| 3704 |
drgn_log_debug(prog, "reading program header table from AT_PHDR"); |
| 3705 |
|
| 3706 |
err = userspace_loaded_module_iterator_read_phdrs(it, |
| 3707 |
prog->auxv.at_phdr, |
| 3708 |
prog->auxv.at_phnum); |
| 3709 |
if (err == &drgn_not_found) |
| 3710 |
return NULL; |
| 3711 |
else if (err) |
| 3712 |
return err; |
| 3713 |
|
| 3714 |
// Silence -Wmaybe-uninitialized false positives on dyn_vaddr and |
| 3715 |
// dyn_memsz last seen with GCC 9. |
| 3716 |
uint64_t phdr_vaddr, dyn_vaddr = 0, dyn_memsz = 0; |
| 3717 |
bool have_phdr_vaddr = false, have_dyn = false; |
| 3718 |
for (uint16_t i = 0; i < prog->auxv.at_phnum; i++) { |
| 3719 |
GElf_Phdr phdr; |
| 3720 |
userspace_loaded_module_iterator_phdr(it, i, &phdr); |
| 3721 |
if (phdr.p_type == PT_LOAD && phdr.p_offset <= it->main_phoff |
| 3722 |
&& it->main_phoff < phdr.p_offset + phdr.p_filesz) { |
| 3723 |
drgn_log_debug(prog, |
| 3724 |
"found PT_LOAD containing program headers with p_vaddr 0x%" PRIx64 |
| 3725 |
" and p_offset 0x%" PRIx64, |
| 3726 |
phdr.p_vaddr, phdr.p_offset); |
| 3727 |
phdr_vaddr = it->main_phoff - phdr.p_offset + phdr.p_vaddr; |
| 3728 |
have_phdr_vaddr = true; |
| 3729 |
} else if (phdr.p_type == PT_DYNAMIC) { |
| 3730 |
drgn_log_debug(prog, |
| 3731 |
"found PT_DYNAMIC with p_vaddr 0x%" PRIx64 |
| 3732 |
" and p_memsz 0x%" PRIx64, |
| 3733 |
phdr.p_vaddr, phdr.p_memsz); |
| 3734 |
have_dyn = true; |
| 3735 |
dyn_vaddr = phdr.p_vaddr; |
| 3736 |
dyn_memsz = phdr.p_memsz; |
| 3737 |
} |
| 3738 |
} |
| 3739 |
if (have_phdr_vaddr) { |
| 3740 |
it->main_bias = prog->auxv.at_phdr - phdr_vaddr; |
| 3741 |
drgn_log_debug(prog, "main bias is 0x%" PRIx64, it->main_bias); |
| 3742 |
} else { |
| 3743 |
drgn_log_debug(prog, |
| 3744 |
"didn't find PT_LOAD containing program headers"); |
| 3745 |
return NULL; |
| 3746 |
} |
| 3747 |
if (have_dyn) { |
| 3748 |
it->have_main_dyn = true; |
| 3749 |
it->main_dyn_vaddr = dyn_vaddr + it->main_bias; |
| 3750 |
it->main_dyn_memsz = dyn_memsz; |
| 3751 |
drgn_log_debug(prog, "main dynamic section is at 0x%" PRIx64, |
| 3752 |
it->main_dyn_vaddr); |
| 3753 |
} else { |
| 3754 |
drgn_log_debug(prog, |
| 3755 |
"didn't find PT_DYNAMIC program header; probably statically linked"); |
| 3756 |
} |
| 3757 |
it->read_main_phdrs = true; |
| 3758 |
return NULL; |
| 3759 |
} |
| 3760 |
|
| 3761 |
static struct drgn_error * |
| 3762 |
identify_module_from_phdrs(struct userspace_loaded_module_iterator *it, |
| 3763 |
struct drgn_module *module, size_t phnum, |
| 3764 |
uint64_t bias) |
| 3765 |
{ |
| 3766 |
struct drgn_error *err; |
| 3767 |
struct drgn_program *prog = it->it.prog; |
| 3768 |
|
| 3769 |
uint64_t start = UINT64_MAX, end = 0; |
| 3770 |
for (size_t i = 0; i < phnum; i++) { |
| 3771 |
GElf_Phdr phdr; |
| 3772 |
userspace_loaded_module_iterator_phdr(it, i, &phdr); |
| 3773 |
if (phdr.p_type == PT_LOAD) { |
| 3774 |
// Like elf_address_range_from_min_and_max_phdr(). |
| 3775 |
start = min(start, phdr.p_vaddr + bias); |
| 3776 |
end = max(end, phdr.p_vaddr + phdr.p_memsz + bias); |
| 3777 |
} else if (phdr.p_type == PT_NOTE |
| 3778 |
&& module->build_id_len == 0) { |
| 3779 |
uint64_t note_size = min(phdr.p_filesz, phdr.p_memsz); |
| 3780 |
if (!note_size) |
| 3781 |
continue; |
| 3782 |
if (note_size > MAX_MEMORY_READ_FOR_DEBUG_INFO) { |
| 3783 |
drgn_log_debug(prog, |
| 3784 |
"note is unreasonably large (%" PRIu64 " bytes); ignoring", |
| 3785 |
note_size); |
| 3786 |
continue; |
| 3787 |
} |
| 3788 |
if (!alloc_or_reuse(&it->segment_buf, |
| 3789 |
&it->segment_buf_capacity, |
| 3790 |
note_size)) |
| 3791 |
return &drgn_enomem; |
| 3792 |
err = drgn_program_read_memory(prog, it->segment_buf, |
| 3793 |
phdr.p_vaddr + bias, |
| 3794 |
note_size, false); |
| 3795 |
if (err && err->code == DRGN_ERROR_FAULT) { |
| 3796 |
drgn_log_debug(prog, |
| 3797 |
"couldn't read note at 0x%" PRIx64 ": %s" |
| 3798 |
"; ignoring", |
| 3799 |
err->address, err->message); |
| 3800 |
drgn_error_destroy(err); |
| 3801 |
continue; |
| 3802 |
} else if (err) { |
| 3803 |
return err; |
| 3804 |
} |
| 3805 |
const void *build_id; |
| 3806 |
size_t build_id_len = |
| 3807 |
parse_gnu_build_id_from_notes(it->segment_buf, |
| 3808 |
note_size, |
| 3809 |
phdr.p_align == 8 ? |
| 3810 |
8 : 4, |
| 3811 |
drgn_platform_bswap(&prog->platform), |
| 3812 |
&build_id); |
| 3813 |
if (build_id_len > 0) { |
| 3814 |
err = drgn_module_set_build_id(module, build_id, |
| 3815 |
build_id_len); |
| 3816 |
if (err) |
| 3817 |
return err; |
| 3818 |
drgn_log_debug(prog, |
| 3819 |
"found build ID %s in note at 0x%" PRIx64, |
| 3820 |
module->build_id_str, |
| 3821 |
phdr.p_vaddr + bias |
| 3822 |
+ ((char *)build_id |
| 3823 |
- (char *)it->segment_buf)); |
| 3824 |
} |
| 3825 |
} |
| 3826 |
} |
| 3827 |
if (module->build_id_len == 0) { |
| 3828 |
drgn_log_debug(prog, |
| 3829 |
"couldn't find build ID from mapped program headers"); |
| 3830 |
} |
| 3831 |
if (start < end) { |
| 3832 |
err = drgn_module_set_address_range(module, start, end); |
| 3833 |
if (err) |
| 3834 |
return err; |
| 3835 |
drgn_log_debug(prog, |
| 3836 |
"got address range 0x%" PRIx64 "-0x%" PRIx64 " from mapped program headers", |
| 3837 |
start, end); |
| 3838 |
} else { |
| 3839 |
drgn_log_debug(prog, |
| 3840 |
"couldn't find address range from mapped program headers"); |
| 3841 |
} |
| 3842 |
return NULL; |
| 3843 |
} |
| 3844 |
|
| 3845 |
static struct drgn_error * |
| 3846 |
userspace_loaded_module_iterator_yield_main(struct userspace_loaded_module_iterator *it, |
| 3847 |
struct drgn_module **ret, |
| 3848 |
bool *new_ret) |
| 3849 |
{ |
| 3850 |
struct drgn_error *err; |
| 3851 |
struct drgn_program *prog = it->it.prog; |
| 3852 |
|
| 3853 |
struct drgn_mapped_file_segment *segment = |
| 3854 |
find_mapped_file_segment(it, prog->auxv.at_phdr); |
| 3855 |
if (segment) { |
| 3856 |
// We don't need to read the file header to get e_phoff. Instead, |
| 3857 |
// determine it from the file mapping. |
| 3858 |
it->main_phoff = |
| 3859 |
segment->file_offset + (prog->auxv.at_phdr - segment->start); |
| 3860 |
drgn_log_debug(prog, |
| 3861 |
"AT_PHDR is mapped from file %s at offset 0x%" PRIx64, |
| 3862 |
segment->file->path, it->main_phoff); |
| 3863 |
} else { |
| 3864 |
drgn_log_debug(prog, |
| 3865 |
"couldn't find mapped file segment containing AT_PHDR"); |
| 3866 |
} |
| 3867 |
|
| 3868 |
_cleanup_(drgn_module_deletep) struct drgn_module *module = NULL; |
| 3869 |
bool new; |
| 3870 |
err = drgn_module_find_or_create_main(prog, |
| 3871 |
segment ? segment->file->path : "", |
| 3872 |
&module, &new); |
| 3873 |
if (err) |
| 3874 |
return err; |
| 3875 |
if (!new) { |
| 3876 |
*ret = no_cleanup_ptr(module); |
| 3877 |
if (new_ret) |
| 3878 |
*new_ret = new; |
| 3879 |
return NULL; |
| 3880 |
} |
| 3881 |
err = userspace_loaded_module_iterator_read_main_phdrs(it); |
| 3882 |
if (err) |
| 3883 |
return err; |
| 3884 |
if (it->read_main_phdrs) { |
| 3885 |
err = identify_module_from_phdrs(it, module, |
| 3886 |
prog->auxv.at_phnum, |
| 3887 |
it->main_bias); |
| 3888 |
if (err) |
| 3889 |
return err; |
| 3890 |
} |
| 3891 |
*ret = no_cleanup_ptr(module); |
| 3892 |
if (new_ret) |
| 3893 |
*new_ret = new; |
| 3894 |
return NULL; |
| 3895 |
} |
| 3896 |
|
| 3897 |
static struct drgn_error * |
| 3898 |
userspace_loaded_module_iterator_yield_vdso(struct userspace_loaded_module_iterator *it, |
| 3899 |
struct drgn_module **ret, |
| 3900 |
bool *new_ret) |
| 3901 |
{ |
| 3902 |
struct drgn_error *err; |
| 3903 |
struct drgn_program *prog = it->it.prog; |
| 3904 |
|
| 3905 |
if (!prog->auxv.at_sysinfo_ehdr) { |
| 3906 |
drgn_log_debug(prog, "no vDSO"); |
| 3907 |
no_vdso: |
| 3908 |
*ret = NULL; |
| 3909 |
return NULL; |
| 3910 |
} |
| 3911 |
|
| 3912 |
drgn_log_debug(prog, "reading vDSO ELF header from AT_SYSINFO_EHDR"); |
| 3913 |
GElf_Ehdr ehdr; |
| 3914 |
err = userspace_loaded_module_iterator_read_ehdr(it, |
| 3915 |
prog->auxv.at_sysinfo_ehdr, |
| 3916 |
&ehdr); |
| 3917 |
if (err == &drgn_not_found) |
| 3918 |
goto no_vdso; |
| 3919 |
else if (err) |
| 3920 |
return err; |
| 3921 |
|
| 3922 |
drgn_log_debug(prog, |
| 3923 |
"reading %" PRIu16 " program headers at 0x%" PRIx64, |
| 3924 |
ehdr.e_phnum, prog->auxv.at_sysinfo_ehdr + ehdr.e_phoff); |
| 3925 |
|
| 3926 |
// It is effectively part of the ABI that the vDSO program headers are |
| 3927 |
// mapped at AT_SYSINFO_EHDR + e_phoff (see the Linux kernel's reference |
| 3928 |
// vDSO parser: vdso_init_from_sysinfo_ehdr() in |
| 3929 |
// tools/testing/selftests/vDSO/parse_vdso.c, glibc: setup_vdso() in |
| 3930 |
// elf/setup-vdso.h, and musl: __vdsosym() in src/internal/vdso.c). |
| 3931 |
err = userspace_loaded_module_iterator_read_phdrs(it, |
| 3932 |
prog->auxv.at_sysinfo_ehdr + ehdr.e_phoff, |
| 3933 |
ehdr.e_phnum); |
| 3934 |
if (err == &drgn_not_found) |
| 3935 |
goto no_vdso; |
| 3936 |
else if (err) |
| 3937 |
return err; |
| 3938 |
|
| 3939 |
// This is based on the Linux kernel's reference vDSO parser. |
| 3940 |
uint64_t bias = prog->auxv.at_sysinfo_ehdr; |
| 3941 |
// Silence -Wmaybe-uninitialized false positives on dyn_vaddr and |
| 3942 |
// dyn_memsz last seen with GCC 12. |
| 3943 |
uint64_t dyn_vaddr = 0, dyn_memsz = 0; |
| 3944 |
bool have_load = false, have_dyn = false; |
| 3945 |
for (size_t i = 0; i < ehdr.e_phnum; i++) { |
| 3946 |
GElf_Phdr phdr; |
| 3947 |
userspace_loaded_module_iterator_phdr(it, i, &phdr); |
| 3948 |
if (phdr.p_type == PT_LOAD && !have_load) { |
| 3949 |
drgn_log_debug(prog, |
| 3950 |
"found PT_LOAD with p_offset 0x%" PRIx64 |
| 3951 |
" and p_vaddr 0x%" PRIx64, |
| 3952 |
phdr.p_offset, phdr.p_vaddr); |
| 3953 |
have_load = true; |
| 3954 |
bias = prog->auxv.at_sysinfo_ehdr + phdr.p_offset - phdr.p_vaddr; |
| 3955 |
} else if (phdr.p_type == PT_DYNAMIC) { |
| 3956 |
drgn_log_debug(prog, |
| 3957 |
"found PT_DYNAMIC with p_offset 0x%" PRIx64 |
| 3958 |
" and p_memsz 0x%" PRIx64, |
| 3959 |
phdr.p_offset, phdr.p_memsz); |
| 3960 |
dyn_vaddr = prog->auxv.at_sysinfo_ehdr + phdr.p_offset; |
| 3961 |
dyn_memsz = phdr.p_memsz; |
| 3962 |
have_dyn = true; |
| 3963 |
} |
| 3964 |
} |
| 3965 |
if (!have_load) { |
| 3966 |
drgn_log_warning(prog, |
| 3967 |
"can't find vDSO: " |
| 3968 |
"no PT_LOAD header in vDSO program headers"); |
| 3969 |
goto no_vdso; |
| 3970 |
} |
| 3971 |
drgn_log_debug(prog, "vDSO bias is 0x%" PRIx64, bias); |
| 3972 |
if (!have_dyn) { |
| 3973 |
drgn_log_warning(prog, |
| 3974 |
"can't find vDSO: " |
| 3975 |
"no PT_DYNAMIC header in vDSO program headers"); |
| 3976 |
goto no_vdso; |
| 3977 |
} |
| 3978 |
it->vdso_dyn_vaddr = dyn_vaddr; |
| 3979 |
it->have_vdso_dyn = true; |
| 3980 |
|
| 3981 |
drgn_log_debug(prog, "reading vDSO dynamic section at 0x%" PRIx64, |
| 3982 |
dyn_vaddr); |
| 3983 |
size_t num_dyn; |
| 3984 |
err = userspace_loaded_module_iterator_read_dynamic(it, dyn_vaddr, |
| 3985 |
dyn_memsz, |
| 3986 |
&num_dyn); |
| 3987 |
if (err == &drgn_not_found) |
| 3988 |
goto no_vdso; |
| 3989 |
else if (err) |
| 3990 |
return err; |
| 3991 |
|
| 3992 |
// Silence -Wmaybe-uninitialized false positives on dt_strtab and |
| 3993 |
// dt_soname last seen with GCC 12. |
| 3994 |
uint64_t dt_strtab = 0, dt_soname = 0; |
| 3995 |
bool have_dt_strtab = false, have_dt_soname = false; |
| 3996 |
for (size_t i = 0; i < num_dyn; i++) { |
| 3997 |
GElf_Dyn dyn; |
| 3998 |
userspace_loaded_module_iterator_dyn(it, i, &dyn); |
| 3999 |
if (dyn.d_tag == DT_STRTAB) { |
| 4000 |
dt_strtab = dyn.d_un.d_ptr; |
| 4001 |
have_dt_strtab = true; |
| 4002 |
drgn_log_debug(prog, "found DT_STRTAB 0x%" PRIx64, |
| 4003 |
dt_strtab); |
| 4004 |
} else if (dyn.d_tag == DT_SONAME) { |
| 4005 |
dt_soname = dyn.d_un.d_val; |
| 4006 |
have_dt_soname = true; |
| 4007 |
drgn_log_debug(prog, "found DT_SONAME 0x%" PRIx64, |
| 4008 |
dt_soname); |
| 4009 |
} else if (dyn.d_tag == DT_NULL) { |
| 4010 |
break; |
| 4011 |
} |
| 4012 |
} |
| 4013 |
if (!have_dt_strtab || !have_dt_soname) { |
| 4014 |
drgn_log_warning(prog, |
| 4015 |
"can't find vDSO: " |
| 4016 |
"no %s%s%s entr%s in vDSO dynamic section", |
| 4017 |
have_dt_strtab ? "" : "DT_STRTAB", |
| 4018 |
have_dt_strtab || have_dt_soname ? "" : " or ", |
| 4019 |
have_dt_soname ? "" : "DT_SONAME", |
| 4020 |
have_dt_strtab || have_dt_soname ? "y" : "ies"); |
| 4021 |
goto no_vdso; |
| 4022 |
} |
| 4023 |
|
| 4024 |
_cleanup_free_ char *name = NULL; |
| 4025 |
err = drgn_program_read_c_string(prog, dt_strtab + bias + dt_soname, |
| 4026 |
false, SIZE_MAX, &name); |
| 4027 |
if (err && err->code == DRGN_ERROR_FAULT) { |
| 4028 |
drgn_log_warning(prog, |
| 4029 |
"can't find vDSO: " |
| 4030 |
"couldn't read soname at 0x%" PRIx64 ": %s", |
| 4031 |
err->address, err->message); |
| 4032 |
drgn_error_destroy(err); |
| 4033 |
goto no_vdso; |
| 4034 |
} else if (err) { |
| 4035 |
return err; |
| 4036 |
} |
| 4037 |
drgn_log_debug(prog, "read vDSO soname \"%s\"", name); |
| 4038 |
|
| 4039 |
_cleanup_(drgn_module_deletep) struct drgn_module *module = NULL; |
| 4040 |
bool new; |
| 4041 |
err = drgn_module_find_or_create_vdso(prog, name, dyn_vaddr, &module, |
| 4042 |
&new); |
| 4043 |
if (err) |
| 4044 |
return err; |
| 4045 |
if (!new) { |
| 4046 |
*ret = no_cleanup_ptr(module); |
| 4047 |
if (new_ret) |
| 4048 |
*new_ret = new; |
| 4049 |
return NULL; |
| 4050 |
} |
| 4051 |
|
| 4052 |
err = identify_module_from_phdrs(it, module, ehdr.e_phnum, bias); |
| 4053 |
if (err) |
| 4054 |
return err; |
| 4055 |
*ret = no_cleanup_ptr(module); |
| 4056 |
if (new_ret) |
| 4057 |
*new_ret = new; |
| 4058 |
return NULL; |
| 4059 |
} |
| 4060 |
|
| 4061 |
#define read_struct64(prog, struct64p, address, type32, visit_members) \ |
| 4062 |
read_struct64_impl(prog, struct64p, address, type32, visit_members, \ |
| 4063 |
PP_UNIQUE(prog), PP_UNIQUE(struct64p), \ |
| 4064 |
PP_UNIQUE(is_64_bit), PP_UNIQUE(err)) |
| 4065 |
#define read_struct64_impl(prog, struct64p, address, type32, visit_members, \ |
| 4066 |
unique_prog, unique_struct64, unique_is_64_bit, \ |
| 4067 |
unique_err) ({ \ |
| 4068 |
struct drgn_program *unique_prog = (prog); \ |
| 4069 |
__auto_type unique_struct64p = (struct64p); \ |
| 4070 |
static_assert(sizeof(*unique_struct64p) >= sizeof(type32), \ |
| 4071 |
"64-bit type is smaller than 32-bit type"); \ |
| 4072 |
const bool unique_is_64_bit = \ |
| 4073 |
drgn_platform_is_64_bit(&unique_prog->platform); \ |
| 4074 |
struct drgn_error *unique_err = \ |
| 4075 |
drgn_program_read_memory(unique_prog, unique_struct64p, \ |
| 4076 |
(address), \ |
| 4077 |
unique_is_64_bit \ |
| 4078 |
? sizeof(*unique_struct64p) \ |
| 4079 |
: sizeof(type32), false); \ |
| 4080 |
if (!unique_err) { \ |
| 4081 |
deserialize_struct64_inplace(unique_struct64p, type32, \ |
| 4082 |
visit_members, unique_is_64_bit, \ |
| 4083 |
drgn_platform_bswap(&unique_prog->platform));\ |
| 4084 |
} \ |
| 4085 |
unique_err; \ |
| 4086 |
}) |
| 4087 |
|
| 4088 |
static struct drgn_error * |
| 4089 |
userspace_get_link_map(struct userspace_loaded_module_iterator *it) |
| 4090 |
{ |
| 4091 |
struct drgn_error *err; |
| 4092 |
struct drgn_program *prog = it->it.prog; |
| 4093 |
|
| 4094 |
if (!it->read_main_phdrs) { |
| 4095 |
err = userspace_loaded_module_iterator_read_main_phdrs(it); |
| 4096 |
if (err) |
| 4097 |
return err; |
| 4098 |
} |
| 4099 |
if (!it->have_main_dyn) |
| 4100 |
return NULL; |
| 4101 |
|
| 4102 |
drgn_log_debug(prog, "reading main dynamic section"); |
| 4103 |
size_t num_dyn; |
| 4104 |
err = userspace_loaded_module_iterator_read_dynamic(it, |
| 4105 |
it->main_dyn_vaddr, |
| 4106 |
it->main_dyn_memsz, |
| 4107 |
&num_dyn); |
| 4108 |
if (err == &drgn_not_found) { |
| 4109 |
drgn_log_warning(prog, |
| 4110 |
"can't find shared libraries: " |
| 4111 |
"couldn't read main dynamic section"); |
| 4112 |
return NULL; |
| 4113 |
} else if (err) { |
| 4114 |
return err; |
| 4115 |
} |
| 4116 |
|
| 4117 |
GElf_Dyn dyn; |
| 4118 |
size_t i; |
| 4119 |
for (i = 0; i < num_dyn; i++) { |
| 4120 |
userspace_loaded_module_iterator_dyn(it, i, &dyn); |
| 4121 |
if (dyn.d_tag == DT_NULL) { |
| 4122 |
i = num_dyn; |
| 4123 |
break; |
| 4124 |
} |
| 4125 |
if (dyn.d_tag == DT_DEBUG) { |
| 4126 |
drgn_log_debug(prog, "found DT_DEBUG 0x%" PRIx64, |
| 4127 |
dyn.d_un.d_ptr); |
| 4128 |
break; |
| 4129 |
} |
| 4130 |
} |
| 4131 |
if (i >= num_dyn) { |
| 4132 |
drgn_log_warning(prog, |
| 4133 |
"can't find shared libraries: " |
| 4134 |
"no DT_DEBUG entry in main dynamic section"); |
| 4135 |
return NULL; |
| 4136 |
} |
| 4137 |
|
| 4138 |
struct drgn_r_debug { |
| 4139 |
int32_t r_version; |
| 4140 |
alignas(8) uint64_t r_map; |
| 4141 |
} r_debug; |
| 4142 |
struct drgn_r_debug32 { |
| 4143 |
int32_t r_version; |
| 4144 |
uint32_t r_map; |
| 4145 |
}; |
| 4146 |
#define visit_r_debug_members(visit_scalar_member, visit_raw_member) do { \ |
| 4147 |
visit_scalar_member(r_version); \ |
| 4148 |
visit_scalar_member(r_map); \ |
| 4149 |
} while (0) |
| 4150 |
err = read_struct64(prog, &r_debug, dyn.d_un.d_ptr, |
| 4151 |
struct drgn_r_debug32, visit_r_debug_members); |
| 4152 |
#undef visit_r_debug_members |
| 4153 |
if (err && err->code == DRGN_ERROR_FAULT) { |
| 4154 |
// Note: musl doesn't update DT_DEBUG for static PIE binaries |
| 4155 |
// compiled with GCC (as of musl v1.2.3 and GCC 13), so that |
| 4156 |
// case is known to fail here. |
| 4157 |
drgn_log_warning(prog, |
| 4158 |
"can't find shared libraries: " |
| 4159 |
"couldn't read r_debug at 0x%" PRIx64 ": %s", |
| 4160 |
err->address, err->message); |
| 4161 |
drgn_error_destroy(err); |
| 4162 |
return NULL; |
| 4163 |
} else if (err) { |
| 4164 |
return err; |
| 4165 |
} |
| 4166 |
drgn_log_debug(prog, |
| 4167 |
"read r_debug = { .r_version = %" PRId32 ", .r_map = 0x%" PRIx64 " }", |
| 4168 |
r_debug.r_version, r_debug.r_map); |
| 4169 |
|
| 4170 |
if (r_debug.r_version < 1) { |
| 4171 |
drgn_log_warning(prog, |
| 4172 |
"can't find shared libraries: " |
| 4173 |
"invalid r_debug.r_version %" PRId32, |
| 4174 |
r_debug.r_version); |
| 4175 |
return NULL; |
| 4176 |
} |
| 4177 |
it->link_map = r_debug.r_map; |
| 4178 |
return NULL; |
| 4179 |
} |
| 4180 |
|
| 4181 |
static struct drgn_error * |
| 4182 |
identify_module_from_link_map(struct userspace_loaded_module_iterator *it, |
| 4183 |
struct drgn_module *module, |
| 4184 |
struct drgn_mapped_file *file, uint64_t l_addr) |
| 4185 |
{ |
| 4186 |
struct drgn_error *err; |
| 4187 |
struct drgn_program *prog = it->it.prog; |
| 4188 |
|
| 4189 |
// Even if it is a 32-bit file, segments should be at least a page, so |
| 4190 |
// we should be able to read the 64-bit size. |
| 4191 |
if (file->offset0_size < sizeof(Elf64_Ehdr)) { |
| 4192 |
drgn_log_debug(prog, "didn't find mapped ELF header"); |
| 4193 |
return NULL; |
| 4194 |
} |
| 4195 |
|
| 4196 |
drgn_log_debug(prog, "reading ELF header at 0x%" PRIx64, |
| 4197 |
file->offset0_vaddr); |
| 4198 |
GElf_Ehdr ehdr; |
| 4199 |
err = userspace_loaded_module_iterator_read_ehdr(it, |
| 4200 |
file->offset0_vaddr, |
| 4201 |
&ehdr); |
| 4202 |
if (err == &drgn_not_found) |
| 4203 |
return NULL; |
| 4204 |
else if (err) |
| 4205 |
return err; |
| 4206 |
|
| 4207 |
drgn_log_debug(prog, |
| 4208 |
"reading %" PRIu16 " program headers from 0x%" PRIx64, |
| 4209 |
ehdr.e_phnum, file->offset0_vaddr + ehdr.e_phoff); |
| 4210 |
// e_phnum and e_phentsize are uint16_t, so this can't overflow. |
| 4211 |
uint32_t phdrs_size = |
| 4212 |
(uint32_t)ehdr.e_phnum * (uint32_t)ehdr.e_phentsize; |
| 4213 |
if (ehdr.e_phoff > file->offset0_size || |
| 4214 |
phdrs_size > file->offset0_size - ehdr.e_phoff) { |
| 4215 |
drgn_log_debug(prog, |
| 4216 |
"program header table is not mapped with ELF header"); |
| 4217 |
return NULL; |
| 4218 |
} |
| 4219 |
err = userspace_loaded_module_iterator_read_phdrs(it, |
| 4220 |
file->offset0_vaddr + ehdr.e_phoff, |
| 4221 |
ehdr.e_phnum); |
| 4222 |
if (err == &drgn_not_found) |
| 4223 |
return NULL; |
| 4224 |
else if (err) |
| 4225 |
return err; |
| 4226 |
|
| 4227 |
return identify_module_from_phdrs(it, module, ehdr.e_phnum, l_addr); |
| 4228 |
} |
| 4229 |
|
| 4230 |
// This is the public definition of struct link_map from glibc's link.h: |
| 4231 |
// |
| 4232 |
// struct link_map |
| 4233 |
// { |
| 4234 |
// /* These first few members are part of the protocol with the debugger. |
| 4235 |
// This is the same format used in SVR4. */ |
| 4236 |
// |
| 4237 |
// ElfW(Addr) l_addr; /* Difference between the address in the ELF |
| 4238 |
// file and the addresses in memory. */ |
| 4239 |
// char *l_name; /* Absolute file name object was found in. */ |
| 4240 |
// ElfW(Dyn) *l_ld; /* Dynamic section of the shared object. */ |
| 4241 |
// struct link_map *l_next, *l_prev; /* Chain of loaded objects. */ |
| 4242 |
// }; |
| 4243 |
// |
| 4244 |
// We don't need l_prev, so we exclude it from our definition. |
| 4245 |
struct drgn_link_map { |
| 4246 |
uint64_t l_addr; |
| 4247 |
uint64_t l_name; |
| 4248 |
uint64_t l_ld; |
| 4249 |
uint64_t l_next; |
| 4250 |
}; |
| 4251 |
struct drgn_link_map32 { |
| 4252 |
uint32_t l_addr; |
| 4253 |
uint32_t l_name; |
| 4254 |
uint32_t l_ld; |
| 4255 |
uint32_t l_next; |
| 4256 |
}; |
| 4257 |
|
| 4258 |
static struct drgn_error * |
| 4259 |
userspace_next_link_map(struct userspace_loaded_module_iterator *it, |
| 4260 |
struct drgn_link_map *ret, char **name_ret) |
| 4261 |
{ |
| 4262 |
struct drgn_error *err; |
| 4263 |
struct drgn_program *prog = it->it.prog; |
| 4264 |
|
| 4265 |
if (!it->link_map) { |
| 4266 |
drgn_log_debug(prog, "found end of link_map list"); |
| 4267 |
return &drgn_stop; |
| 4268 |
} |
| 4269 |
|
| 4270 |
if (it->state |
| 4271 |
>= USERSPACE_LOADED_MODULE_ITERATOR_STATE_LINK_MAP |
| 4272 |
+ MAX_LINK_MAP_LIST_ITERATIONS) { |
| 4273 |
drgn_log_warning(prog, |
| 4274 |
"can't find remaining shared libraries: " |
| 4275 |
"too many entries or cycle in link_map list"); |
| 4276 |
return &drgn_stop; |
| 4277 |
} |
| 4278 |
it->state++; |
| 4279 |
|
| 4280 |
#define visit_link_map_members(visit_scalar_member, visit_raw_member) do { \ |
| 4281 |
visit_scalar_member(l_addr); \ |
| 4282 |
visit_scalar_member(l_name); \ |
| 4283 |
visit_scalar_member(l_ld); \ |
| 4284 |
visit_scalar_member(l_next); \ |
| 4285 |
} while (0) |
| 4286 |
err = read_struct64(prog, ret, it->link_map, struct drgn_link_map32, |
| 4287 |
visit_link_map_members); |
| 4288 |
#undef visit_link_map_members |
| 4289 |
if (err && err->code == DRGN_ERROR_FAULT) { |
| 4290 |
drgn_log_warning(prog, |
| 4291 |
"can't find remaining shared libraries: " |
| 4292 |
"couldn't read next link_map at 0x%" PRIx64 ": %s", |
| 4293 |
err->address, err->message); |
| 4294 |
drgn_error_destroy(err); |
| 4295 |
return &drgn_stop; |
| 4296 |
} else if (err) { |
| 4297 |
return err; |
| 4298 |
} |
| 4299 |
|
| 4300 |
it->link_map = ret->l_next; |
| 4301 |
|
| 4302 |
err = drgn_program_read_c_string(prog, ret->l_name, false, SIZE_MAX, |
| 4303 |
name_ret); |
| 4304 |
if (err && err->code == DRGN_ERROR_FAULT) |
| 4305 |
*name_ret = NULL; |
| 4306 |
else if (err) |
| 4307 |
return err; |
| 4308 |
drgn_log_debug(prog, |
| 4309 |
"read link_map = { .l_addr = 0x%" PRIx64 ", .l_name = 0x%" PRIx64 "%s%s%s, .l_ld = 0x%" PRIx64 ", .l_next = 0x%" PRIx64 " }", |
| 4310 |
ret->l_addr, ret->l_name, *name_ret ? " = \"" : "", |
| 4311 |
*name_ret ? *name_ret : "", *name_ret ? "\"" : "", |
| 4312 |
ret->l_ld, ret->l_next); |
| 4313 |
if (err) { |
| 4314 |
drgn_log_debug(prog, |
| 4315 |
"couldn't read l_name at 0x%" PRIx64 ": %s" |
| 4316 |
"; skipping", |
| 4317 |
err->address, err->message); |
| 4318 |
drgn_error_destroy(err); |
| 4319 |
} |
| 4320 |
return NULL; |
| 4321 |
} |
| 4322 |
|
| 4323 |
static struct drgn_error * |
| 4324 |
yield_from_link_map(struct userspace_loaded_module_iterator *it, |
| 4325 |
struct drgn_module **ret, bool *new_ret) |
| 4326 |
{ |
| 4327 |
struct drgn_error *err; |
| 4328 |
struct drgn_program *prog = it->it.prog; |
| 4329 |
|
| 4330 |
for (;;) { |
| 4331 |
struct drgn_link_map link_map; |
| 4332 |
_cleanup_free_ char *name = NULL; |
| 4333 |
err = userspace_next_link_map(it, &link_map, &name); |
| 4334 |
if (err == &drgn_stop) { |
| 4335 |
*ret = NULL; |
| 4336 |
return NULL; |
| 4337 |
} else if (err) { |
| 4338 |
return err; |
| 4339 |
} |
| 4340 |
|
| 4341 |
if (link_map.l_ld == it->main_dyn_vaddr) { |
| 4342 |
drgn_log_debug(prog, |
| 4343 |
"l_ld matches main dynamic section; skipping"); |
| 4344 |
continue; |
| 4345 |
} |
| 4346 |
if (it->have_vdso_dyn && link_map.l_ld == it->vdso_dyn_vaddr) { |
| 4347 |
drgn_log_debug(prog, |
| 4348 |
"l_ld matches vDSO dynamic section; skipping"); |
| 4349 |
continue; |
| 4350 |
} |
| 4351 |
if (!name) |
| 4352 |
continue; |
| 4353 |
|
| 4354 |
_cleanup_(drgn_module_deletep) struct drgn_module *module = NULL; |
| 4355 |
bool new; |
| 4356 |
err = drgn_module_find_or_create_shared_library(prog, name, |
| 4357 |
link_map.l_ld, |
| 4358 |
&module, &new); |
| 4359 |
if (err) |
| 4360 |
return err; |
| 4361 |
if (!new) { |
| 4362 |
*ret = no_cleanup_ptr(module); |
| 4363 |
if (new_ret) |
| 4364 |
*new_ret = new; |
| 4365 |
return NULL; |
| 4366 |
} |
| 4367 |
|
| 4368 |
struct drgn_mapped_file_segment *segment = |
| 4369 |
find_mapped_file_segment(it, link_map.l_ld); |
| 4370 |
if (segment) { |
| 4371 |
err = identify_module_from_link_map(it, module, |
| 4372 |
segment->file, |
| 4373 |
link_map.l_addr); |
| 4374 |
if (err) |
| 4375 |
return err; |
| 4376 |
} else { |
| 4377 |
drgn_log_debug(prog, |
| 4378 |
"couldn't find mapped file segment containing l_ld"); |
| 4379 |
} |
| 4380 |
*ret = no_cleanup_ptr(module); |
| 4381 |
if (new_ret) |
| 4382 |
*new_ret = new; |
| 4383 |
return NULL; |
| 4384 |
} |
| 4385 |
} |
| 4386 |
|
| 4387 |
static struct drgn_error * |
| 4388 |
userspace_loaded_module_iterator_next(struct drgn_module_iterator *_it, |
| 4389 |
struct drgn_module **ret, |
| 4390 |
bool *new_ret) |
| 4391 |
{ |
| 4392 |
struct drgn_error *err; |
| 4393 |
struct userspace_loaded_module_iterator *it = |
| 4394 |
container_of(_it, struct userspace_loaded_module_iterator, it); |
| 4395 |
switch (it->state) { |
| 4396 |
case USERSPACE_LOADED_MODULE_ITERATOR_STATE_MAIN: |
| 4397 |
err = drgn_program_cache_auxv(it->it.prog); |
| 4398 |
if (err) |
| 4399 |
return err; |
| 4400 |
it->state = USERSPACE_LOADED_MODULE_ITERATOR_STATE_VDSO; |
| 4401 |
return userspace_loaded_module_iterator_yield_main(it, ret, |
| 4402 |
new_ret); |
| 4403 |
case USERSPACE_LOADED_MODULE_ITERATOR_STATE_VDSO: |
| 4404 |
it->state = USERSPACE_LOADED_MODULE_ITERATOR_STATE_R_DEBUG; |
| 4405 |
err = userspace_loaded_module_iterator_yield_vdso(it, ret, |
| 4406 |
new_ret); |
| 4407 |
if (err || *ret) |
| 4408 |
return err; |
| 4409 |
fallthrough; |
| 4410 |
case USERSPACE_LOADED_MODULE_ITERATOR_STATE_R_DEBUG: |
| 4411 |
it->state = USERSPACE_LOADED_MODULE_ITERATOR_STATE_LINK_MAP; |
| 4412 |
err = userspace_get_link_map(it); |
| 4413 |
if (err) |
| 4414 |
return err; |
| 4415 |
fallthrough; |
| 4416 |
default: |
| 4417 |
return yield_from_link_map(it, ret, new_ret); |
| 4418 |
} |
| 4419 |
} |
| 4420 |
|
| 4421 |
struct process_mapped_file_entry { |
| 4422 |
dev_t dev; |
| 4423 |
ino_t ino; |
| 4424 |
struct drgn_mapped_file *file; |
| 4425 |
}; |
| 4426 |
|
| 4427 |
struct process_mapped_file_key { |
| 4428 |
dev_t dev; |
| 4429 |
ino_t ino; |
| 4430 |
const char *path; |
| 4431 |
}; |
| 4432 |
|
| 4433 |
static struct process_mapped_file_key |
| 4434 |
process_mapped_file_entry_to_key(const struct process_mapped_file_entry *entry) |
| 4435 |
{ |
| 4436 |
return (struct process_mapped_file_key){ |
| 4437 |
.dev = entry->dev, |
| 4438 |
.ino = entry->ino, |
| 4439 |
.path = entry->file->path, |
| 4440 |
}; |
| 4441 |
} |
| 4442 |
|
| 4443 |
static struct hash_pair |
| 4444 |
process_mapped_file_key_hash_pair(const struct process_mapped_file_key *key) |
| 4445 |
{ |
| 4446 |
size_t hash = hash_combine(key->dev, key->ino); |
| 4447 |
hash = hash_combine(hash, hash_c_string(key->path)); |
| 4448 |
return hash_pair_from_avalanching_hash(hash); |
| 4449 |
} |
| 4450 |
|
| 4451 |
static bool process_mapped_file_key_eq(const struct process_mapped_file_key *a, |
| 4452 |
const struct process_mapped_file_key *b) |
| 4453 |
{ |
| 4454 |
return (a->dev == b->dev |
| 4455 |
&& a->ino == b->ino |
| 4456 |
&& strcmp(a->path, b->path) == 0); |
| 4457 |
} |
| 4458 |
|
| 4459 |
DEFINE_HASH_TABLE(process_mapped_files, struct process_mapped_file_entry, |
| 4460 |
process_mapped_file_entry_to_key, |
| 4461 |
process_mapped_file_key_hash_pair, |
| 4462 |
process_mapped_file_key_eq); |
| 4463 |
|
| 4464 |
struct process_loaded_module_iterator { |
| 4465 |
struct userspace_loaded_module_iterator u; |
| 4466 |
struct process_mapped_files files; |
| 4467 |
}; |
| 4468 |
|
| 4469 |
static struct drgn_error * |
| 4470 |
process_add_mapping(struct process_loaded_module_iterator *it, |
| 4471 |
const char *maps_path, const char *map_files_path, |
| 4472 |
int map_files_fd, bool *logged_readlink_eperm, |
| 4473 |
bool *logged_stat_eperm, |
| 4474 |
struct drgn_map_files_segment_vector *map_files_segments, |
| 4475 |
struct drgn_mapped_file_segments *segments, |
| 4476 |
char *line, size_t line_len) |
| 4477 |
{ |
| 4478 |
struct drgn_program *prog = it->u.it.prog; |
| 4479 |
|
| 4480 |
struct drgn_map_files_segment segment; |
| 4481 |
uint64_t segment_file_offset; |
| 4482 |
unsigned int dev_major, dev_minor; |
| 4483 |
uint64_t ino; |
| 4484 |
int map_name_len, path_index; |
| 4485 |
if (sscanf(line, |
| 4486 |
"%" SCNx64 "-%" SCNx64 "%n %*s %" SCNx64 " %x:%x %" SCNu64 " %n", |
| 4487 |
&segment.start, &segment.end, &map_name_len, |
| 4488 |
&segment_file_offset, &dev_major, &dev_minor, &ino, |
| 4489 |
&path_index) != 6) { |
| 4490 |
return drgn_error_format(DRGN_ERROR_OTHER, "couldn't parse %s", |
| 4491 |
maps_path); |
| 4492 |
} |
| 4493 |
// Skip anonymous mappings. |
| 4494 |
if (ino == 0) |
| 4495 |
return NULL; |
| 4496 |
|
| 4497 |
if (!drgn_map_files_segment_vector_append(map_files_segments, &segment)) |
| 4498 |
return &drgn_enomem; |
| 4499 |
|
| 4500 |
struct process_mapped_file_key key = { |
| 4501 |
.dev = makedev(dev_major, dev_minor), |
| 4502 |
.ino = ino, |
| 4503 |
.path = line + path_index, |
| 4504 |
}; |
| 4505 |
_cleanup_free_ char *real_path = NULL; |
| 4506 |
|
| 4507 |
// /proc/$pid/maps has a couple of ambiguities that |
| 4508 |
// /proc/$pid/map_files/<address> can help with: |
| 4509 |
// |
| 4510 |
// 1. Newlines in the file path from /proc/$pid/maps are escaped as |
| 4511 |
// \012. However, \ is not escaped, so it is ambiguous whether \012 |
| 4512 |
// is a newline or appeared literally in the path. We can read the |
| 4513 |
// map_files link to get the unescaped path. |
| 4514 |
// 2. The device number in /proc/$pid/maps is incorrect for some |
| 4515 |
// filesystems. Specifically, for Btrfs as of Linux 6.5, it refers to |
| 4516 |
// a filesystem-wide device number rather than the subvolume-specific |
| 4517 |
// device numbers returned by stat. We can stat the map_files link to |
| 4518 |
// get the correct device number. |
| 4519 |
if (map_files_fd >= 0) { |
| 4520 |
char map_files_name[34]; |
| 4521 |
snprintf(map_files_name, sizeof(map_files_name), |
| 4522 |
"%" PRIx64 "-%" PRIx64, segment.start, segment.end); |
| 4523 |
|
| 4524 |
// The escaped path must be at least as long as the original |
| 4525 |
// path, so use that as the readlink buffer size. |
| 4526 |
size_t bufsiz = line_len - path_index + 1; |
| 4527 |
real_path = malloc(bufsiz); |
| 4528 |
if (!real_path) |
| 4529 |
return &drgn_enomem; |
| 4530 |
// Before Linux kernel commit bdb4d100afe9 ("procfs: always |
| 4531 |
// expose /proc/<pid>/map_files/ and make it readable") (in |
| 4532 |
// v4.3), reading these links required CAP_SYS_ADMIN. Since that |
| 4533 |
// commit, it only requires PTRACE_MODE_READ, which we must have |
| 4534 |
// since we opened /proc/$pid/maps. |
| 4535 |
// |
| 4536 |
// If we can't read this link, we have to fall back to the |
| 4537 |
// escaped path. Newlines and the literal sequence \012 are |
| 4538 |
// unlikely to appear in a path, so it's not a big deal. |
| 4539 |
ssize_t r = readlinkat(map_files_fd, map_files_name, real_path, |
| 4540 |
bufsiz); |
| 4541 |
if (r < 0) { |
| 4542 |
if (errno == EPERM) { |
| 4543 |
free(real_path); |
| 4544 |
real_path = NULL; |
| 4545 |
if (!*logged_readlink_eperm) { |
| 4546 |
drgn_log_debug(prog, |
| 4547 |
"don't have permission to read symlinks in %s", |
| 4548 |
map_files_path); |
| 4549 |
} |
| 4550 |
*logged_readlink_eperm = true; |
| 4551 |
} else if (errno == ENOENT) { |
| 4552 |
// We raced with a change to the mapping. |
| 4553 |
drgn_log_debug(prog, "mapping %s disappeared", |
| 4554 |
map_files_name); |
| 4555 |
return NULL; |
| 4556 |
} else { |
| 4557 |
return drgn_error_format_os("readlink", errno, |
| 4558 |
"%s/%s", |
| 4559 |
map_files_path, |
| 4560 |
map_files_name); |
| 4561 |
} |
| 4562 |
} else if (r >= bufsiz) { |
| 4563 |
// We didn't allocate enough for the link contents. The |
| 4564 |
// only way this is possible is if we raced with the |
| 4565 |
// mapping being replaced by a different path. |
| 4566 |
drgn_log_debug(prog, |
| 4567 |
"mapping %s path changed; skipping", |
| 4568 |
map_files_name); |
| 4569 |
return NULL; |
| 4570 |
} else { |
| 4571 |
real_path[r] = '\0'; |
| 4572 |
key.path = real_path; |
| 4573 |
} |
| 4574 |
|
| 4575 |
// Following these links requires CAP_SYS_ADMIN. If we can't, we |
| 4576 |
// have to fall back to using the device number from |
| 4577 |
// /proc/$pid/maps. Mapping files with the same path and inode |
| 4578 |
// number in different Btrfs subvolumes is unlikely, so this is |
| 4579 |
// also not a big deal. |
| 4580 |
struct stat st; |
| 4581 |
if (fstatat(map_files_fd, map_files_name, &st, 0) < 0) { |
| 4582 |
if (errno == EPERM) { |
| 4583 |
if (!*logged_stat_eperm) { |
| 4584 |
drgn_log_debug(prog, |
| 4585 |
"don't have permission to follow symlinks in %s", |
| 4586 |
map_files_path); |
| 4587 |
} |
| 4588 |
*logged_stat_eperm = true; |
| 4589 |
} else if (errno == ENOENT) { |
| 4590 |
// We raced with a change to the mapping. |
| 4591 |
drgn_log_debug(prog, "mapping %s disappeared", |
| 4592 |
map_files_name); |
| 4593 |
return NULL; |
| 4594 |
} else { |
| 4595 |
return drgn_error_format_os("stat", errno, |
| 4596 |
"%s/%s", |
| 4597 |
map_files_path, |
| 4598 |
map_files_name); |
| 4599 |
} |
| 4600 |
} else { |
| 4601 |
key.dev = st.st_dev; |
| 4602 |
} |
| 4603 |
} |
| 4604 |
|
| 4605 |
struct hash_pair hp = process_mapped_files_hash(&key); |
| 4606 |
struct process_mapped_files_iterator files_it = |
| 4607 |
process_mapped_files_search_hashed(&it->files, &key, hp); |
| 4608 |
if (!files_it.entry) { |
| 4609 |
if (!real_path) { |
| 4610 |
real_path = strdup(key.path); |
| 4611 |
if (!real_path) |
| 4612 |
return &drgn_enomem; |
| 4613 |
} |
| 4614 |
struct drgn_mapped_file *file = |
| 4615 |
drgn_mapped_file_create(real_path); |
| 4616 |
if (!file) |
| 4617 |
return &drgn_enomem; |
| 4618 |
struct process_mapped_file_entry entry = { |
| 4619 |
.dev = key.dev, |
| 4620 |
.ino = key.ino, |
| 4621 |
.file = file, |
| 4622 |
}; |
| 4623 |
if (process_mapped_files_insert_searched(&it->files, &entry, hp, |
| 4624 |
&files_it) < 0) { |
| 4625 |
drgn_mapped_file_destroy(file); |
| 4626 |
return &drgn_enomem; |
| 4627 |
} |
| 4628 |
// real_path is owned by the iterator now. |
| 4629 |
real_path = NULL; |
| 4630 |
} |
| 4631 |
return drgn_add_mapped_file_segment(segments, segment.start, segment.end, |
| 4632 |
segment_file_offset, |
| 4633 |
files_it.entry->file); |
| 4634 |
} |
| 4635 |
|
| 4636 |
static struct drgn_error * |
| 4637 |
process_get_mapped_files(struct process_loaded_module_iterator *it) |
| 4638 |
{ |
| 4639 |
struct drgn_error *err; |
| 4640 |
struct drgn_program *prog = it->u.it.prog; |
| 4641 |
|
| 4642 |
#define FORMAT "/proc/%ld/maps" |
| 4643 |
char maps_path[sizeof(FORMAT) |
| 4644 |
- sizeof("%ld") |
| 4645 |
+ max_decimal_length(long) |
| 4646 |
+ 1]; |
| 4647 |
snprintf(maps_path, sizeof(maps_path), FORMAT, (long)prog->pid); |
| 4648 |
#undef FORMAT |
| 4649 |
_cleanup_fclose_ FILE *maps_file = fopen(maps_path, "r"); |
| 4650 |
if (!maps_file) |
| 4651 |
return drgn_error_create_os("fopen", errno, maps_path); |
| 4652 |
drgn_log_debug(prog, "parsing %s", maps_path); |
| 4653 |
|
| 4654 |
#define FORMAT "/proc/%ld/map_files" |
| 4655 |
char map_files_path[sizeof(FORMAT) |
| 4656 |
- sizeof("%ld") |
| 4657 |
+ max_decimal_length(long) |
| 4658 |
+ 1]; |
| 4659 |
snprintf(map_files_path, sizeof(map_files_path), FORMAT, |
| 4660 |
(long)prog->pid); |
| 4661 |
#undef FORMAT |
| 4662 |
// Since Linux kernel commit bdb4d100afe9 ("procfs: always expose |
| 4663 |
// /proc/<pid>/map_files/ and make it readable") (in v4.3), |
| 4664 |
// /proc/$pid/map_files always exists. Before that, it only exists if |
| 4665 |
// CONFIG_CHECKPOINT_RESTORE is enabled. |
| 4666 |
// |
| 4667 |
// If it exists, we should always have permission to open it since we |
| 4668 |
// were able to open /proc/$pid/maps. |
| 4669 |
_cleanup_close_ int map_files_fd = |
| 4670 |
open(map_files_path, O_RDONLY | O_DIRECTORY); |
| 4671 |
if (map_files_fd < 0) { |
| 4672 |
if (errno != ENOENT) { |
| 4673 |
return drgn_error_create_os("open", errno, |
| 4674 |
map_files_path); |
| 4675 |
} |
| 4676 |
drgn_log_debug(prog, "%s: %m", map_files_path); |
| 4677 |
} |
| 4678 |
|
| 4679 |
_cleanup_free_ char *line = NULL; |
| 4680 |
size_t n = 0; |
| 4681 |
bool logged_readlink_eperm = false, logged_stat_eperm = false; |
| 4682 |
// While we're reading /proc/$pid/maps, we might as well cache the |
| 4683 |
// segments for drgn_module_try_proc_files_for_shared_library(). |
| 4684 |
VECTOR(drgn_map_files_segment_vector, map_files_segments); |
| 4685 |
struct drgn_mapped_file_segments segments = DRGN_MAPPED_FILE_SEGMENTS_INIT; |
| 4686 |
for (;;) { |
| 4687 |
errno = 0; |
| 4688 |
ssize_t len; |
| 4689 |
if ((len = getline(&line, &n, maps_file)) == -1) { |
| 4690 |
if (errno) { |
| 4691 |
err = drgn_error_create_os("getline", errno, |
| 4692 |
maps_path); |
| 4693 |
} else { |
| 4694 |
err = NULL; |
| 4695 |
} |
| 4696 |
break; |
| 4697 |
} |
| 4698 |
// Remove the newline. |
| 4699 |
if (len > 0 && line[len - 1] == '\n') |
| 4700 |
line[--len] = '\0'; |
| 4701 |
|
| 4702 |
drgn_log_debug(prog, "read %s", line); |
| 4703 |
err = process_add_mapping(it, maps_path, map_files_path, |
| 4704 |
map_files_fd, &logged_readlink_eperm, |
| 4705 |
&logged_stat_eperm, |
| 4706 |
&map_files_segments, &segments, line, |
| 4707 |
len); |
| 4708 |
if (err) |
| 4709 |
break; |
| 4710 |
} |
| 4711 |
if (err) { |
| 4712 |
drgn_mapped_file_segments_abort(&segments); |
| 4713 |
} else { |
| 4714 |
drgn_debug_info_set_map_files_segments(&prog->dbinfo, |
| 4715 |
&map_files_segments, |
| 4716 |
segments.sorted); |
| 4717 |
userspace_loaded_module_iterator_set_file_segments(&it->u, |
| 4718 |
&segments); |
| 4719 |
} |
| 4720 |
return err; |
| 4721 |
} |
| 4722 |
|
| 4723 |
static void |
| 4724 |
process_loaded_module_iterator_destroy(struct drgn_module_iterator *_it) |
| 4725 |
{ |
| 4726 |
struct process_loaded_module_iterator *it = |
| 4727 |
container_of(_it, struct process_loaded_module_iterator, u.it); |
| 4728 |
hash_table_for_each(process_mapped_files, files_it, &it->files) { |
| 4729 |
free((char *)files_it.entry->file->path); |
| 4730 |
drgn_mapped_file_destroy(files_it.entry->file); |
| 4731 |
} |
| 4732 |
process_mapped_files_deinit(&it->files); |
| 4733 |
userspace_loaded_module_iterator_deinit(&it->u); |
| 4734 |
free(it); |
| 4735 |
} |
| 4736 |
|
| 4737 |
static struct drgn_error * |
| 4738 |
process_loaded_module_iterator_create(struct drgn_program *prog, |
| 4739 |
struct drgn_module_iterator **ret) |
| 4740 |
{ |
| 4741 |
struct drgn_error *err; |
| 4742 |
struct process_loaded_module_iterator *it = calloc(1, sizeof(*it)); |
| 4743 |
if (!it) |
| 4744 |
return &drgn_enomem; |
| 4745 |
drgn_module_iterator_init(&it->u.it, prog, |
| 4746 |
process_loaded_module_iterator_destroy, |
| 4747 |
userspace_loaded_module_iterator_next); |
| 4748 |
process_mapped_files_init(&it->files); |
| 4749 |
err = process_get_mapped_files(it); |
| 4750 |
if (err) { |
| 4751 |
process_loaded_module_iterator_destroy(&it->u.it); |
| 4752 |
return err; |
| 4753 |
} |
| 4754 |
*ret = &it->u.it; |
| 4755 |
return NULL; |
| 4756 |
} |
| 4757 |
|
| 4758 |
static const char * |
| 4759 |
core_mapped_file_entry_to_key(struct drgn_mapped_file * const *entry) |
| 4760 |
{ |
| 4761 |
return (*entry)->path; |
| 4762 |
} |
| 4763 |
|
| 4764 |
DEFINE_HASH_TABLE(core_mapped_files, struct drgn_mapped_file *, |
| 4765 |
core_mapped_file_entry_to_key, c_string_key_hash_pair, |
| 4766 |
c_string_key_eq); |
| 4767 |
|
| 4768 |
struct core_loaded_module_iterator { |
| 4769 |
struct userspace_loaded_module_iterator u; |
| 4770 |
struct core_mapped_files files; |
| 4771 |
}; |
| 4772 |
|
| 4773 |
static struct drgn_error *parse_nt_file_error(struct binary_buffer *bb, |
| 4774 |
const char *pos, |
| 4775 |
const char *message) |
| 4776 |
{ |
| 4777 |
return drgn_error_create(DRGN_ERROR_OTHER, "couldn't parse NT_FILE"); |
| 4778 |
} |
| 4779 |
|
| 4780 |
static struct drgn_error * |
| 4781 |
core_get_mapped_files(struct core_loaded_module_iterator *it) |
| 4782 |
{ |
| 4783 |
struct drgn_error *err; |
| 4784 |
struct drgn_program *prog = it->u.it.prog; |
| 4785 |
|
| 4786 |
const void *note; |
| 4787 |
size_t note_size; |
| 4788 |
if (find_elf_note(prog->core, "CORE", NT_FILE, ¬e, ¬e_size)) |
| 4789 |
return drgn_error_libelf(); |
| 4790 |
if (!note) { |
| 4791 |
drgn_log_debug(prog, "core doesn't have NT_FILE note"); |
| 4792 |
return NULL; |
| 4793 |
} |
| 4794 |
|
| 4795 |
drgn_log_debug(prog, "parsing NT_FILE"); |
| 4796 |
|
| 4797 |
bool is_64_bit = drgn_platform_is_64_bit(&prog->platform); |
| 4798 |
bool little_endian = drgn_platform_is_little_endian(&prog->platform); |
| 4799 |
|
| 4800 |
struct binary_buffer bb; |
| 4801 |
binary_buffer_init(&bb, note, note_size, little_endian, |
| 4802 |
parse_nt_file_error); |
| 4803 |
|
| 4804 |
// fs/binfmt_elf.c in the Linux kernel source code documents the format |
| 4805 |
// of NT_FILE as: |
| 4806 |
// |
| 4807 |
// long count -- how many files are mapped |
| 4808 |
// long page_size -- units for file_ofs |
| 4809 |
// array of [COUNT] elements of |
| 4810 |
// long start |
| 4811 |
// long end |
| 4812 |
// long file_ofs |
| 4813 |
// followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL... |
| 4814 |
struct nt_file_segment64 { |
| 4815 |
uint64_t start; |
| 4816 |
uint64_t end; |
| 4817 |
uint64_t file_offset; |
| 4818 |
}; |
| 4819 |
struct nt_file_segment32 { |
| 4820 |
uint32_t start; |
| 4821 |
uint32_t end; |
| 4822 |
uint32_t file_offset; |
| 4823 |
}; |
| 4824 |
uint64_t count, page_size; |
| 4825 |
if (is_64_bit) { |
| 4826 |
if ((err = binary_buffer_next_u64(&bb, &count))) |
| 4827 |
return err; |
| 4828 |
if (count > UINT64_MAX / sizeof(struct nt_file_segment64)) |
| 4829 |
return binary_buffer_error(&bb, "count is too large"); |
| 4830 |
if ((err = binary_buffer_next_u64(&bb, &page_size)) || |
| 4831 |
(err = binary_buffer_skip(&bb, |
| 4832 |
count * sizeof(struct nt_file_segment64)))) |
| 4833 |
return err; |
| 4834 |
} else { |
| 4835 |
if ((err = binary_buffer_next_u32_into_u64(&bb, &count))) |
| 4836 |
return err; |
| 4837 |
if (count > UINT64_MAX / sizeof(struct nt_file_segment32)) |
| 4838 |
return binary_buffer_error(&bb, "count is too large"); |
| 4839 |
if ((err = binary_buffer_next_u32_into_u64(&bb, &page_size)) || |
| 4840 |
(err = binary_buffer_skip(&bb, |
| 4841 |
count * sizeof(struct nt_file_segment32)))) |
| 4842 |
return err; |
| 4843 |
} |
| 4844 |
|
| 4845 |
struct drgn_mapped_file_segments segments = |
| 4846 |
DRGN_MAPPED_FILE_SEGMENTS_INIT; |
| 4847 |
for (uint64_t i = 0; i < count; i++) { |
| 4848 |
struct nt_file_segment64 segment; |
| 4849 |
#define visit_nt_file_segment_members(visit_scalar_member, visit_raw_member) do { \ |
| 4850 |
visit_scalar_member(start); \ |
| 4851 |
visit_scalar_member(end); \ |
| 4852 |
visit_scalar_member(file_offset); \ |
| 4853 |
} while (0) |
| 4854 |
deserialize_struct64(&segment, struct nt_file_segment32, |
| 4855 |
visit_nt_file_segment_members, |
| 4856 |
(char *)note |
| 4857 |
+ (is_64_bit |
| 4858 |
? 16 + i * sizeof(struct nt_file_segment64) |
| 4859 |
: 8 + i * sizeof(struct nt_file_segment32)), |
| 4860 |
is_64_bit, bb.bswap); |
| 4861 |
#undef visit_nt_file_segment_members |
| 4862 |
segment.file_offset *= page_size; |
| 4863 |
const char *path = bb.pos; |
| 4864 |
if ((err = binary_buffer_skip_string(&bb))) |
| 4865 |
goto err; |
| 4866 |
drgn_log_debug(prog, |
| 4867 |
"found 0x%" PRIx64 "-0x%" PRIx64 " 0x%" PRIx64 " %s", |
| 4868 |
segment.start, segment.end, segment.file_offset, |
| 4869 |
path); |
| 4870 |
if (segment.start >= segment.end) |
| 4871 |
continue; |
| 4872 |
|
| 4873 |
struct hash_pair hp = core_mapped_files_hash(&path); |
| 4874 |
struct core_mapped_files_iterator files_it = |
| 4875 |
core_mapped_files_search_hashed(&it->files, &path, hp); |
| 4876 |
struct drgn_mapped_file *file; |
| 4877 |
if (files_it.entry) { |
| 4878 |
file = *files_it.entry; |
| 4879 |
} else { |
| 4880 |
file = drgn_mapped_file_create(path); |
| 4881 |
if (!file) { |
| 4882 |
err = &drgn_enomem; |
| 4883 |
goto err; |
| 4884 |
} |
| 4885 |
if (core_mapped_files_insert_searched(&it->files, &file, |
| 4886 |
hp, NULL) < 0) { |
| 4887 |
drgn_mapped_file_destroy(file); |
| 4888 |
err = &drgn_enomem; |
| 4889 |
goto err; |
| 4890 |
} |
| 4891 |
} |
| 4892 |
err = drgn_add_mapped_file_segment(&segments, segment.start, |
| 4893 |
segment.end, |
| 4894 |
segment.file_offset, file); |
| 4895 |
if (err) |
| 4896 |
goto err; |
| 4897 |
} |
| 4898 |
userspace_loaded_module_iterator_set_file_segments(&it->u, &segments); |
| 4899 |
return NULL; |
| 4900 |
|
| 4901 |
err: |
| 4902 |
drgn_mapped_file_segments_abort(&segments); |
| 4903 |
return err; |
| 4904 |
} |
| 4905 |
|
| 4906 |
static void |
| 4907 |
core_loaded_module_iterator_destroy(struct drgn_module_iterator *_it) |
| 4908 |
{ |
| 4909 |
struct core_loaded_module_iterator *it = |
| 4910 |
container_of(_it, struct core_loaded_module_iterator, u.it); |
| 4911 |
hash_table_for_each(core_mapped_files, files_it, &it->files) |
| 4912 |
drgn_mapped_file_destroy(*files_it.entry); |
| 4913 |
core_mapped_files_deinit(&it->files); |
| 4914 |
userspace_loaded_module_iterator_deinit(&it->u); |
| 4915 |
free(it); |
| 4916 |
} |
| 4917 |
|
| 4918 |
static struct drgn_error * |
| 4919 |
core_loaded_module_iterator_create(struct drgn_program *prog, |
| 4920 |
struct drgn_module_iterator **ret) |
| 4921 |
{ |
| 4922 |
struct drgn_error *err; |
| 4923 |
struct core_loaded_module_iterator *it = calloc(1, sizeof(*it)); |
| 4924 |
if (!it) |
| 4925 |
return &drgn_enomem; |
| 4926 |
drgn_module_iterator_init(&it->u.it, prog, |
| 4927 |
core_loaded_module_iterator_destroy, |
| 4928 |
userspace_loaded_module_iterator_next); |
| 4929 |
core_mapped_files_init(&it->files); |
| 4930 |
err = core_get_mapped_files(it); |
| 4931 |
if (err) { |
| 4932 |
core_loaded_module_iterator_destroy(&it->u.it); |
| 4933 |
return err; |
| 4934 |
} |
| 4935 |
*ret = &it->u.it; |
| 4936 |
return NULL; |
| 4937 |
} |
| 4938 |
|
| 4939 |
static struct drgn_error * |
| 4940 |
null_module_iterator_create(struct drgn_program *prog, |
| 4941 |
struct drgn_module_iterator **ret) |
| 4942 |
{ |
| 4943 |
struct drgn_module_iterator *it = calloc(1, sizeof(*it)); |
| 4944 |
if (!it) |
| 4945 |
return &drgn_enomem; |
| 4946 |
drgn_module_iterator_init(it, prog, NULL, NULL); |
| 4947 |
*ret = it; |
| 4948 |
return NULL; |
| 4949 |
} |
| 4950 |
|
| 4951 |
LIBDRGN_PUBLIC struct drgn_error * |
| 4952 |
drgn_loaded_module_iterator_create(struct drgn_program *prog, |
| 4953 |
struct drgn_module_iterator **ret) |
| 4954 |
{ |
| 4955 |
if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) |
| 4956 |
return linux_kernel_loaded_module_iterator_create(prog, ret); |
| 4957 |
else if (drgn_program_is_userspace_process(prog)) |
| 4958 |
return process_loaded_module_iterator_create(prog, ret); |
| 4959 |
else if (drgn_program_is_userspace_core(prog)) |
| 4960 |
return core_loaded_module_iterator_create(prog, ret); |
| 4961 |
else |
| 4962 |
return null_module_iterator_create(prog, ret); |
| 4963 |
} |
| 4964 |
|
| 4965 |
static inline void drgn_module_iterator_destroyp(struct drgn_module_iterator **itp) |
| 4966 |
{ |
| 4967 |
drgn_module_iterator_destroy(*itp); |
| 4968 |
} |
| 4969 |
|
| 4970 |
LIBDRGN_PUBLIC struct drgn_error * |
| 4971 |
drgn_create_loaded_modules(struct drgn_program *prog) |
| 4972 |
{ |
| 4973 |
_cleanup_(drgn_module_iterator_destroyp) |
| 4974 |
struct drgn_module_iterator *it = NULL; |
| 4975 |
struct drgn_error *err = drgn_loaded_module_iterator_create(prog, &it); |
| 4976 |
if (err) |
| 4977 |
return err; |
| 4978 |
struct drgn_module *module; |
| 4979 |
while (!(err = drgn_module_iterator_next(it, &module, NULL)) && module); |
| 4980 |
return err; |
| 4981 |
} |
| 4982 |
|
| 4983 |
struct load_debug_info_file { |
| 4984 |
const char *path; |
| 4985 |
// We only keep this to keep load_debug_info_provided::build_id alive |
| 4986 |
// without needing to copy it. If we add a drgn_module_try_file API that |
| 4987 |
// allows providing an Elf handle, we could pass it down. |
| 4988 |
Elf *elf; |
| 4989 |
// This may be consumed and set to -1. |
| 4990 |
int fd; |
| 4991 |
}; |
| 4992 |
|
| 4993 |
DEFINE_VECTOR(load_debug_info_file_vector, struct load_debug_info_file); |
| 4994 |
|
| 4995 |
struct load_debug_info_provided { |
| 4996 |
const void *build_id; |
| 4997 |
size_t build_id_len; |
| 4998 |
struct load_debug_info_file_vector files; |
| 4999 |
bool matched; |
| 5000 |
}; |
| 5001 |
|
| 5002 |
static struct nstring |
| 5003 |
load_debug_info_provided_key(const struct load_debug_info_provided *provided) |
| 5004 |
{ |
| 5005 |
return (struct nstring){ provided->build_id, provided->build_id_len }; |
| 5006 |
} |
| 5007 |
|
| 5008 |
DEFINE_HASH_TABLE(load_debug_info_provided_table, |
| 5009 |
struct load_debug_info_provided, |
| 5010 |
load_debug_info_provided_key, nstring_hash_pair, nstring_eq); |
| 5011 |
|
| 5012 |
struct load_debug_info_state { |
| 5013 |
// Provided files grouped by build ID. |
| 5014 |
struct load_debug_info_provided_table provided; |
| 5015 |
// Number of entries in the provided table that haven't matched any |
| 5016 |
// modules. |
| 5017 |
size_t unmatched_provided; |
| 5018 |
}; |
| 5019 |
|
| 5020 |
static struct drgn_error * |
| 5021 |
load_debug_info_add_provided_file(struct drgn_program *prog, |
| 5022 |
struct load_debug_info_state *state, |
| 5023 |
const char *path) |
| 5024 |
{ |
| 5025 |
_cleanup_close_ int fd = open(path, O_RDONLY); |
| 5026 |
if (fd < 0) { |
| 5027 |
drgn_log_warning(prog, "%s: %m; ignoring", path); |
| 5028 |
return NULL; |
| 5029 |
} |
| 5030 |
_cleanup_elf_end_ Elf *elf = dwelf_elf_begin(fd); |
| 5031 |
if (!elf) { |
| 5032 |
drgn_log_warning(prog, "%s: %s; ignoring", path, |
| 5033 |
elf_errmsg(-1)); |
| 5034 |
return NULL; |
| 5035 |
} |
| 5036 |
if (elf_kind(elf) != ELF_K_ELF) { |
| 5037 |
drgn_log_warning(prog, "%s: not an ELF file; ignoring", path); |
| 5038 |
return NULL; |
| 5039 |
} |
| 5040 |
const void *build_id; |
| 5041 |
ssize_t build_id_len = drgn_elf_gnu_build_id(elf, &build_id); |
| 5042 |
if (build_id_len <= 0) { |
| 5043 |
if (build_id_len < 0) { |
| 5044 |
drgn_log_warning(prog, "%s: %s; ignoring", path, |
| 5045 |
elf_errmsg(-1)); |
| 5046 |
} else { |
| 5047 |
drgn_log_warning(prog, "%s: no build ID; ignoring", |
| 5048 |
path); |
| 5049 |
} |
| 5050 |
return NULL; |
| 5051 |
} |
| 5052 |
|
| 5053 |
if (drgn_log_is_enabled(prog, DRGN_LOG_DEBUG)) { |
| 5054 |
_cleanup_free_ char *build_id_str = |
| 5055 |
ahexlify(build_id, build_id_len); |
| 5056 |
if (!build_id_str) |
| 5057 |
return &drgn_enomem; |
| 5058 |
drgn_log_debug(prog, "provided file %s build ID %s", |
| 5059 |
path, build_id_str); |
| 5060 |
} |
| 5061 |
|
| 5062 |
struct load_debug_info_provided provided = { |
| 5063 |
.build_id = build_id, |
| 5064 |
.build_id_len = build_id_len, |
| 5065 |
}; |
| 5066 |
struct load_debug_info_provided_table_iterator it; |
| 5067 |
int r = load_debug_info_provided_table_insert(&state->provided, |
| 5068 |
&provided, &it); |
| 5069 |
if (r < 0) |
| 5070 |
return &drgn_enomem; |
| 5071 |
if (r > 0) { |
| 5072 |
load_debug_info_file_vector_init(&it.entry->files); |
| 5073 |
state->unmatched_provided++; |
| 5074 |
} |
| 5075 |
|
| 5076 |
struct load_debug_info_file file = { |
| 5077 |
.path = path, |
| 5078 |
.fd = fd, |
| 5079 |
.elf = elf, |
| 5080 |
}; |
| 5081 |
if (!load_debug_info_file_vector_append(&it.entry->files, &file)) { |
| 5082 |
if (load_debug_info_file_vector_empty(&it.entry->files)) { |
| 5083 |
// The key will no longer be valid once we free the Elf |
| 5084 |
// handle, so we need to delete the entry. |
| 5085 |
load_debug_info_provided_table_delete_iterator(&state->provided, |
| 5086 |
it); |
| 5087 |
} |
| 5088 |
return &drgn_enomem; |
| 5089 |
} |
| 5090 |
// fd and elf are owned by state now. |
| 5091 |
fd = -1; |
| 5092 |
elf = NULL; |
| 5093 |
return NULL; |
| 5094 |
} |
| 5095 |
|
| 5096 |
static void load_debug_info_state_deinit(struct load_debug_info_state *state) |
| 5097 |
{ |
| 5098 |
hash_table_for_each(load_debug_info_provided_table, it, |
| 5099 |
&state->provided) { |
| 5100 |
vector_for_each(load_debug_info_file_vector, file, |
| 5101 |
&it.entry->files) { |
| 5102 |
elf_end(file->elf); |
| 5103 |
if (file->fd >= 0) |
| 5104 |
close(file->fd); |
| 5105 |
} |
| 5106 |
load_debug_info_file_vector_deinit(&it.entry->files); |
| 5107 |
} |
| 5108 |
load_debug_info_provided_table_deinit(&state->provided); |
| 5109 |
} |
| 5110 |
|
| 5111 |
static struct load_debug_info_provided * |
| 5112 |
load_debug_info_find_provided(struct load_debug_info_state *state, |
| 5113 |
const void *build_id, size_t build_id_len) |
| 5114 |
{ |
| 5115 |
struct nstring key = { build_id, build_id_len }; |
| 5116 |
struct load_debug_info_provided *provided = |
| 5117 |
load_debug_info_provided_table_search(&state->provided, |
| 5118 |
&key).entry; |
| 5119 |
if (provided && !provided->matched) { |
| 5120 |
state->unmatched_provided--; |
| 5121 |
provided->matched = true; |
| 5122 |
} |
| 5123 |
return provided; |
| 5124 |
} |
| 5125 |
|
| 5126 |
static struct drgn_error * |
| 5127 |
load_debug_info_try_provided(struct drgn_module *module, |
| 5128 |
struct load_debug_info_provided *provided, |
| 5129 |
enum drgn_module_file_status not_status) |
| 5130 |
{ |
| 5131 |
struct drgn_error *err; |
| 5132 |
vector_for_each(load_debug_info_file_vector, file, &provided->files) { |
| 5133 |
// No need to check build ID again. |
| 5134 |
err = drgn_module_try_file_internal(module, file->path, |
| 5135 |
file->fd, false, NULL); |
| 5136 |
// drgn_module_try_file_internal took ownership of file->fd. In |
| 5137 |
// the unlikely scenario that another module has the same build |
| 5138 |
// ID, we'll just have to reopen it by path. |
| 5139 |
file->fd = -1; |
| 5140 |
if (err) |
| 5141 |
return err; |
| 5142 |
|
| 5143 |
if (module->loaded_file_status != not_status |
| 5144 |
&& module->debug_file_status != not_status) |
| 5145 |
break; |
| 5146 |
} |
| 5147 |
return NULL; |
| 5148 |
} |
| 5149 |
|
| 5150 |
static struct drgn_error * |
| 5151 |
load_debug_info_try_provided_supplementary_files(struct drgn_module *module, |
| 5152 |
struct load_debug_info_state *state) |
| 5153 |
{ |
| 5154 |
const void *checksum; |
| 5155 |
size_t checksum_len; |
| 5156 |
if (drgn_module_wanted_supplementary_debug_file(module, NULL, NULL, |
| 5157 |
&checksum, |
| 5158 |
&checksum_len) |
| 5159 |
!= DRGN_SUPPLEMENTARY_FILE_GNU_DEBUGALTLINK) |
| 5160 |
return NULL; |
| 5161 |
struct load_debug_info_provided *provided = |
| 5162 |
load_debug_info_find_provided(state, checksum, checksum_len); |
| 5163 |
if (!provided) |
| 5164 |
return NULL; |
| 5165 |
drgn_module_try_supplementary_debug_file_log(module, |
| 5166 |
"trying provided files for"); |
| 5167 |
return load_debug_info_try_provided(module, provided, |
| 5168 |
DRGN_MODULE_FILE_WANT_SUPPLEMENTARY); |
| 5169 |
} |
| 5170 |
|
| 5171 |
static struct drgn_error * |
| 5172 |
load_debug_info_try_provided_vmlinux(struct drgn_module *module, |
| 5173 |
struct load_debug_info_state *state) |
| 5174 |
{ |
| 5175 |
struct drgn_error *err; |
| 5176 |
struct drgn_program *prog = module->prog; |
| 5177 |
bool logged_trying = false; |
| 5178 |
hash_table_for_each(load_debug_info_provided_table, it, |
| 5179 |
&state->provided) { |
| 5180 |
vector_for_each(load_debug_info_file_vector, file, |
| 5181 |
&it.entry->files) { |
| 5182 |
int r = elf_is_vmlinux(file->elf); |
| 5183 |
if (r < 0) { |
| 5184 |
drgn_log_debug(prog, "%s: %s", file->path, |
| 5185 |
elf_errmsg(-1)); |
| 5186 |
} |
| 5187 |
if (r <= 0) |
| 5188 |
continue; |
| 5189 |
|
| 5190 |
if (!logged_trying) { |
| 5191 |
drgn_module_try_files_log(module, |
| 5192 |
"(Linux version %s): trying provided files for", |
| 5193 |
prog->vmcoreinfo.osrelease); |
| 5194 |
logged_trying = true; |
| 5195 |
} |
| 5196 |
|
| 5197 |
const char *release; |
| 5198 |
ssize_t release_len = |
| 5199 |
elf_vmlinux_release(file->elf, &release); |
| 5200 |
if (release_len < 0) { |
| 5201 |
drgn_log_debug(prog, "%s: %s", file->path, |
| 5202 |
elf_errmsg(-1)); |
| 5203 |
continue; |
| 5204 |
} else if (release_len == 0) { |
| 5205 |
drgn_log_debug(prog, "%s: %s Linux version not found", |
| 5206 |
module->name, file->path); |
| 5207 |
continue; |
| 5208 |
} |
| 5209 |
|
| 5210 |
if (strlen(prog->vmcoreinfo.osrelease) == release_len |
| 5211 |
&& memcmp(release, prog->vmcoreinfo.osrelease, |
| 5212 |
release_len) == 0) { |
| 5213 |
drgn_log_debug(prog, "%s: %s Linux version matches", |
| 5214 |
module->name, file->path); |
| 5215 |
} else { |
| 5216 |
drgn_log_debug(prog, |
| 5217 |
"%s: %s Linux version (%.*s) does not match", |
| 5218 |
module->name, file->path, |
| 5219 |
release_len > INT_MAX |
| 5220 |
? INT_MAX : (int)release_len, |
| 5221 |
release); |
| 5222 |
continue; |
| 5223 |
} |
| 5224 |
|
| 5225 |
if (!it.entry->matched) { |
| 5226 |
state->unmatched_provided--; |
| 5227 |
it.entry->matched = true; |
| 5228 |
} |
| 5229 |
|
| 5230 |
err = drgn_module_try_file_internal(module, file->path, |
| 5231 |
file->fd, true, |
| 5232 |
NULL); |
| 5233 |
file->fd = -1; |
| 5234 |
if (err) |
| 5235 |
return err; |
| 5236 |
if (module->loaded_file_status != DRGN_MODULE_FILE_WANT |
| 5237 |
&& module->debug_file_status != DRGN_MODULE_FILE_WANT) |
| 5238 |
break; |
| 5239 |
} |
| 5240 |
} |
| 5241 |
return NULL; |
| 5242 |
} |
| 5243 |
|
| 5244 |
static struct drgn_error * |
| 5245 |
load_debug_info_try_provided_files(struct drgn_module *module, |
| 5246 |
struct load_debug_info_state *state) |
| 5247 |
{ |
| 5248 |
struct drgn_error *err; |
| 5249 |
|
| 5250 |
err = load_debug_info_try_provided_supplementary_files(module, state); |
| 5251 |
if (err) |
| 5252 |
return err; |
| 5253 |
|
| 5254 |
const void *build_id; |
| 5255 |
size_t build_id_len; |
| 5256 |
drgn_module_build_id(module, &build_id, &build_id_len); |
| 5257 |
if (build_id_len > 0) { |
| 5258 |
// Look up the provided file even if we don't need it so that it |
| 5259 |
// counts as matched. |
| 5260 |
struct load_debug_info_provided *provided = |
| 5261 |
load_debug_info_find_provided(state, build_id, |
| 5262 |
build_id_len); |
| 5263 |
if (provided && drgn_module_wants_file(module)) { |
| 5264 |
uint64_t orig_supplementary_file_generation = |
| 5265 |
module->prog->dbinfo.supplementary_file_generation; |
| 5266 |
drgn_module_try_files_log(module, |
| 5267 |
"trying provided files for"); |
| 5268 |
err = load_debug_info_try_provided(module, provided, |
| 5269 |
DRGN_MODULE_FILE_WANT); |
| 5270 |
if (err) |
| 5271 |
return err; |
| 5272 |
// If the wanted supplementary debug file changed, try |
| 5273 |
// finding it again. |
| 5274 |
if (drgn_module_wanted_supplementary_debug_file_is_new(module, |
| 5275 |
orig_supplementary_file_generation)) { |
| 5276 |
err = load_debug_info_try_provided_supplementary_files(module, |
| 5277 |
state); |
| 5278 |
if (err) |
| 5279 |
return err; |
| 5280 |
} |
| 5281 |
} |
| 5282 |
} else if (module->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL |
| 5283 |
&& drgn_module_kind(module) == DRGN_MODULE_MAIN) { |
| 5284 |
// Before Linux kernel commit 0935288c6e00 ("kdump: append |
| 5285 |
// kernel build-id string to VMCOREINFO") (in v5.9) and in a few |
| 5286 |
// broken stable versions (see |
| 5287 |
// ignore_broken_vmcoreinfo_build_id()), we can't get the |
| 5288 |
// vmlinux build ID from a kernel core dump. Fall back to |
| 5289 |
// checking every provided file for a vmlinux file with a |
| 5290 |
// matching version. |
| 5291 |
err = load_debug_info_try_provided_vmlinux(module, state); |
| 5292 |
if (err) |
| 5293 |
return err; |
| 5294 |
} |
| 5295 |
return NULL; |
| 5296 |
} |
| 5297 |
|
| 5298 |
static void load_debug_info_log_missing(struct drgn_module *module, |
| 5299 |
unsigned int max_warnings, |
| 5300 |
unsigned int *num_missing) |
| 5301 |
{ |
| 5302 |
if (++(*num_missing) > max_warnings) |
| 5303 |
return; |
| 5304 |
const char *missing_loaded = ""; |
| 5305 |
if (drgn_module_loaded_file_status(module) == DRGN_MODULE_FILE_WANT) { |
| 5306 |
switch (drgn_module_kind(module)) { |
| 5307 |
case DRGN_MODULE_MAIN: |
| 5308 |
missing_loaded = "executable file"; |
| 5309 |
break; |
| 5310 |
case DRGN_MODULE_SHARED_LIBRARY: |
| 5311 |
case DRGN_MODULE_VDSO: |
| 5312 |
missing_loaded = "shared object file"; |
| 5313 |
break; |
| 5314 |
default: |
| 5315 |
missing_loaded = "loaded file"; |
| 5316 |
break; |
| 5317 |
} |
| 5318 |
} |
| 5319 |
const char *missing_debug; |
| 5320 |
switch (drgn_module_debug_file_status(module)) { |
| 5321 |
case DRGN_MODULE_FILE_WANT: |
| 5322 |
missing_debug = "debugging symbols"; |
| 5323 |
break; |
| 5324 |
case DRGN_MODULE_FILE_WANT_SUPPLEMENTARY: |
| 5325 |
missing_debug = "supplementary debugging symbols"; |
| 5326 |
break; |
| 5327 |
default: |
| 5328 |
missing_debug = ""; |
| 5329 |
break; |
| 5330 |
} |
| 5331 |
const char *name_extra = ""; |
| 5332 |
if (module->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL |
| 5333 |
&& drgn_module_kind(module) == DRGN_MODULE_MAIN) |
| 5334 |
name_extra = module->prog->vmcoreinfo.osrelease; |
| 5335 |
drgn_log_warning(module->prog, "missing %s%s%s for %s%s%s", missing_loaded, |
| 5336 |
missing_loaded[0] && missing_debug[0] ? " and ": "", |
| 5337 |
missing_debug, module->name, name_extra[0] ? " " : "", |
| 5338 |
name_extra); |
| 5339 |
} |
| 5340 |
|
| 5341 |
LIBDRGN_PUBLIC struct drgn_error * |
| 5342 |
drgn_program_load_debug_info(struct drgn_program *prog, const char **paths, |
| 5343 |
size_t n, bool load_default, bool load_main) |
| 5344 |
{ |
| 5345 |
struct drgn_error *err; |
| 5346 |
|
| 5347 |
if (n == 0 && !load_default && !load_main) { |
| 5348 |
// We don't have any files to try. Don't create any modules. |
| 5349 |
return NULL; |
| 5350 |
} |
| 5351 |
|
| 5352 |
drgn_blocking_guard(blocking_state); |
| 5353 |
|
| 5354 |
const char *env = getenv("DRGN_MAX_DEBUG_INFO_ERRORS"); |
| 5355 |
unsigned int max_warnings = env ? atoi(env) : 5; |
| 5356 |
unsigned int num_missing = 0; |
| 5357 |
|
| 5358 |
drgn_log_debug(prog, "loading %sdebugging symbols", |
| 5359 |
load_default ? "default " : load_main ? "main " : ""); |
| 5360 |
|
| 5361 |
_cleanup_(load_debug_info_state_deinit) |
| 5362 |
struct load_debug_info_state state = { |
| 5363 |
.provided = HASH_TABLE_INIT, |
| 5364 |
}; |
| 5365 |
for (size_t i = 0; i < n; i++) { |
| 5366 |
err = load_debug_info_add_provided_file(prog, &state, paths[i]); |
| 5367 |
if (err) |
| 5368 |
return err; |
| 5369 |
} |
| 5370 |
|
| 5371 |
if (load_debug_info_provided_table_empty(&state.provided) |
| 5372 |
&& !load_default && !load_main) { |
| 5373 |
drgn_log_debug(prog, "no usable provided files"); |
| 5374 |
return NULL; |
| 5375 |
} |
| 5376 |
|
| 5377 |
uint64_t old_generation = prog->dbinfo.load_debug_info_generation; |
| 5378 |
|
| 5379 |
_cleanup_(drgn_module_iterator_destroyp) |
| 5380 |
struct drgn_module_iterator *it = NULL; |
| 5381 |
err = drgn_loaded_module_iterator_create(prog, &it); |
| 5382 |
if (err) |
| 5383 |
return err; |
| 5384 |
it->for_load_debug_info = true; |
| 5385 |
VECTOR(drgn_module_vector, modules); |
| 5386 |
struct drgn_module *module; |
| 5387 |
while (!(err = drgn_module_iterator_next(it, &module, NULL)) && module) { |
| 5388 |
// Reset DONT_WANT to WANT. |
| 5389 |
if (module->loaded_file_status == DRGN_MODULE_FILE_DONT_WANT) |
| 5390 |
module->loaded_file_status = DRGN_MODULE_FILE_WANT; |
| 5391 |
if (module->debug_file_status == DRGN_MODULE_FILE_DONT_WANT) |
| 5392 |
module->debug_file_status = DRGN_MODULE_FILE_WANT; |
| 5393 |
|
| 5394 |
err = load_debug_info_try_provided_files(module, &state); |
| 5395 |
if (err) |
| 5396 |
return err; |
| 5397 |
|
| 5398 |
if (drgn_module_wants_file(module) |
| 5399 |
&& (load_default |
| 5400 |
|| (load_main |
| 5401 |
&& drgn_module_kind(module) == DRGN_MODULE_MAIN)) |
| 5402 |
&& !drgn_module_vector_append(&modules, &module)) |
| 5403 |
return &drgn_enomem; |
| 5404 |
} |
| 5405 |
if (err) |
| 5406 |
return err; |
| 5407 |
|
| 5408 |
struct drgn_module **wanted_modules = |
| 5409 |
drgn_module_vector_begin(&modules); |
| 5410 |
size_t num_wanted_modules = drgn_module_vector_size(&modules); |
| 5411 |
|
| 5412 |
// The module iterator may have tried to load debug info, so we need to |
| 5413 |
// check each module again. |
| 5414 |
if (num_wanted_modules > 0) { |
| 5415 |
uint64_t new_generation = |
| 5416 |
++prog->dbinfo.load_debug_info_generation; |
| 5417 |
size_t new_num_wanted_modules = 0; |
| 5418 |
for (size_t i = 0; i < num_wanted_modules; i++) { |
| 5419 |
module = wanted_modules[i]; |
| 5420 |
if (module->load_debug_info_generation <= old_generation) { |
| 5421 |
// Reset DONT_WANT to WANT. |
| 5422 |
if (module->loaded_file_status == DRGN_MODULE_FILE_DONT_WANT) |
| 5423 |
module->loaded_file_status = DRGN_MODULE_FILE_WANT; |
| 5424 |
if (module->debug_file_status == DRGN_MODULE_FILE_DONT_WANT) |
| 5425 |
module->debug_file_status = DRGN_MODULE_FILE_WANT; |
| 5426 |
if (drgn_module_wants_file(module)) { |
| 5427 |
wanted_modules[new_num_wanted_modules++] = module; |
| 5428 |
module->load_debug_info_generation = new_generation; |
| 5429 |
} |
| 5430 |
} else if (drgn_module_wants_file(module)) { |
| 5431 |
load_debug_info_log_missing(module, |
| 5432 |
max_warnings, |
| 5433 |
&num_missing); |
| 5434 |
} |
| 5435 |
} |
| 5436 |
num_wanted_modules = new_num_wanted_modules; |
| 5437 |
} |
| 5438 |
|
| 5439 |
if (num_wanted_modules > 0) { |
| 5440 |
uint64_t orig_supplementary_file_generation = |
| 5441 |
prog->dbinfo.supplementary_file_generation; |
| 5442 |
drgn_handler_list_for_each_enabled(struct drgn_debug_info_finder, |
| 5443 |
finder, |
| 5444 |
&prog->dbinfo.debug_info_finders) { |
| 5445 |
err = finder->ops.find(wanted_modules, |
| 5446 |
num_wanted_modules, finder->arg); |
| 5447 |
if (err) |
| 5448 |
return err; |
| 5449 |
size_t new_num_wanted_modules = 0; |
| 5450 |
for (size_t i = 0; i < num_wanted_modules; i++) { |
| 5451 |
module = wanted_modules[i]; |
| 5452 |
// If there are no more finders to try after |
| 5453 |
// this and a finder changed the wanted |
| 5454 |
// supplementary debug file, try to find a |
| 5455 |
// provided file for it one last time. |
| 5456 |
if (drgn_handler_is_last_enabled(&finder->handler) |
| 5457 |
&& drgn_module_wanted_supplementary_debug_file_is_new(module, |
| 5458 |
orig_supplementary_file_generation)) { |
| 5459 |
err = load_debug_info_try_provided_supplementary_files(module, |
| 5460 |
&state); |
| 5461 |
if (err) |
| 5462 |
return err; |
| 5463 |
} |
| 5464 |
if (drgn_module_wants_file(module)) { |
| 5465 |
wanted_modules[new_num_wanted_modules++] = |
| 5466 |
module; |
| 5467 |
} |
| 5468 |
} |
| 5469 |
num_wanted_modules = new_num_wanted_modules; |
| 5470 |
if (num_wanted_modules == 0) |
| 5471 |
break; |
| 5472 |
} |
| 5473 |
} |
| 5474 |
|
| 5475 |
if (state.unmatched_provided != 0) { |
| 5476 |
hash_table_for_each(load_debug_info_provided_table, pit, |
| 5477 |
&state.provided) { |
| 5478 |
if (!pit.entry->matched) { |
| 5479 |
vector_for_each(load_debug_info_file_vector, |
| 5480 |
file, &pit.entry->files) { |
| 5481 |
drgn_log_warning(prog, |
| 5482 |
"provided file %s did not match any loaded modules; ignoring", |
| 5483 |
file->path); |
| 5484 |
} |
| 5485 |
} |
| 5486 |
} |
| 5487 |
} |
| 5488 |
|
| 5489 |
for (size_t i = 0; i < num_wanted_modules; i++) { |
| 5490 |
load_debug_info_log_missing(wanted_modules[i], max_warnings, |
| 5491 |
&num_missing); |
| 5492 |
} |
| 5493 |
if (num_missing > max_warnings) { |
| 5494 |
drgn_log_warning(prog, "... missing %u more", |
| 5495 |
num_missing - max_warnings); |
| 5496 |
} |
| 5497 |
|
| 5498 |
// Update the DWARF index eagerly, mostly because that's what we did |
| 5499 |
// back when we used libdwfl. We may want to remove this in the future. |
| 5500 |
err = drgn_dwarf_info_update_index(&prog->dbinfo); |
| 5501 |
if (err) |
| 5502 |
return err; |
| 5503 |
|
| 5504 |
if (num_missing > 0) { |
| 5505 |
return drgn_error_create(DRGN_ERROR_MISSING_DEBUG_INFO, |
| 5506 |
"missing some debugging symbols; see https://drgn.readthedocs.io/en/latest/getting_debugging_symbols.html"); |
| 5507 |
} |
| 5508 |
|
| 5509 |
return NULL; |
| 5510 |
} |
| 5511 |
|
| 5512 |
LIBDRGN_PUBLIC struct drgn_error * |
| 5513 |
drgn_load_module_debug_info(struct drgn_module **modules, size_t *num_modulesp) |
| 5514 |
{ |
| 5515 |
struct drgn_error *err; |
| 5516 |
|
| 5517 |
const size_t orig_num_modules = *num_modulesp; |
| 5518 |
if (orig_num_modules == 0) |
| 5519 |
return NULL; |
| 5520 |
|
| 5521 |
struct drgn_program *prog = modules[0]->prog; |
| 5522 |
drgn_log_debug(prog, "loading debugging symbols for %zu modules", |
| 5523 |
orig_num_modules); |
| 5524 |
|
| 5525 |
size_t num_wanted_modules = 0; |
| 5526 |
for (size_t i = 0; i < orig_num_modules; i++) { |
| 5527 |
if (modules[i]->prog != prog) { |
| 5528 |
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT, |
| 5529 |
"modules are from different programs"); |
| 5530 |
} |
| 5531 |
if (drgn_module_wants_file(modules[i])) { |
| 5532 |
modules[num_wanted_modules++] = modules[i]; |
| 5533 |
} else if (modules[i]->loaded_file_status == DRGN_MODULE_FILE_DONT_WANT |
| 5534 |
|| modules[i]->loaded_file_status == DRGN_MODULE_FILE_DONT_WANT) { |
| 5535 |
drgn_log_debug(prog, |
| 5536 |
"debugging symbols not wanted for %s", |
| 5537 |
modules[i]->name); |
| 5538 |
} else { |
| 5539 |
drgn_log_debug(prog, |
| 5540 |
"debugging symbols already loaded for %s", |
| 5541 |
modules[i]->name); |
| 5542 |
} |
| 5543 |
} |
| 5544 |
if (num_wanted_modules == 0) { |
| 5545 |
*num_modulesp = 0; |
| 5546 |
return NULL; |
| 5547 |
} |
| 5548 |
|
| 5549 |
uint64_t generation = ++prog->dbinfo.load_debug_info_generation; |
| 5550 |
for (size_t i = 0; i < num_wanted_modules; i++) |
| 5551 |
modules[i]->load_debug_info_generation = generation; |
| 5552 |
|
| 5553 |
drgn_blocking_guard(blocking_state); |
| 5554 |
|
| 5555 |
const size_t orig_num_wanted_modules = num_wanted_modules; |
| 5556 |
drgn_handler_list_for_each_enabled(struct drgn_debug_info_finder, |
| 5557 |
finder, |
| 5558 |
&prog->dbinfo.debug_info_finders) { |
| 5559 |
err = finder->ops.find(modules, num_wanted_modules, |
| 5560 |
finder->arg); |
| 5561 |
if (err) |
| 5562 |
return err; |
| 5563 |
size_t new_num_wanted_modules = 0; |
| 5564 |
for (size_t i = 0; i < num_wanted_modules; i++) { |
| 5565 |
if (drgn_module_wants_file(modules[i])) |
| 5566 |
modules[new_num_wanted_modules++] = modules[i]; |
| 5567 |
} |
| 5568 |
num_wanted_modules = new_num_wanted_modules; |
| 5569 |
if (num_wanted_modules == 0) |
| 5570 |
break; |
| 5571 |
} |
| 5572 |
drgn_log_debug(prog, "debugging symbols loaded for %zu/%zu modules", |
| 5573 |
orig_num_wanted_modules - num_wanted_modules, |
| 5574 |
orig_num_wanted_modules); |
| 5575 |
*num_modulesp = num_wanted_modules; |
| 5576 |
return NULL; |
| 5577 |
} |
| 5578 |
|
| 5579 |
static struct drgn_error * |
| 5580 |
elf_symbols_search(const char *name, uint64_t addr, |
| 5581 |
enum drgn_find_symbol_flags flags, void *data, |
| 5582 |
struct drgn_symbol_result_builder *builder) |
| 5583 |
{ |
| 5584 |
struct drgn_error *err; |
| 5585 |
struct drgn_program *prog = data; |
| 5586 |
|
| 5587 |
if (flags & DRGN_FIND_SYMBOL_ADDR) { |
| 5588 |
struct drgn_module *module = |
| 5589 |
drgn_module_find_by_address(prog, addr); |
| 5590 |
if (!module) |
| 5591 |
return NULL; |
| 5592 |
return drgn_module_elf_symbols_search(module, name, addr, flags, |
| 5593 |
builder); |
| 5594 |
} else { |
| 5595 |
hash_table_for_each(drgn_module_table, it, |
| 5596 |
&prog->dbinfo.modules) { |
| 5597 |
for (struct drgn_module *module = *it.entry; module; |
| 5598 |
module = module->next_same_name) { |
| 5599 |
err = drgn_module_elf_symbols_search(module, |
| 5600 |
name, addr, |
| 5601 |
flags, |
| 5602 |
builder); |
| 5603 |
if (err == &drgn_stop) |
| 5604 |
break; |
| 5605 |
if (err) |
| 5606 |
return err; |
| 5607 |
} |
| 5608 |
} |
| 5609 |
return NULL; |
| 5610 |
} |
| 5611 |
} |
| 5612 |
|
| 5613 |
void drgn_debug_info_init(struct drgn_debug_info *dbinfo, |
| 5614 |
struct drgn_program *prog) |
| 5615 |
{ |
| 5616 |
elf_version(EV_CURRENT); |
| 5617 |
dbinfo->prog = prog; |
| 5618 |
drgn_module_table_init(&dbinfo->modules); |
| 5619 |
drgn_module_address_tree_init(&dbinfo->modules_by_address); |
| 5620 |
const struct drgn_type_finder_ops type_finder_ops = { |
| 5621 |
.find = drgn_debug_info_find_type, |
| 5622 |
}; |
| 5623 |
drgn_program_register_type_finder_impl(prog, &dbinfo->type_finder, |
| 5624 |
"dwarf", &type_finder_ops, |
| 5625 |
dbinfo, 0); |
| 5626 |
const struct drgn_object_finder_ops object_finder_ops = { |
| 5627 |
.find = drgn_debug_info_find_object, |
| 5628 |
}; |
| 5629 |
drgn_program_register_object_finder_impl(prog, &dbinfo->object_finder, |
| 5630 |
"dwarf", &object_finder_ops, |
| 5631 |
dbinfo, 0); |
| 5632 |
const struct drgn_symbol_finder_ops symbol_finder_ops = { |
| 5633 |
.find = elf_symbols_search, |
| 5634 |
}; |
| 5635 |
drgn_program_register_symbol_finder_impl(prog, &dbinfo->symbol_finder, |
| 5636 |
"elf", &symbol_finder_ops, |
| 5637 |
prog, 0); |
| 5638 |
const struct drgn_debug_info_finder_ops |
| 5639 |
standard_debug_info_finder_ops = { |
| 5640 |
.find = drgn_standard_debug_info_find, |
| 5641 |
}; |
| 5642 |
drgn_program_register_debug_info_finder_impl(prog, |
| 5643 |
&dbinfo->standard_debug_info_finder, |
| 5644 |
"standard", |
| 5645 |
&standard_debug_info_finder_ops, |
| 5646 |
&dbinfo->options, 0); |
| 5647 |
drgn_debug_info_options_init(&dbinfo->options); |
| 5648 |
#if WITH_DEBUGINFOD |
| 5649 |
dbinfo->debuginfod_client = NULL; |
| 5650 |
if (drgn_have_debuginfod()) { |
| 5651 |
const struct drgn_debug_info_finder_ops |
| 5652 |
debuginfod_debug_info_finder_ops = { |
| 5653 |
.find = drgn_debuginfod_find, |
| 5654 |
}; |
| 5655 |
drgn_program_register_debug_info_finder_impl(prog, |
| 5656 |
&dbinfo->debuginfod_debug_info_finder, |
| 5657 |
"debuginfod", |
| 5658 |
&debuginfod_debug_info_finder_ops, |
| 5659 |
prog, |
| 5660 |
DRGN_HANDLER_REGISTER_ENABLE_LAST); |
| 5661 |
} |
| 5662 |
#endif |
| 5663 |
drgn_dwarf_info_init(dbinfo); |
| 5664 |
} |
| 5665 |
|
| 5666 |
void drgn_debug_info_deinit(struct drgn_debug_info *dbinfo) |
| 5667 |
{ |
| 5668 |
free(dbinfo->map_files_segments); |
| 5669 |
drgn_debug_info_options_deinit(&dbinfo->options); |
| 5670 |
#if WITH_DEBUGINFOD |
| 5671 |
if (dbinfo->debuginfod_client) |
| 5672 |
drgn_debuginfod_end(dbinfo->debuginfod_client); |
| 5673 |
#endif |
| 5674 |
drgn_handler_list_deinit(struct drgn_debug_info_finder, finder, |
| 5675 |
&dbinfo->debug_info_finders, |
| 5676 |
if (finder->ops.destroy) |
| 5677 |
finder->ops.destroy(finder->arg); |
| 5678 |
); |
| 5679 |
drgn_dwarf_info_deinit(dbinfo); |
| 5680 |
hash_table_for_each(drgn_module_table, it, &dbinfo->modules) { |
| 5681 |
struct drgn_module *module = *it.entry; |
| 5682 |
do { |
| 5683 |
struct drgn_module *next = module->next_same_name; |
| 5684 |
drgn_module_destroy(module); |
| 5685 |
module = next; |
| 5686 |
} while (module); |
| 5687 |
} |
| 5688 |
drgn_module_table_deinit(&dbinfo->modules); |
| 5689 |
} |
| 5690 |
|
| 5691 |
struct drgn_elf_file *drgn_module_find_dwarf_file(struct drgn_module *module, |
| 5692 |
Dwarf *dwarf) |
| 5693 |
{ |
| 5694 |
if (!module->debug_file) |
| 5695 |
return NULL; |
| 5696 |
if (dwarf == module->debug_file->_dwarf) |
| 5697 |
return module->debug_file; |
| 5698 |
if (module->supplementary_debug_file |
| 5699 |
&& dwarf == module->supplementary_debug_file->_dwarf) |
| 5700 |
return module->supplementary_debug_file; |
| 5701 |
struct drgn_elf_file_dwarf_table_iterator it = |
| 5702 |
drgn_elf_file_dwarf_table_search(&module->split_dwarf_files, |
| 5703 |
&dwarf); |
| 5704 |
return it.entry ? *it.entry : NULL; |
| 5705 |
} |
| 5706 |
|
| 5707 |
struct drgn_error * |
| 5708 |
drgn_module_create_split_dwarf_file(struct drgn_module *module, |
| 5709 |
const char *name, Dwarf *dwarf, |
| 5710 |
struct drgn_elf_file **ret) |
| 5711 |
{ |
| 5712 |
struct drgn_error *err; |
| 5713 |
err = drgn_elf_file_create(module, name, -1, NULL, dwarf_getelf(dwarf), |
| 5714 |
ret); |
| 5715 |
if (err) |
| 5716 |
return err; |
| 5717 |
(*ret)->_dwarf = dwarf; |
| 5718 |
int r = drgn_elf_file_dwarf_table_insert(&module->split_dwarf_files, |
| 5719 |
ret, NULL); |
| 5720 |
if (r < 0) { |
| 5721 |
drgn_elf_file_destroy(*ret); |
| 5722 |
return &drgn_enomem; |
| 5723 |
} |
| 5724 |
assert(r > 0); |
| 5725 |
return NULL; |
| 5726 |
} |
| 5727 |
|
| 5728 |
struct drgn_error * |
| 5729 |
drgn_module_find_cfi(struct drgn_program *prog, struct drgn_module *module, |
| 5730 |
uint64_t pc, struct drgn_elf_file **file_ret, |
| 5731 |
struct drgn_cfi_row **row_ret, bool *interrupted_ret, |
| 5732 |
drgn_register_number *ret_addr_regno_ret) |
| 5733 |
{ |
| 5734 |
struct drgn_error *err; |
| 5735 |
|
| 5736 |
// If the file's platform doesn't match the program's, we can't use its |
| 5737 |
// CFI. |
| 5738 |
const bool can_use_loaded_file = |
| 5739 |
(module->loaded_file && |
| 5740 |
drgn_platforms_equal(&module->loaded_file->platform, |
| 5741 |
&prog->platform)); |
| 5742 |
const bool can_use_debug_file = |
| 5743 |
(module->debug_file && |
| 5744 |
drgn_platforms_equal(&module->debug_file->platform, |
| 5745 |
&prog->platform)); |
| 5746 |
|
| 5747 |
bool prefer_orc = false; |
| 5748 |
if (can_use_debug_file) { |
| 5749 |
if (!module->parsed_debug_frame) { |
| 5750 |
err = drgn_module_parse_debug_frame(module); |
| 5751 |
if (err) |
| 5752 |
return err; |
| 5753 |
module->parsed_debug_frame = true; |
| 5754 |
} |
| 5755 |
if (!module->parsed_orc) { |
| 5756 |
err = drgn_module_parse_orc(module, false); |
| 5757 |
if (err) |
| 5758 |
return err; |
| 5759 |
|
| 5760 |
// For some distributions, such as Fedora & derivatives, |
| 5761 |
// ORC sections are stripped from the debug file. Try |
| 5762 |
// using built-in ORC if nothing was loaded from the |
| 5763 |
// debug_file. |
| 5764 |
if (!module->orc.num_entries) |
| 5765 |
err = drgn_module_parse_orc(module, true); |
| 5766 |
if (err) |
| 5767 |
return err; |
| 5768 |
|
| 5769 |
module->parsed_orc = true; |
| 5770 |
} |
| 5771 |
|
| 5772 |
prefer_orc = drgn_module_should_prefer_orc_cfi(module, pc); |
| 5773 |
|
| 5774 |
*file_ret = module->debug_file; |
| 5775 |
if (prefer_orc) { |
| 5776 |
err = drgn_module_find_orc_cfi(module, pc, row_ret, |
| 5777 |
interrupted_ret, |
| 5778 |
ret_addr_regno_ret); |
| 5779 |
if (err != &drgn_not_found) |
| 5780 |
return err; |
| 5781 |
} |
| 5782 |
err = drgn_module_find_dwarf_cfi(module, pc, row_ret, |
| 5783 |
interrupted_ret, |
| 5784 |
ret_addr_regno_ret); |
| 5785 |
if (err != &drgn_not_found) |
| 5786 |
return err; |
| 5787 |
} |
| 5788 |
|
| 5789 |
if (can_use_loaded_file) { |
| 5790 |
if (!module->parsed_eh_frame) { |
| 5791 |
err = drgn_module_parse_eh_frame(module); |
| 5792 |
if (err) |
| 5793 |
return err; |
| 5794 |
module->parsed_eh_frame = true; |
| 5795 |
} |
| 5796 |
*file_ret = module->loaded_file; |
| 5797 |
err = drgn_module_find_eh_cfi(module, pc, row_ret, |
| 5798 |
interrupted_ret, |
| 5799 |
ret_addr_regno_ret); |
| 5800 |
if (err != &drgn_not_found) |
| 5801 |
return err; |
| 5802 |
} |
| 5803 |
|
| 5804 |
if (can_use_debug_file && !prefer_orc) { |
| 5805 |
err = drgn_module_find_orc_cfi(module, pc, row_ret, |
| 5806 |
interrupted_ret, |
| 5807 |
ret_addr_regno_ret); |
| 5808 |
if (err != &drgn_not_found) |
| 5809 |
return err; |
| 5810 |
} |
| 5811 |
|
| 5812 |
if (!can_use_debug_file) { |
| 5813 |
if (!module->parsed_orc) { |
| 5814 |
err = drgn_module_parse_orc(module, true); |
| 5815 |
if (err) |
| 5816 |
return err; |
| 5817 |
module->parsed_orc = true; |
| 5818 |
} |
| 5819 |
*file_ret = NULL; |
| 5820 |
err = drgn_module_find_orc_cfi(module, pc, row_ret, |
| 5821 |
interrupted_ret, |
| 5822 |
ret_addr_regno_ret); |
| 5823 |
if (err != &drgn_not_found) |
| 5824 |
return err; |
| 5825 |
} |
| 5826 |
|
| 5827 |
return &drgn_not_found; |
| 5828 |
} |