| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "analyze.h" |
| 7 |
#include "analyze-inspect-elf.h" |
| 8 |
#include "chase.h" |
| 9 |
#include "elf-util.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "format-table.h" |
| 12 |
#include "json-util.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "strv.h" |
| 15 |
|
| 16 |
static int analyze_elf(char **filenames, sd_json_format_flags_t json_flags) { |
| 17 |
int r; |
| 18 |
|
| 19 |
STRV_FOREACH(filename, filenames) { |
| 20 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *package_metadata = NULL; |
| 21 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 22 |
_cleanup_free_ char *abspath = NULL, *stacktrace = NULL; |
| 23 |
_cleanup_close_ int fd = -EBADF; |
| 24 |
bool coredump = false; |
| 25 |
|
| 26 |
fd = chase_and_open(*filename, arg_root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC, &abspath); |
| 27 |
if (fd < 0) |
| 28 |
return log_error_errno(fd, "Could not open \"%s\": %m", *filename); |
| 29 |
|
| 30 |
r = parse_elf_object( |
| 31 |
fd, |
| 32 |
abspath, |
| 33 |
arg_root, |
| 34 |
/* fork_disable_dump= */ false, |
| 35 |
&stacktrace, |
| 36 |
&package_metadata, |
| 37 |
/* ret_dlopen_metadata= */ NULL); |
| 38 |
if (r < 0) |
| 39 |
return log_error_errno(r, "Parsing \"%s\" as ELF object failed: %m", abspath); |
| 40 |
|
| 41 |
t = table_new_vertical(); |
| 42 |
if (!t) |
| 43 |
return log_oom(); |
| 44 |
|
| 45 |
r = table_add_many( |
| 46 |
t, |
| 47 |
TABLE_FIELD, "path", |
| 48 |
TABLE_STRING, abspath); |
| 49 |
if (r < 0) |
| 50 |
return table_log_add_error(r); |
| 51 |
|
| 52 |
if (package_metadata) { |
| 53 |
sd_json_variant *module_json; |
| 54 |
const char *module_name; |
| 55 |
|
| 56 |
JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, package_metadata) { |
| 57 |
const char *field_name; |
| 58 |
sd_json_variant *field; |
| 59 |
|
| 60 |
/* The ELF type and architecture are added as top-level objects, |
| 61 |
* since they are only parsed for the file itself, but the packaging |
| 62 |
* metadata is parsed recursively in core files, so there might be |
| 63 |
* multiple modules. */ |
| 64 |
if (STR_IN_SET(module_name, "elfType", "elfArchitecture")) { |
| 65 |
if (streq(module_name, "elfType") && streq(sd_json_variant_string(module_json), "coredump")) |
| 66 |
coredump = true; |
| 67 |
|
| 68 |
r = table_add_many( |
| 69 |
t, |
| 70 |
TABLE_FIELD, module_name, |
| 71 |
TABLE_STRING, sd_json_variant_string(module_json)); |
| 72 |
if (r < 0) |
| 73 |
return table_log_add_error(r); |
| 74 |
|
| 75 |
continue; |
| 76 |
} |
| 77 |
|
| 78 |
/* path/elfType/elfArchitecture come first just once per file, |
| 79 |
* then we might have multiple modules, so add a separator between |
| 80 |
* them to make the output more readable. */ |
| 81 |
r = table_add_many(t, TABLE_EMPTY, TABLE_EMPTY); |
| 82 |
if (r < 0) |
| 83 |
return table_log_add_error(r); |
| 84 |
|
| 85 |
/* In case of core files the module name will be the executable, |
| 86 |
* but for binaries/libraries it's just the path, so don't print it |
| 87 |
* twice. */ |
| 88 |
if (!streq(abspath, module_name)) { |
| 89 |
r = table_add_many( |
| 90 |
t, |
| 91 |
TABLE_FIELD, "module name", |
| 92 |
TABLE_STRING, module_name); |
| 93 |
if (r < 0) |
| 94 |
return table_log_add_error(r); |
| 95 |
} |
| 96 |
|
| 97 |
JSON_VARIANT_OBJECT_FOREACH(field_name, field, module_json) |
| 98 |
if (sd_json_variant_is_string(field)) { |
| 99 |
r = table_add_many( |
| 100 |
t, |
| 101 |
TABLE_FIELD, field_name, |
| 102 |
TABLE_STRING, sd_json_variant_string(field)); |
| 103 |
if (r < 0) |
| 104 |
return table_log_add_error(r); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if (coredump) { |
| 110 |
r = table_add_many(t, |
| 111 |
TABLE_EMPTY, TABLE_EMPTY, |
| 112 |
TABLE_FIELD, "stacktrace", |
| 113 |
TABLE_STRING, stacktrace); |
| 114 |
if (r < 0) |
| 115 |
return table_log_add_error(r); |
| 116 |
} |
| 117 |
|
| 118 |
if (sd_json_format_enabled(json_flags)) |
| 119 |
sd_json_variant_dump(package_metadata, json_flags, stdout, NULL); |
| 120 |
else { |
| 121 |
r = table_print_or_warn(t); |
| 122 |
if (r < 0) |
| 123 |
return r; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
int verb_elf_inspection(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 131 |
pager_open(arg_pager_flags); |
| 132 |
|
| 133 |
return analyze_elf(strv_skip(argv, 1), arg_json_format_flags); |
| 134 |
} |