VirtualBox

source: vbox/trunk/src/VBox/VMM/DBGFCoreWrite.cpp@ 32534

Last change on this file since 32534 was 32534, checked in by vboxsync, 15 years ago

VMM/DBGFCoreWrite: log.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.3 KB
Line 
1/* $Id: DBGFCoreWrite.cpp 32534 2010-09-15 17:30:20Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Guest Core Dump.
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*
19 * VBox VMCore Format:
20 * [ ELF 64 Header] -- Only 1
21 *
22 * [ PT_NOTE ] -- Only 1
23 * - Offset into CoreDescriptor followed by list of Notes (Note Hdr + data) of VBox CPUs.
24 * - (Any Additional custom Note sections)
25 *
26 * [ PT_LOAD ] -- One for each contiguous memory chunk
27 * - Memory offset
28 * - File offset
29 *
30 * CoreDescriptor
31 * - Magic, VBox version
32 * - Number of CPus
33 *
34 * Per-CPU register dump
35 * - CPU 1 Note Hdr + Data
36 * - CPU 2 Note Hdr + Data
37 * ...
38 * (Additional custom notes Hdr+data)
39 * - VBox 1 Note Hdr + Data
40 * - VBox 2 Note Hdr + Data
41 * ...
42 * Memory dump
43 *
44 */
45
46/*******************************************************************************
47* Header Files *
48*******************************************************************************/
49#define LOG_GROUP LOG_GROUP_DBGF
50#include <iprt/param.h>
51#include <iprt/file.h>
52
53#include "DBGFInternal.h"
54
55#include <VBox/cpum.h>
56#include "CPUMInternal.h"
57#include <VBox/dbgf.h>
58#include <VBox/dbgfcorefmt.h>
59#include <VBox/vm.h>
60#include <VBox/pgm.h>
61#include <VBox/err.h>
62#include <VBox/log.h>
63#include <VBox/mm.h>
64#include <VBox/version.h>
65
66#include "../Runtime/include/internal/ldrELF64.h"
67
68/*******************************************************************************
69* Defined Constants And Macros *
70*******************************************************************************/
71#ifdef DEBUG_ramshankar
72# undef Log
73# define Log LogRel
74#endif
75#define DBGFLOG_NAME "DBGFCoreWrite"
76
77/*
78 * For now use Solaris-specific padding and namesz length (i.e. includes NULL terminator)
79 */
80static const int s_NoteAlign = 4; /* @todo see #5211 comment 3 */
81static const int s_cbNoteName = 16;
82
83/* These strings *HAVE* to be 8-byte aligned */
84static const char *s_pcszCoreVBoxCore = "VBCORE";
85static const char *s_pcszCoreVBoxCpu = "VBCPU";
86
87
88/**
89 * DBGFCOREDATA: Core data.
90 */
91typedef struct
92{
93 const char *pszDumpPath; /* File path to dump the core into. */
94} DBGFCOREDATA, *PDBGFCOREDATA;
95
96/**
97 * ELF function to write 64-bit ELF header.
98 *
99 * @param hFile The file to write to.
100 * @param cProgHdrs Number of program headers.
101 * @param cSecHdrs Number of section headers.
102 * @param pcbElfHdr Where to store the size of written header to file,
103 * can be NULL.
104 *
105 * @return IPRT status code.
106 */
107static int Elf64WriteElfHdr(RTFILE hFile, uint16_t cProgHdrs, uint16_t cSecHdrs, uint64_t *pcbElfHdr)
108{
109 Elf64_Ehdr ElfHdr;
110 RT_ZERO(ElfHdr);
111 ElfHdr.e_ident[EI_MAG0] = ELFMAG0;
112 ElfHdr.e_ident[EI_MAG1] = ELFMAG1;
113 ElfHdr.e_ident[EI_MAG2] = ELFMAG2;
114 ElfHdr.e_ident[EI_MAG3] = ELFMAG3;
115 ElfHdr.e_ident[EI_DATA] = ELFDATA2LSB;
116 ElfHdr.e_type = ET_CORE;
117 ElfHdr.e_version = EV_CURRENT;
118 ElfHdr.e_ident[EI_CLASS] = ELFCLASS64;
119 /* 32-bit VMs will produce cores with e_machine EM_386. */
120#ifdef RT_ARCH_AMD64
121 ElfHdr.e_machine = EM_X86_64;
122#else
123 ElfHdr.e_machine = EM_386;
124#endif
125 ElfHdr.e_phnum = cProgHdrs;
126 ElfHdr.e_shnum = cSecHdrs;
127 ElfHdr.e_ehsize = sizeof(ElfHdr);
128 ElfHdr.e_phoff = sizeof(ElfHdr);
129 ElfHdr.e_phentsize = sizeof(Elf64_Phdr);
130 ElfHdr.e_shentsize = sizeof(Elf64_Shdr);
131
132 int rc = RTFileWrite(hFile, &ElfHdr, sizeof(ElfHdr), NULL /* all */);
133 if (RT_SUCCESS(rc) && pcbElfHdr)
134 *pcbElfHdr = sizeof(ElfHdr);
135 return rc;
136}
137
138
139/**
140 * ELF function to write 64-bit program header.
141 *
142 * @param hFile The file to write to.
143 * @param Type Type of program header (PT_*).
144 * @param fFlags Flags (access permissions, PF_*).
145 * @param offFileData File offset of contents.
146 * @param cbFileData Size of contents in the file.
147 * @param cbMemData Size of contents in memory.
148 * @param Phys Physical address, pass zero if not applicable.
149 * @param pcbProgHdr Where to store the size of written header to file,
150 * can be NULL.
151 *
152 * @return IPRT status code.
153 */
154static int Elf64WriteProgHdr(RTFILE hFile, uint32_t Type, uint32_t fFlags, uint64_t offFileData, uint64_t cbFileData, uint64_t cbMemData,
155 RTGCPHYS Phys, uint64_t *pcbProgHdr)
156{
157 Elf64_Phdr ProgHdr;
158 RT_ZERO(ProgHdr);
159 ProgHdr.p_type = Type;
160 ProgHdr.p_flags = fFlags;
161 ProgHdr.p_offset = offFileData;
162 ProgHdr.p_filesz = cbFileData;
163 ProgHdr.p_memsz = cbMemData;
164 ProgHdr.p_paddr = Phys;
165
166 int rc = RTFileWrite(hFile, &ProgHdr, sizeof(ProgHdr), NULL /* all */);
167 if (RT_SUCCESS(rc) && pcbProgHdr)
168 *pcbProgHdr = sizeof(ProgHdr);
169 return rc;
170}
171
172
173/**
174 * Returns the size of the NOTE section given the name and size of the data.
175 *
176 * @param pszName Name of the note section.
177 * @param cb Size of the data portion of the note section.
178 *
179 * @return The size of the NOTE section as rounded to the file alignment.
180 */
181static inline uint64_t Elf64NoteSectionSize(const char *pszName, uint64_t cbData)
182{
183 uint64_t cbNote = sizeof(Elf64_Nhdr);
184
185 size_t cbName = strlen(pszName) + 1;
186 size_t cbNameAlign = RT_ALIGN_Z(cbName, s_NoteAlign);
187
188 cbNote += cbNameAlign;
189 cbNote += RT_ALIGN_64(cbData, s_NoteAlign);
190 return cbNote;
191}
192
193
194/**
195 * Elf function to write 64-bit note header.
196 *
197 * @param hFile The file to write to.
198 * @param Type Type of this section.
199 * @param pszName Name of this section.
200 * @param pcv Opaque pointer to the data, if NULL only computes size.
201 * @param cbData Size of the data.
202 * @param pcbNoteHdr Where to store the size of written header to file,
203 * can be NULL.
204 *
205 * @return IPRT status code.
206 */
207static int Elf64WriteNoteHdr(RTFILE hFile, uint16_t Type, const char *pszName, const void *pcvData, uint64_t cbData, uint64_t *pcbNoteHdr)
208{
209 AssertReturn(pcvData, VERR_INVALID_POINTER);
210 AssertReturn(cbData > 0, VERR_NO_DATA);
211
212 char szNoteName[s_cbNoteName];
213 RT_ZERO(szNoteName);
214 RTStrCopy(szNoteName, sizeof(szNoteName), pszName);
215
216 size_t cbName = strlen(szNoteName) + 1;
217 size_t cbNameAlign = RT_ALIGN_Z(cbName, s_NoteAlign);
218 uint64_t cbDataAlign = RT_ALIGN_64(cbData, s_NoteAlign);
219
220 /*
221 * Yell loudly and bail if we are going to be writing a core file that is not compatible with
222 * both Solaris and the 64-bit ELF spec. which dictates 8-byte alignment. See #5211 comment 3.
223 */
224 if (cbNameAlign % 8)
225 {
226 LogRel((DBGFLOG_NAME ":Elf64WriteNoteHdr pszName=%s cbNameAlign=%u, not 8-byte aligned!\n", pszName, cbNameAlign));
227 return VERR_INVALID_PARAMETER;
228 }
229
230 if (cbDataAlign % 8)
231 {
232 LogRel((DBGFLOG_NAME ":Elf64WriteNoteHdr pszName=%s cbDataAlign=%u, not 8-byte aligned!\n", pszName, cbDataAlign));
233 return VERR_INVALID_PARAMETER;
234 }
235
236 static const char s_achPad[7] = { 0, 0, 0, 0, 0, 0, 0 };
237 AssertCompile(sizeof(s_achPad) >= s_NoteAlign - 1);
238
239 Elf64_Nhdr ElfNoteHdr;
240 RT_ZERO(ElfNoteHdr);
241 ElfNoteHdr.n_namesz = (Elf64_Word)cbName - 1; /* Again a discrepancy between Elf64 and Solaris (#5211 comment 3) */
242 ElfNoteHdr.n_type = Type;
243 ElfNoteHdr.n_descsz = (Elf64_Word)cbDataAlign;
244
245 /*
246 * Write note header.
247 */
248 int rc = RTFileWrite(hFile, &ElfNoteHdr, sizeof(ElfNoteHdr), NULL /* all */);
249 if (RT_SUCCESS(rc))
250 {
251 /*
252 * Write note name.
253 */
254 rc = RTFileWrite(hFile, szNoteName, cbName, NULL /* all */);
255 if (RT_SUCCESS(rc))
256 {
257 /*
258 * Write note name padding if required.
259 */
260 if (cbNameAlign > cbName)
261 rc = RTFileWrite(hFile, s_achPad, cbNameAlign - cbName, NULL);
262
263 if (RT_SUCCESS(rc))
264 {
265 /*
266 * Write note data.
267 */
268 rc = RTFileWrite(hFile, pcvData, cbData, NULL /* all */);
269 if (RT_SUCCESS(rc))
270 {
271 /*
272 * Write note data padding if required.
273 */
274 if (cbDataAlign > cbData)
275 rc = RTFileWrite(hFile, s_achPad, cbDataAlign - cbData, NULL /* all*/);
276 }
277 }
278 }
279 }
280
281 if (RT_FAILURE(rc))
282 LogRel((DBGFLOG_NAME ":RTFileWrite failed. rc=%Rrc pszName=%s cbData=%u cbDataAlign=%u\n", rc, pszName, cbData, cbDataAlign));
283
284 return rc;
285}
286
287
288/**
289 * Count the number of memory ranges that go into the core file.
290 *
291 * We cannot do a page-by-page dump of the entire guest memory as there will be
292 * way too many program header entries. Also we don't want to dump MMIO regions
293 * which means we cannot have a 1:1 mapping between core file offset and memory
294 * offset. Instead we dump the memory in ranges. A memory range is a contiguous
295 * memory area suitable for dumping to a core file.
296 *
297 * @param pVM The VM handle.
298 *
299 * @return Number of memory ranges
300 */
301static uint32_t dbgfR3GetRamRangeCount(PVM pVM)
302{
303 return PGMR3PhysGetRamRangeCount(pVM);
304}
305
306
307/**
308 * EMT Rendezvous worker function for DBGFR3CoreWrite.
309 *
310 * @param pVM The VM handle.
311 * @param pVCpu The handle of the calling VCPU.
312 * @param pvData Opaque data.
313 *
314 * @return VBox status code.
315 */
316static DECLCALLBACK(VBOXSTRICTRC) dbgfR3CoreWrite(PVM pVM, PVMCPU pVCpu, void *pvData)
317{
318 /*
319 * Validate input.
320 */
321 AssertReturn(pVM, VERR_INVALID_VM_HANDLE);
322 AssertReturn(pVCpu, VERR_INVALID_VMCPU_HANDLE);
323 AssertReturn(pvData, VERR_INVALID_POINTER);
324
325 PDBGFCOREDATA pDbgfData = (PDBGFCOREDATA)pvData;
326
327 /*
328 * Collect core information.
329 */
330 uint32_t u32MemRanges = dbgfR3GetRamRangeCount(pVM);
331 uint16_t cMemRanges = u32MemRanges < UINT16_MAX - 1 ? u32MemRanges : UINT16_MAX - 1; /* One PT_NOTE Program header */
332 uint16_t cProgHdrs = cMemRanges + 1;
333
334 DBGFCOREDESCRIPTOR CoreDescriptor;
335 RT_ZERO(CoreDescriptor);
336 CoreDescriptor.u32Magic = DBGFCORE_MAGIC;
337 CoreDescriptor.u32FmtVersion = DBGFCORE_FMT_VERSION;
338 CoreDescriptor.cbSelf = sizeof(CoreDescriptor);
339 CoreDescriptor.u32VBoxVersion = VBOX_FULL_VERSION;
340 CoreDescriptor.u32VBoxRevision = VMMGetSvnRev();
341 CoreDescriptor.cCpus = pVM->cCpus;
342
343 Log((DBGFLOG_NAME ":CoreDescriptor Version=%u Revision=%u\n", CoreDescriptor.u32VBoxVersion, CoreDescriptor.u32VBoxRevision));
344
345 /*
346 * Compute total size of the note section.
347 */
348 uint64_t cbNoteSection = Elf64NoteSectionSize(s_pcszCoreVBoxCore, sizeof(CoreDescriptor))
349 + pVM->cCpus * Elf64NoteSectionSize(s_pcszCoreVBoxCpu, sizeof(CPUMCTX));
350 uint64_t off = 0;
351
352 /*
353 * Create the core file.
354 */
355 RTFILE hFile = NIL_RTFILE;
356 int rc = RTFileOpen(&hFile, pDbgfData->pszDumpPath, RTFILE_O_CREATE_REPLACE | RTFILE_O_READWRITE);
357 if (RT_FAILURE(rc))
358 {
359 LogRel((DBGFLOG_NAME ":RTFileOpen failed for '%s' rc=%Rrc\n", pDbgfData->pszDumpPath, rc));
360 return rc;
361 }
362
363 /*
364 * Write ELF header.
365 */
366 uint64_t cbElfHdr = 0;
367 uint64_t cbProgHdr = 0;
368 uint64_t offMemRange = 0;
369 rc = Elf64WriteElfHdr(hFile, cProgHdrs, 0 /* cSecHdrs */, &cbElfHdr);
370 off += cbElfHdr;
371 if (RT_FAILURE(rc))
372 {
373 LogRel((DBGFLOG_NAME ":Elf64WriteElfHdr failed. rc=%Rrc\n", rc));
374 goto CoreWriteDone;
375 }
376
377 /*
378 * Write PT_NOTE program header.
379 */
380 rc = Elf64WriteProgHdr(hFile, PT_NOTE, PF_R,
381 cbElfHdr + cProgHdrs * sizeof(Elf64_Phdr), /* file offset to contents */
382 cbNoteSection, /* size in core file */
383 cbNoteSection, /* size in memory */
384 0, /* physical address */
385 &cbProgHdr);
386 Assert(cbProgHdr == sizeof(Elf64_Phdr));
387 off += cbProgHdr;
388
389 if (RT_FAILURE(rc))
390 {
391 LogRel((DBGFLOG_NAME ":Elf64WritreProgHdr failed for PT_NOTE. rc=%Rrc\n", rc));
392 goto CoreWriteDone;
393 }
394
395 /*
396 * Write PT_LOAD program header for each memory range.
397 */
398 offMemRange = off + cbNoteSection;
399 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
400 {
401 RTGCPHYS GCPhysStart;
402 RTGCPHYS GCPhysEnd;
403
404 bool fIsMmio;
405 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
406 if (RT_FAILURE(rc))
407 {
408 LogRel((DBGFLOG_NAME ": PGMR3PhysGetRange failed for iRange(%u) rc=%Rrc\n", iRange, rc));
409 goto CoreWriteDone;
410 }
411
412 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
413 uint64_t cbFileRange = fIsMmio ? 0 : cbMemRange;
414
415 Log((DBGFLOG_NAME ": PGMR3PhysGetRange iRange=%u GCPhysStart=%#x GCPhysEnd=%#x cbMemRange=%u\n",
416 iRange, GCPhysStart, GCPhysEnd, cbMemRange));
417
418 rc = Elf64WriteProgHdr(hFile, PT_LOAD, PF_R,
419 offMemRange, /* file offset to contents */
420 cbFileRange, /* size in core file */
421 cbMemRange, /* size in memory */
422 GCPhysStart, /* physical address */
423 &cbProgHdr);
424 Assert(cbProgHdr == sizeof(Elf64_Phdr));
425 if (RT_FAILURE(rc))
426 {
427 LogRel((DBGFLOG_NAME ":Elf64WriteProgHdr failed for memory range(%u) cbFileRange=%u cbMemRange=%u rc=%Rrc\n", iRange,
428 cbFileRange, cbMemRange, rc));
429 goto CoreWriteDone;
430 }
431
432 offMemRange += cbFileRange;
433 }
434
435 /*
436 * Write the Core descriptor note header and data.
437 */
438 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCORE, s_pcszCoreVBoxCore, &CoreDescriptor, sizeof(CoreDescriptor),
439 NULL /* pcbNoteHdr */);
440 if (RT_FAILURE(rc))
441 {
442 LogRel((DBGFLOG_NAME ":Elf64WriteNoteHdr failed for Note '%s' rc=%Rrc\n", s_pcszCoreVBoxCore, rc));
443 goto CoreWriteDone;
444 }
445
446 /*
447 * Write the CPU context note headers and data.
448 */
449 for (uint32_t iCpu = 0; iCpu < pVM->cCpus; iCpu++)
450 {
451 PCPUMCTX pCpuCtx = &pVM->aCpus[iCpu].cpum.s.Guest;
452 rc = Elf64WriteNoteHdr(hFile, NT_VBOXCPU, s_pcszCoreVBoxCpu, pCpuCtx, sizeof(CPUMCTX), NULL /* pcbNoteHdr */);
453 if (RT_FAILURE(rc))
454 {
455 LogRel((DBGFLOG_NAME ":Elf64WriteNoteHdr failed for vCPU[%u] rc=%Rrc\n", iCpu, rc));
456 goto CoreWriteDone;
457 }
458 }
459
460 /*
461 * Write memory ranges.
462 */
463 for (uint16_t iRange = 0; iRange < cMemRanges; iRange++)
464 {
465 RTGCPHYS GCPhysStart;
466 RTGCPHYS GCPhysEnd;
467 bool fIsMmio;
468 rc = PGMR3PhysGetRange(pVM, iRange, &GCPhysStart, &GCPhysEnd, NULL /* pszDesc */, &fIsMmio);
469 if (RT_FAILURE(rc))
470 {
471 LogRel((DBGFLOG_NAME ":PGMR3PhysGetRange(2) failed for iRange(%u) rc=%Rrc\n", iRange, rc));
472 goto CoreWriteDone;
473 }
474
475 if (fIsMmio)
476 continue;
477
478 /*
479 * Write page-by-page of this memory range.
480 */
481 uint64_t cbMemRange = GCPhysEnd - GCPhysStart + 1;
482 uint64_t cPages = cbMemRange >> PAGE_SHIFT;
483 for (uint64_t iPage = 0; iPage < cPages; iPage++)
484 {
485 const int cbBuf = PAGE_SIZE;
486 void *pvBuf = MMR3HeapAlloc(pVM, MM_TAG_DBGF_CORE_WRITE, cbBuf);
487 if (RT_UNLIKELY(!pvBuf))
488 {
489 LogRel((DBGFLOG_NAME ":MMR3HeapAlloc failed. iRange=%u iPage=%u\n", iRange, iPage));
490 goto CoreWriteDone;
491 }
492
493 rc = PGMPhysRead(pVM, GCPhysStart, pvBuf, cbBuf);
494 if (RT_FAILURE(rc))
495 {
496 /*
497 * For some reason this failed, write out a zero page instead.
498 */
499 LogRel((DBGFLOG_NAME ":PGMPhysRead failed for iRange=%u iPage=%u. rc=%Rrc. Ignoring...\n", iRange,
500 iPage, rc));
501 memset(pvBuf, 0, cbBuf);
502 }
503
504 rc = RTFileWrite(hFile, pvBuf, cbBuf, NULL /* all */);
505 if (RT_FAILURE(rc))
506 {
507 LogRel((DBGFLOG_NAME ":RTFileWrite failed. iRange=%u iPage=%u rc=%Rrc\n", iRange, iPage, rc));
508 MMR3HeapFree(pvBuf);
509 goto CoreWriteDone;
510 }
511
512 MMR3HeapFree(pvBuf);
513 }
514 }
515
516CoreWriteDone:
517 RTFileClose(hFile);
518
519 return rc;
520}
521
522
523/**
524 * Write core dump of the guest.
525 *
526 * @return VBox status code.
527 * @param pVM The VM handle.
528 * @param pszDumpPath The path of the file to dump into, cannot be
529 * NULL.
530 *
531 * @remarks The VM must be suspended before calling this function.
532 */
533VMMR3DECL(int) DBGFR3CoreWrite(PVM pVM, const char *pszDumpPath)
534{
535 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
536 AssertReturn(pszDumpPath, VERR_INVALID_HANDLE);
537
538 /*
539 * Pass the core write request down to EMT rendezvous which makes sure
540 * other EMTs, if any, are not running. IO threads could still be running
541 * but we don't care about them.
542 */
543 DBGFCOREDATA CoreData;
544 RT_ZERO(CoreData);
545 CoreData.pszDumpPath = pszDumpPath;
546
547 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, dbgfR3CoreWrite, &CoreData);
548 if (RT_SUCCESS(rc))
549 LogRel((DBGFLOG_NAME ":Successfully wrote guest core '%s'\n", pszDumpPath));
550 else
551 LogRel((DBGFLOG_NAME ":Failed to write guest core '%s' (%Rrc)\n", pszDumpPath, rc));
552 return rc;
553}
554
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette