| 1 |
// Copyright 2019 - Serapheim Dimitropoulos |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include <inttypes.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include "linux_kernel.h" |
| 10 |
#include "plugins.h" |
| 11 |
#include "program.h" // IWYU pragma: associated |
| 12 |
#include "util.h" |
| 13 |
|
| 14 |
static struct drgn_error *drgn_platform_from_kdump(kdump_ctx_t *ctx, |
| 15 |
struct drgn_platform *ret) |
| 16 |
{ |
| 17 |
kdump_status ks; |
| 18 |
const char *str; |
| 19 |
kdump_num_t num; |
| 20 |
const struct drgn_architecture_info *arch; |
| 21 |
bool is_64_bit, is_little_endian; |
| 22 |
|
| 23 |
ks = kdump_get_string_attr(ctx, KDUMP_ATTR_ARCH_NAME, &str); |
| 24 |
if (ks != KDUMP_OK) { |
| 25 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 26 |
"kdump_get_string_attr(KDUMP_ATTR_ARCH_NAME): %s", |
| 27 |
kdump_get_err(ctx)); |
| 28 |
} |
| 29 |
if (strcmp(str, KDUMP_ARCH_X86_64) == 0) |
| 30 |
arch = &arch_info_x86_64; |
| 31 |
else if (strcmp(str, KDUMP_ARCH_IA32) == 0) |
| 32 |
arch = &arch_info_i386; |
| 33 |
else if (strcmp(str, KDUMP_ARCH_AARCH64) == 0) |
| 34 |
arch = &arch_info_aarch64; |
| 35 |
else if (strcmp(str, KDUMP_ARCH_ARM) == 0) |
| 36 |
arch = &arch_info_arm; |
| 37 |
else if (strcmp(str, KDUMP_ARCH_PPC64) == 0) |
| 38 |
arch = &arch_info_ppc64; |
| 39 |
else if (strcmp(str, KDUMP_ARCH_S390X) == 0) |
| 40 |
arch = &arch_info_s390x; |
| 41 |
else if (strcmp(str, KDUMP_ARCH_S390) == 0) |
| 42 |
arch = &arch_info_s390; |
| 43 |
#if KDUMPFILE_VERSION >= KDUMPFILE_MKVER(0, 5, 4) |
| 44 |
else if (strcmp(str, KDUMP_ARCH_RISCV64) == 0) |
| 45 |
arch = &arch_info_riscv64; |
| 46 |
else if (strcmp(str, KDUMP_ARCH_RISCV32) == 0) |
| 47 |
arch = &arch_info_riscv32; |
| 48 |
#endif |
| 49 |
else |
| 50 |
arch = &arch_info_unknown; |
| 51 |
|
| 52 |
ks = kdump_get_number_attr(ctx, KDUMP_ATTR_PTR_SIZE, &num); |
| 53 |
if (ks != KDUMP_OK) { |
| 54 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 55 |
"kdump_get_number_attr(KDUMP_ATTR_PTR_SIZE): %s", |
| 56 |
kdump_get_err(ctx)); |
| 57 |
} |
| 58 |
is_64_bit = num == 8; |
| 59 |
|
| 60 |
ks = kdump_get_number_attr(ctx, KDUMP_ATTR_BYTE_ORDER, &num); |
| 61 |
if (ks != KDUMP_OK) { |
| 62 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 63 |
"kdump_get_number_attr(KDUMP_ATTR_BYTE_ORDER): %s", |
| 64 |
kdump_get_err(ctx)); |
| 65 |
} |
| 66 |
is_little_endian = num == KDUMP_LITTLE_ENDIAN; |
| 67 |
|
| 68 |
drgn_platform_from_arch(arch, is_64_bit, is_little_endian, ret); |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
static struct drgn_error *drgn_platform_to_kdump(kdump_ctx_t *ctx, |
| 73 |
const struct drgn_platform *platform) |
| 74 |
{ |
| 75 |
kdump_status ks; |
| 76 |
char *arch_str = NULL; |
| 77 |
|
| 78 |
if (platform->arch == &arch_info_x86_64) |
| 79 |
arch_str = KDUMP_ARCH_X86_64; |
| 80 |
else if (platform->arch == &arch_info_i386) |
| 81 |
arch_str = KDUMP_ARCH_IA32; |
| 82 |
else if (platform->arch == &arch_info_aarch64) |
| 83 |
arch_str = KDUMP_ARCH_AARCH64; |
| 84 |
else if (platform->arch == &arch_info_arm) |
| 85 |
arch_str = KDUMP_ARCH_ARM; |
| 86 |
else if (platform->arch == &arch_info_ppc64) |
| 87 |
arch_str = KDUMP_ARCH_PPC64; |
| 88 |
else if (platform->arch == &arch_info_s390x) |
| 89 |
arch_str = KDUMP_ARCH_S390X; |
| 90 |
else if (platform->arch == &arch_info_s390) |
| 91 |
arch_str = KDUMP_ARCH_S390; |
| 92 |
#if KDUMPFILE_VERSION >= KDUMPFILE_MKVER(0, 5, 4) |
| 93 |
else if (platform->arch == &arch_info_riscv64) |
| 94 |
arch_str = KDUMP_ARCH_RISCV64; |
| 95 |
else if (platform->arch == &arch_info_riscv32) |
| 96 |
arch_str = KDUMP_ARCH_RISCV32; |
| 97 |
#endif |
| 98 |
|
| 99 |
if (arch_str) { |
| 100 |
ks = kdump_set_string_attr(ctx, KDUMP_ATTR_ARCH_NAME, arch_str); |
| 101 |
if (ks != KDUMP_OK) { |
| 102 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 103 |
"kdump_set_string_attr(\"%s\"): %s", |
| 104 |
arch_str, kdump_get_err(ctx)); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
static struct drgn_error *drgn_read_kdump(void *buf, uint64_t address, |
| 112 |
size_t count, uint64_t offset, |
| 113 |
void *arg, bool physical) |
| 114 |
{ |
| 115 |
kdump_ctx_t *ctx = arg; |
| 116 |
kdump_status ks; |
| 117 |
|
| 118 |
ks = kdump_read(ctx, physical ? KDUMP_KPHYSADDR : KDUMP_KVADDR, address, |
| 119 |
buf, &count); |
| 120 |
if (ks != KDUMP_OK) { |
| 121 |
return drgn_error_format_fault(address, |
| 122 |
"could not read memory from kdump: %s", |
| 123 |
kdump_get_err(ctx)); |
| 124 |
} |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
|
| 128 |
static bool prefer_drgn_vaddr_reader(void) |
| 129 |
{ |
| 130 |
const char *env = getenv("DRGN_PREFER_PGTABLE_READER"); |
| 131 |
return (env && atoi(env)); |
| 132 |
} |
| 133 |
|
| 134 |
struct drgn_error *drgn_program_set_kdump(struct drgn_program *prog) |
| 135 |
{ |
| 136 |
struct drgn_error *err; |
| 137 |
kdump_ctx_t *ctx; |
| 138 |
kdump_status ks; |
| 139 |
bool had_platform; |
| 140 |
bool had_vmcoreinfo = prog->vmcoreinfo.raw; |
| 141 |
|
| 142 |
ctx = kdump_new(); |
| 143 |
if (!ctx) { |
| 144 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 145 |
"kdump_new() failed"); |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* We need to be careful to set libkdumpfile attributes in the correct |
| 150 |
* order, in order to achive the desired result in the rare cases when |
| 151 |
* the program has the architecture and/or vmcoreinfo already set. In |
| 152 |
* these cases, frequently, the information is not available to |
| 153 |
* libkdumpfile, and so the user is providing it instead. We should set: |
| 154 |
* |
| 155 |
* - Architecture before file descriptor. This is because when |
| 156 |
* libkdumpfile reads the vmcore, it immediately parses the notes in |
| 157 |
* the header. If it doesn't know the current architecture, it will |
| 158 |
* skip the architecture-specific notes, such as the PRSTATUS. This |
| 159 |
* manifests to users as a missing "cpu.number" attribute, and thus a |
| 160 |
* failure to get accurate stack traces for on-CPU tasks. |
| 161 |
* - Vmcoreinfo after architecture. This is because when libkdumpfile |
| 162 |
* gets a new vmcoreinfo note set, it marks the address translation |
| 163 |
* metadata as dirty and resets its address translation info using the |
| 164 |
* new data. If no architecture is set, it skips this setup since it |
| 165 |
* won't know how to do so. This manifests for users as FaultError |
| 166 |
* when they access almost any memory address. |
| 167 |
* |
| 168 |
* For the common case, where we are just setting the FD and OS type, |
| 169 |
* and then letting libkdumpfile give us the vmcoreinfo and platform |
| 170 |
* info, the only ordering constraint is the obvious one: we need to set |
| 171 |
* the FD before getting data from libkdumpfile. |
| 172 |
*/ |
| 173 |
|
| 174 |
if (prog->has_platform) { |
| 175 |
err = drgn_platform_to_kdump(ctx, drgn_program_platform(prog)); |
| 176 |
if (err) |
| 177 |
goto err; |
| 178 |
} |
| 179 |
|
| 180 |
ks = kdump_set_number_attr(ctx, KDUMP_ATTR_FILE_FD, prog->core_fd); |
| 181 |
if (ks == KDUMP_ERR_NOTIMPL) { |
| 182 |
err = drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT, |
| 183 |
"%s", kdump_get_err(ctx)); |
| 184 |
goto err; |
| 185 |
} |
| 186 |
if (ks != KDUMP_OK) { |
| 187 |
err = drgn_error_format(DRGN_ERROR_OTHER, |
| 188 |
"kdump_set_number_attr(KDUMP_ATTR_FILE_FD): %s", |
| 189 |
kdump_get_err(ctx)); |
| 190 |
goto err; |
| 191 |
} |
| 192 |
ks = kdump_set_string_attr(ctx, KDUMP_ATTR_OSTYPE, "linux"); |
| 193 |
if (ks != KDUMP_OK) { |
| 194 |
err = drgn_error_format(DRGN_ERROR_OTHER, |
| 195 |
"kdump_set_string_attr(KDUMP_ATTR_OSTYPE): %s", |
| 196 |
kdump_get_err(ctx)); |
| 197 |
goto err; |
| 198 |
} |
| 199 |
|
| 200 |
if (prog->vmcoreinfo.raw) { |
| 201 |
#if KDUMPFILE_VERSION >= KDUMPFILE_MKVER(0, 4, 1) |
| 202 |
char *vmcoreinfo = memdup(prog->vmcoreinfo.raw, prog->vmcoreinfo.raw_size); |
| 203 |
if (!vmcoreinfo) { |
| 204 |
err = &drgn_enomem; |
| 205 |
goto err; |
| 206 |
} |
| 207 |
kdump_blob_t *blob = kdump_blob_new(vmcoreinfo, prog->vmcoreinfo.raw_size); |
| 208 |
if (!blob) { |
| 209 |
free(vmcoreinfo); |
| 210 |
err = &drgn_enomem; |
| 211 |
goto err; |
| 212 |
} |
| 213 |
kdump_attr_t attr; |
| 214 |
attr.type = KDUMP_BLOB; |
| 215 |
attr.val.blob = blob; |
| 216 |
ks = kdump_set_attr(ctx, "linux.vmcoreinfo.raw", &attr); |
| 217 |
if (ks != KDUMP_OK) { |
| 218 |
err = drgn_error_format(DRGN_ERROR_OTHER, |
| 219 |
"kdump_set_attr(linux.vmcoreinfo.raw): %s", |
| 220 |
kdump_get_err(ctx)); |
| 221 |
goto err; |
| 222 |
} |
| 223 |
#else |
| 224 |
err = drgn_error_create(DRGN_ERROR_NOT_IMPLEMENTED, |
| 225 |
"overriding vmcoreinfo is not supported in libkdumpfile < 0.4.1"); |
| 226 |
goto err; |
| 227 |
#endif |
| 228 |
} else { |
| 229 |
#if KDUMPFILE_VERSION >= KDUMPFILE_MKVER(0, 4, 1) |
| 230 |
char *vmcoreinfo; |
| 231 |
#else |
| 232 |
const char *vmcoreinfo; |
| 233 |
#endif |
| 234 |
ks = kdump_vmcoreinfo_raw(ctx, &vmcoreinfo); |
| 235 |
if (ks != KDUMP_OK) { |
| 236 |
err = drgn_error_format(DRGN_ERROR_OTHER, |
| 237 |
"kdump_vmcoreinfo_raw: %s", |
| 238 |
kdump_get_err(ctx)); |
| 239 |
goto err; |
| 240 |
} |
| 241 |
|
| 242 |
err = drgn_program_parse_vmcoreinfo(prog, vmcoreinfo, |
| 243 |
strlen(vmcoreinfo) + 1); |
| 244 |
/* |
| 245 |
* As of libkdumpfile 0.4.1, the string returned by |
| 246 |
* kdump_vmcoreinfo_raw() needs to be freed. |
| 247 |
*/ |
| 248 |
#if KDUMPFILE_VERSION >= KDUMPFILE_MKVER(0, 4, 1) |
| 249 |
free(vmcoreinfo); |
| 250 |
#endif |
| 251 |
if (err) |
| 252 |
goto err; |
| 253 |
} |
| 254 |
|
| 255 |
had_platform = prog->has_platform; |
| 256 |
if (!had_platform) { |
| 257 |
struct drgn_platform platform; |
| 258 |
err = drgn_platform_from_kdump(ctx, &platform); |
| 259 |
if (err) |
| 260 |
goto err; |
| 261 |
drgn_program_set_platform(prog, &platform); |
| 262 |
} |
| 263 |
|
| 264 |
// Drgn has fallback logic for some architectures which allows it to |
| 265 |
// figure out the virtual memory mappings of the kernel. Sometimes |
| 266 |
// libkdumpfile gets it wrong, for example when we're trying to debug |
| 267 |
// the dump capture kernel itself: libkdumpfile will still attempt to |
| 268 |
// use the original kernel's virtual memory mapping, while drgn knows |
| 269 |
// the correct mapping from the kdump kernel's vmcoreinfo. Use an |
| 270 |
// environment variable knob to opt-in to drgn's virtual memory reading |
| 271 |
// logic instead of libkdumpfile's. |
| 272 |
if (prefer_drgn_vaddr_reader()) |
| 273 |
err = drgn_program_add_memory_segment(prog, 0, UINT64_MAX, |
| 274 |
read_memory_via_pgtable, |
| 275 |
prog, false); |
| 276 |
else |
| 277 |
err = drgn_program_add_memory_segment(prog, 0, UINT64_MAX, |
| 278 |
drgn_read_kdump, ctx, false); |
| 279 |
if (err) |
| 280 |
goto err_platform; |
| 281 |
err = drgn_program_add_memory_segment(prog, 0, UINT64_MAX, |
| 282 |
drgn_read_kdump, ctx, true); |
| 283 |
if (err) |
| 284 |
goto err_segments; |
| 285 |
|
| 286 |
prog->flags |= DRGN_PROGRAM_IS_LINUX_KERNEL; |
| 287 |
err = drgn_program_finish_set_kernel(prog); |
| 288 |
if (err) |
| 289 |
goto err_segments; |
| 290 |
prog->kdump_ctx = ctx; |
| 291 |
drgn_call_plugins_prog("drgn_prog_set", prog); |
| 292 |
return NULL; |
| 293 |
|
| 294 |
err_segments: |
| 295 |
drgn_memory_reader_clear(&prog->reader); |
| 296 |
err_platform: |
| 297 |
prog->has_platform = had_platform; |
| 298 |
err: |
| 299 |
// Reset anything we parsed from vmcoreinfo |
| 300 |
if (!had_vmcoreinfo) { |
| 301 |
free(prog->vmcoreinfo.raw); |
| 302 |
memset(&prog->vmcoreinfo, 0, sizeof(prog->vmcoreinfo)); |
| 303 |
} |
| 304 |
kdump_free(ctx); |
| 305 |
return err; |
| 306 |
} |
| 307 |
|
| 308 |
struct drgn_error *drgn_program_cache_kdump_threads(struct drgn_program *prog) |
| 309 |
{ |
| 310 |
struct drgn_error *err; |
| 311 |
kdump_num_t ncpus, i; |
| 312 |
kdump_status ks; |
| 313 |
|
| 314 |
ks = kdump_get_number_attr(prog->kdump_ctx, "cpu.number", &ncpus); |
| 315 |
if (ks != KDUMP_OK) { |
| 316 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 317 |
"kdump_get_number_attr(cpu.number): %s", |
| 318 |
kdump_get_err(prog->kdump_ctx)); |
| 319 |
} |
| 320 |
|
| 321 |
/* |
| 322 |
* Note that in the following loop we never call kdump_blob_unpin() on |
| 323 |
* the prstatus blob that we get from libkdumpfile. Since drgn never |
| 324 |
* modifies the PRSTATUS attributes (neither directly nor indirectly), |
| 325 |
* we "leak" the blob pin until kdump_free() is called, which will |
| 326 |
* clean up everything for us. |
| 327 |
*/ |
| 328 |
for (i = 0; i < ncpus; i++) { |
| 329 |
kdump_attr_t prstatus_attr; |
| 330 |
void *prstatus_data; |
| 331 |
size_t prstatus_size; |
| 332 |
|
| 333 |
#define FORMAT "cpu.%" PRIuFAST64 ".PRSTATUS" |
| 334 |
/* Enough for the longest possible PRSTATUS attribute name. */ |
| 335 |
char attr_name[sizeof(FORMAT) |
| 336 |
- sizeof("%" PRIuFAST64) |
| 337 |
+ max_decimal_length(uint_fast64_t) |
| 338 |
+ 1]; |
| 339 |
snprintf(attr_name, sizeof(attr_name), FORMAT, i); |
| 340 |
#undef FORMAT |
| 341 |
#if KDUMPFILE_VERSION >= KDUMPFILE_MKVER(0, 5, 5) |
| 342 |
ks = kdump_get_typed_attr(prog->kdump_ctx, attr_name, |
| 343 |
KDUMP_BLOB, &prstatus_attr.val); |
| 344 |
#else |
| 345 |
prstatus_attr.type = KDUMP_BLOB; |
| 346 |
ks = kdump_get_typed_attr(prog->kdump_ctx, attr_name, |
| 347 |
&prstatus_attr); |
| 348 |
#endif |
| 349 |
if (ks != KDUMP_OK) { |
| 350 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 351 |
"kdump_get_typed_attr(%s): %s", |
| 352 |
attr_name, |
| 353 |
kdump_get_err(prog->kdump_ctx)); |
| 354 |
} |
| 355 |
|
| 356 |
prstatus_data = kdump_blob_pin(prstatus_attr.val.blob); |
| 357 |
prstatus_size = kdump_blob_size(prstatus_attr.val.blob); |
| 358 |
uint32_t _; |
| 359 |
err = drgn_program_cache_prstatus_entry(prog, prstatus_data, |
| 360 |
prstatus_size, &_); |
| 361 |
if (err) |
| 362 |
return err; |
| 363 |
} |
| 364 |
return NULL; |
| 365 |
} |