VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/solaris/coredumper-solaris.cpp@ 31905

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

coredumper-solaris.cpp: RTFileGetSize returns uint64_t and this cannot be safely cast to size_t! Please fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 73.2 KB
Line 
1/* $Id: coredumper-solaris.cpp 31869 2010-08-23 15:22:50Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Core Dumper.
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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#define LOG_GROUP LOG_GROUP_CORE_DUMPER
31#include <VBox/log.h>
32#include <iprt/coredumper.h>
33#include <iprt/types.h>
34#include <iprt/file.h>
35#include <iprt/err.h>
36#include <iprt/dir.h>
37#include <iprt/path.h>
38#include <iprt/string.h>
39#include <iprt/thread.h>
40#include <iprt/param.h>
41#include <iprt/asm.h>
42#include "coredumper-solaris.h"
43
44#ifdef RT_OS_SOLARIS
45# include <syslog.h>
46# include <signal.h>
47# include <unistd.h>
48# include <errno.h>
49# include <zone.h>
50# include <sys/proc.h>
51# include <sys/sysmacros.h>
52# include <sys/systeminfo.h>
53# include <sys/mman.h>
54#endif /* RT_OS_SOLARIS */
55
56/*******************************************************************************
57* Globals *
58*******************************************************************************/
59volatile static uint64_t g_CoreDumpThread = NIL_RTTHREAD;
60volatile static bool g_fCoreDumpSignalSetup = false;
61volatile static bool g_fCoreDumpDeliberate = false;
62volatile static bool g_fCoreDumpInProgress = false;
63volatile static uint32_t g_fCoreDumpFlags = 0;
64static char g_szCoreDumpDir[PATH_MAX] = { 0 };
65static char g_szCoreDumpFile[PATH_MAX] = { 0 };
66
67
68/*******************************************************************************
69* Defined Constants And Macros *
70*******************************************************************************/
71#define CORELOG_NAME "CoreDumper: "
72#define CORELOG(a) Log(a)
73#define CORELOGRELSYS(a) \
74 do { \
75 LogRel(a); \
76 rtCoreDumperSysLogWrapper a; \
77 } while (0)
78
79
80/**
81 * Wrapper function to write IPRT format style string to the syslog.
82 *
83 * @param pszFormat Format string
84 */
85static void rtCoreDumperSysLogWrapper(const char *pszFormat, ...)
86{
87 va_list va;
88 va_start(va, pszFormat);
89 char szBuf[1024];
90 RTStrPrintfV(szBuf, sizeof(szBuf), pszFormat, va);
91 va_end(va);
92 syslog(LOG_ERR, "%s", szBuf);
93}
94
95
96/**
97 * Determines endianness of the system. Just for completeness.
98 *
99 * @return Will return false if system is little endian, true otherwise.
100 */
101static bool IsBigEndian()
102{
103 const int i = 1;
104 char *p = (char *)&i;
105 if (p[0] == 1)
106 return false;
107 return true;
108}
109
110
111/**
112 * Reads from a file making sure an interruption doesn't cause a failure.
113 *
114 * @param hFile Handle to the file to read.
115 * @param pv Where to store the read data.
116 * @param cbToRead Size of data to read.
117 *
118 * @return VBox status code.
119 */
120static int ReadFileNoIntr(RTFILE hFile, void *pv, size_t cbToRead)
121{
122 int rc = VERR_READ_ERROR;
123 while (1)
124 {
125 rc = RTFileRead(hFile, pv, cbToRead, NULL /* Read all */);
126 if (rc == VERR_INTERRUPTED)
127 continue;
128 break;
129 }
130 return rc;
131}
132
133
134/**
135 * Writes to a file making sure an interruption doesn't cause a failure.
136 *
137 * @param hFile Handle to the file to write.
138 * @param pv Pointer to what to write.
139 * @param cbToRead Size of data to write.
140 *
141 * @return VBox status code.
142 */
143static int WriteFileNoIntr(RTFILE hFile, const void *pcv, size_t cbToRead)
144{
145 int rc = VERR_READ_ERROR;
146 while (1)
147 {
148 rc = RTFileWrite(hFile, pcv, cbToRead, NULL /* Write all */);
149 if (rc == VERR_INTERRUPTED)
150 continue;
151 break;
152 }
153 return rc;
154}
155
156
157/**
158 * Read from a given offet in the process' address space.
159 *
160 * @param pVBoxProc Pointer to the VBox process.
161 * @param pv Where to read the data into.
162 * @param cb Size of the read buffer.
163 * @param off Offset to read from.
164 *
165 * @return VINF_SUCCESS, if all the given bytes was read in, otherwise VERR_READ_ERROR.
166 */
167static ssize_t ProcReadAddrSpace(PVBOXPROCESS pVBoxProc, RTFOFF off, void *pvBuf, size_t cbToRead)
168{
169 while (1)
170 {
171 int rc = RTFileReadAt(pVBoxProc->hAs, off, pvBuf, cbToRead, NULL);
172 if (rc == VERR_INTERRUPTED)
173 continue;
174 return rc;
175 }
176}
177
178
179/**
180 * Determines if the current process' architecture is suitable for dumping core.
181 *
182 * @param pVBoxProc Pointer to the VBox process.
183 *
184 * @return true if the architecture matches the current one.
185 */
186static inline bool IsProcessArchNative(PVBOXPROCESS pVBoxProc)
187{
188 return pVBoxProc->ProcInfo.pr_dmodel == PR_MODEL_NATIVE;
189}
190
191
192/**
193 * Helper function to get the size of a file given it's path.
194 *
195 * @param pszPath Pointer to the full path of the file.
196 *
197 * @return The size of the file in bytes.
198 */
199static size_t GetFileSize(const char *pszPath)
200{
201 uint64_t cb = 0;
202 RTFILE hFile;
203 int rc = RTFileOpen(&hFile, pszPath, RTFILE_O_OPEN | RTFILE_O_READ);
204 if (RT_SUCCESS(rc))
205 {
206 RTFileGetSize(hFile, &cb);
207 RTFileClose(hFile);
208 }
209 else
210 CORELOGRELSYS((CORELOG_NAME "GetFileSize failed to open %s rc=%Rrc\n", pszPath, rc));
211 return cb < ~(size_t)0 ? (size_t)cb : ~(size_t)0;
212}
213
214
215/**
216 * Pre-compute and pre-allocate sufficient memory for dumping core.
217 * This is meant to be called once, as a single-large anonymously
218 * mapped memory area which will be used during the core dumping routines.
219 *
220 * @param pVBoxCore Pointer to the core object.
221 *
222 * @return VBox status code.
223 */
224static int AllocMemoryArea(PVBOXCORE pVBoxCore)
225{
226 AssertReturn(pVBoxCore->pvCore == NULL, VERR_ALREADY_EXISTS);
227 AssertReturn(pVBoxCore->VBoxProc.Process != NIL_RTPROCESS, VERR_PROCESS_NOT_FOUND);
228
229 struct VBOXSOLPREALLOCTABLE
230 {
231 const char *pszFilePath; /* Proc based path */
232 size_t cbHeader; /* Size of header */
233 size_t cbEntry; /* Size of each entry in file */
234 size_t cbAccounting; /* Size of each accounting entry per entry */
235 } aPreAllocTable[] = {
236 { "/proc/%d/map", 0, sizeof(prmap_t), sizeof(VBOXSOLMAPINFO) },
237 { "/proc/%d/auxv", 0, 0, 0 },
238 { "/proc/%d/lpsinfo", sizeof(prheader_t), sizeof(lwpsinfo_t), sizeof(VBOXSOLTHREADINFO) },
239 { "/proc/%d/lstatus", 0, 0, 0 },
240 { "/proc/%d/ldt", 0, 0, 0 },
241 { "/proc/%d/cred", sizeof(prcred_t), sizeof(gid_t), 1 },
242 { "/proc/%d/priv", sizeof(prpriv_t), sizeof(priv_chunk_t), 1 },
243 };
244
245 size_t cb = 0;
246 for (int i = 0; i < (int)RT_ELEMENTS(aPreAllocTable); i++)
247 {
248 char szPath[PATH_MAX];
249 RTStrPrintf(szPath, sizeof(szPath), aPreAllocTable[i].pszFilePath, (int)pVBoxCore->VBoxProc.Process);
250 size_t cbFile = GetFileSize(szPath);
251 cb += cbFile;
252 if ( cbFile > 0
253 && aPreAllocTable[i].cbEntry > 0
254 && aPreAllocTable[i].cbAccounting > 0)
255 {
256 cb += ((cbFile - aPreAllocTable[i].cbHeader) / aPreAllocTable[i].cbEntry) * aPreAllocTable[i].cbAccounting;
257 cb += aPreAllocTable[i].cbHeader;
258 }
259 }
260
261 /*
262 * Make room for our own mapping accountant entry which will also be included in the core.
263 */
264 cb += sizeof(VBOXSOLMAPINFO);
265
266 /*
267 * Allocate the required space, plus some extra room.
268 */
269 cb += _128K;
270 void *pv = mmap(NULL, cb, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1 /* fd */, 0 /* offset */);
271 if (pv)
272 {
273 CORELOG((CORELOG_NAME "AllocMemoryArea: memory area of %u bytes allocated.\n", cb));
274 pVBoxCore->pvCore = pv;
275 pVBoxCore->pvFree = pv;
276 pVBoxCore->cbCore = cb;
277 return VINF_SUCCESS;
278 }
279 else
280 {
281 CORELOGRELSYS((CORELOG_NAME "AllocMemoryArea: failed cb=%u\n", cb));
282 return VERR_NO_MEMORY;
283 }
284}
285
286
287/**
288 * Free memory area used by the core object.
289 *
290 * @param pVBoxCore Pointer to the core object.
291 */
292static void FreeMemoryArea(PVBOXCORE pVBoxCore)
293{
294 AssertReturnVoid(pVBoxCore);
295 AssertReturnVoid(pVBoxCore->pvCore);
296 AssertReturnVoid(pVBoxCore->cbCore > 0);
297
298 munmap(pVBoxCore->pvCore, pVBoxCore->cbCore);
299 CORELOG((CORELOG_NAME "FreeMemoryArea: memory area of %u bytes freed.\n", pVBoxCore->cbCore));
300
301 pVBoxCore->pvCore = NULL;
302 pVBoxCore->pvFree= NULL;
303 pVBoxCore->cbCore = 0;
304}
305
306
307/**
308 * Get a chunk from the area of allocated memory.
309 *
310 * @param pVBoxCore Pointer to the core object.
311 * @param cb Size of requested chunk.
312 *
313 * @return Pointer to allocated memory, or NULL on failure.
314 */
315static void *GetMemoryChunk(PVBOXCORE pVBoxCore, size_t cb)
316{
317 AssertReturn(pVBoxCore, NULL);
318 AssertReturn(pVBoxCore->pvCore, NULL);
319 AssertReturn(pVBoxCore->pvFree, NULL);
320
321 size_t cbAllocated = (char *)pVBoxCore->pvFree - (char *)pVBoxCore->pvCore;
322 if (cbAllocated < pVBoxCore->cbCore)
323 {
324 char *pb = (char *)pVBoxCore->pvFree;
325 pVBoxCore->pvFree = pb + cb;
326 return pb;
327 }
328
329 return NULL;
330}
331
332
333/**
334 * Reads the proc file's content into a newly allocated buffer.
335 *
336 * @param pVBoxCore Pointer to the core object.
337 * @param pszFileFmt Only the name of the file to read from (/proc/<pid> will be prepended)
338 * @param ppv Where to store the allocated buffer.
339 * @param pcb Where to store size of the buffer.
340 *
341 * @return VBox status code.
342 */
343static int ProcReadFileInto(PVBOXCORE pVBoxCore, const char *pszProcFileName, void **ppv, size_t *pcb)
344{
345 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
346
347 char szPath[PATH_MAX];
348 RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/%s", (int)pVBoxCore->VBoxProc.Process, pszProcFileName);
349 RTFILE hFile;
350 int rc = RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
351 if (RT_SUCCESS(rc))
352 {
353 RTFileGetSize(hFile, (uint64_t *)pcb); /** @todo size_t != uint64_t! */
354 if (*pcb > 0)
355 {
356 *ppv = GetMemoryChunk(pVBoxCore, *pcb);
357 if (*ppv)
358 rc = ReadFileNoIntr(hFile, *ppv, *pcb);
359 else
360 rc = VERR_NO_MEMORY;
361 }
362 else
363 {
364 *pcb = 0;
365 *ppv = NULL;
366 }
367 RTFileClose(hFile);
368 }
369 else
370 CORELOGRELSYS((CORELOG_NAME "ProcReadFileInto: failed to open %s. rc=%Rrc\n", szPath, rc));
371 return rc;
372}
373
374
375/**
376 * Read process information (format psinfo_t) from /proc.
377 *
378 * @param pVBoxCore Pointer to the core object.
379 *
380 * @return VBox status code.
381 */
382static int ProcReadInfo(PVBOXCORE pVBoxCore)
383{
384 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
385
386 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
387 char szPath[PATH_MAX];
388 RTFILE hFile;
389
390 RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/psinfo", (int)pVBoxProc->Process);
391 int rc = RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
392 if (RT_SUCCESS(rc))
393 {
394 size_t cbProcInfo = sizeof(psinfo_t);
395 rc = ReadFileNoIntr(hFile, &pVBoxProc->ProcInfo, cbProcInfo);
396 }
397
398 RTFileClose(hFile);
399 return rc;
400}
401
402
403/**
404 * Read process status (format pstatus_t) from /proc.
405 *
406 * @param pVBoxCore Pointer to the core object.
407 *
408 * @return VBox status code.
409 */
410static int ProcReadStatus(PVBOXCORE pVBoxCore)
411{
412 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
413
414 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
415
416 char szPath[PATH_MAX];
417 RTFILE hFile;
418
419 RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/status", (int)pVBoxProc->Process);
420 int rc = RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
421 if (RT_SUCCESS(rc))
422 {
423 size_t cbRead;
424 size_t cbProcStatus = sizeof(pstatus_t);
425 AssertCompile(sizeof(pstatus_t) == sizeof(pVBoxProc->ProcStatus));
426 rc = ReadFileNoIntr(hFile, &pVBoxProc->ProcStatus, cbProcStatus);
427 }
428 RTFileClose(hFile);
429 return rc;
430}
431
432
433/**
434 * Read process credential information (format prcred_t + array of guid_t)
435 *
436 * @param pVBoxCore Pointer to the core object.
437 *
438 * @remarks Should not be called before successful call to @see AllocMemoryArea()
439 * @return VBox status code.
440 */
441static int ProcReadCred(PVBOXCORE pVBoxCore)
442{
443 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
444
445 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
446 return ProcReadFileInto(pVBoxCore, "cred", &pVBoxProc->pvCred, &pVBoxProc->cbCred);
447}
448
449
450/**
451 * Read process privilege information (format prpriv_t + array of priv_chunk_t)
452 *
453 * @param pVBoxCore Pointer to the core object.
454 *
455 * @remarks Should not be called before successful call to @see AllocMemoryArea()
456 * @return VBox status code.
457 */
458static int ProcReadPriv(PVBOXCORE pVBoxCore)
459{
460 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
461
462 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
463 int rc = ProcReadFileInto(pVBoxCore, "priv", (void **)&pVBoxProc->pPriv, &pVBoxProc->cbPriv);
464 if (RT_FAILURE(rc))
465 return rc;
466 pVBoxProc->pcPrivImpl = getprivimplinfo();
467 if (!pVBoxProc->pcPrivImpl)
468 {
469 CORELOGRELSYS((CORELOG_NAME "ProcReadPriv: getprivimplinfo returned NULL.\n"));
470 return VERR_INVALID_STATE;
471 }
472 return rc;
473}
474
475
476/**
477 * Read process LDT information (format array of struct ssd) from /proc.
478 *
479 * @param pVBoxProc Pointer to the core object.
480 *
481 * @remarks Should not be called before successful call to @see AllocMemoryArea()
482 * @return VBox status code.
483 */
484static int ProcReadLdt(PVBOXCORE pVBoxCore)
485{
486 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
487
488 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
489 return ProcReadFileInto(pVBoxCore, "ldt", &pVBoxProc->pvLdt, &pVBoxProc->cbLdt);
490}
491
492
493/**
494 * Read process auxiliary vectors (format auxv_t) for the process.
495 *
496 * @param pVBoxCore Pointer to the core object.
497 *
498 * @remarks Should not be called before successful call to @see AllocMemoryArea()
499 * @return VBox status code.
500 */
501static int ProcReadAuxVecs(PVBOXCORE pVBoxCore)
502{
503 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
504
505 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
506 char szPath[PATH_MAX];
507 RTFILE hFile = NIL_RTFILE;
508 RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/auxv", (int)pVBoxProc->Process);
509 int rc = RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
510 if (RT_FAILURE(rc))
511 {
512 CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: RTFileOpen %s failed rc=%Rrc\n", szPath, rc));
513 return rc;
514 }
515
516 size_t cbAuxFile = 0;
517 RTFileGetSize(hFile, (uint64_t *)&cbAuxFile); /** @todo size_t != uint64_t! */
518 if (cbAuxFile >= sizeof(auxv_t))
519 {
520 pVBoxProc->pAuxVecs = (auxv_t*)GetMemoryChunk(pVBoxCore, cbAuxFile + sizeof(auxv_t));
521 if (pVBoxProc->pAuxVecs)
522 {
523 rc = ReadFileNoIntr(hFile, pVBoxProc->pAuxVecs, cbAuxFile);
524 if (RT_SUCCESS(rc))
525 {
526 /* Terminate list of vectors */
527 pVBoxProc->cAuxVecs = cbAuxFile / sizeof(auxv_t);
528 CORELOG((CORELOG_NAME "ProcReadAuxVecs: cbAuxFile=%u auxv_t size %d cAuxVecs=%u\n", cbAuxFile, sizeof(auxv_t), pVBoxProc->cAuxVecs));
529 if (pVBoxProc->cAuxVecs > 0)
530 {
531 pVBoxProc->pAuxVecs[pVBoxProc->cAuxVecs].a_type = AT_NULL;
532 pVBoxProc->pAuxVecs[pVBoxProc->cAuxVecs].a_un.a_val = 0L;
533 RTFileClose(hFile);
534 return VINF_SUCCESS;
535 }
536 else
537 {
538 CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: Invalid vector count %u\n", pVBoxProc->cAuxVecs));
539 rc = VERR_READ_ERROR;
540 }
541 }
542 else
543 CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: ReadFileNoIntr failed. rc=%Rrc cbAuxFile=%u\n", rc, cbAuxFile));
544
545 pVBoxProc->pAuxVecs = NULL;
546 pVBoxProc->cAuxVecs = 0;
547 }
548 else
549 {
550 CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: no memory for %u bytes\n", cbAuxFile + sizeof(auxv_t)));
551 rc = VERR_NO_MEMORY;
552 }
553 }
554 else
555 CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: aux file too small %u, expecting %u or more\n", cbAuxFile, sizeof(auxv_t)));
556
557 RTFileClose(hFile);
558 return rc;
559}
560
561
562/*
563 * Find an element in the process' auxiliary vector.
564 */
565static long GetAuxVal(PVBOXPROCESS pVBoxProc, int Type)
566{
567 AssertReturn(pVBoxProc, -1);
568 if (pVBoxProc->pAuxVecs)
569 {
570 auxv_t *pAuxVec = pVBoxProc->pAuxVecs;
571 for (; pAuxVec->a_type != AT_NULL; pAuxVec++)
572 {
573 if (pAuxVec->a_type == Type)
574 return pAuxVec->a_un.a_val;
575 }
576 }
577 return -1;
578}
579
580
581/**
582 * Read the process mappings (format prmap_t array).
583 *
584 * @param pVBoxCore Pointer to the core object.
585 *
586 * @remarks Should not be called before successful call to @see AllocMemoryArea()
587 * @return VBox status code.
588 */
589static int ProcReadMappings(PVBOXCORE pVBoxCore)
590{
591 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
592
593 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
594 char szPath[PATH_MAX];
595 RTFILE hFile = NIL_RTFILE;
596 RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/map", (int)pVBoxProc->Process);
597 int rc = RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
598 if (RT_FAILURE(rc))
599 return rc;
600
601 RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/as", (int)pVBoxProc->Process);
602 rc = RTFileOpen(&pVBoxProc->hAs, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
603 if (RT_SUCCESS(rc))
604 {
605 /*
606 * Allocate and read all the prmap_t objects from proc.
607 */
608 size_t cbMapFile = 0;
609 RTFileGetSize(hFile, (uint64_t *)&cbMapFile); /** @todo size_t != uint64_t! */
610 if (cbMapFile >= sizeof(prmap_t))
611 {
612 prmap_t *pMap = (prmap_t*)GetMemoryChunk(pVBoxCore, cbMapFile);
613 if (pMap)
614 {
615 rc = ReadFileNoIntr(hFile, pMap, cbMapFile);
616 if (RT_SUCCESS(rc))
617 {
618 pVBoxProc->cMappings = cbMapFile / sizeof(prmap_t);
619 if (pVBoxProc->cMappings > 0)
620 {
621 /*
622 * Allocate for each prmap_t object, a corresponding VBOXSOLMAPINFO object.
623 */
624 pVBoxProc->pMapInfoHead = (PVBOXSOLMAPINFO)GetMemoryChunk(pVBoxCore, pVBoxProc->cMappings * sizeof(VBOXSOLMAPINFO));
625 if (pVBoxProc->pMapInfoHead)
626 {
627 /*
628 * Associate the prmap_t with the mapping info object.
629 */
630 Assert(pVBoxProc->pMapInfoHead == NULL);
631 PVBOXSOLMAPINFO pCur = pVBoxProc->pMapInfoHead;
632 PVBOXSOLMAPINFO pPrev = NULL;
633 for (uint64_t i = 0; i < pVBoxProc->cMappings; i++, pMap++, pCur++)
634 {
635 memcpy(&pCur->pMap, pMap, sizeof(pCur->pMap));
636 if (pPrev)
637 pPrev->pNext = pCur;
638
639 pCur->fError = 0;
640
641 /*
642 * Make sure we can read the mapping, otherwise mark them to be skipped.
643 */
644 char achBuf[PAGE_SIZE];
645 uint64_t k = 0;
646 while (k < pCur->pMap.pr_size)
647 {
648 size_t cb = RT_MIN(sizeof(achBuf), pCur->pMap.pr_size - k);
649 int rc2 = ProcReadAddrSpace(pVBoxProc, pCur->pMap.pr_vaddr + k, &achBuf, cb);
650 if (RT_FAILURE(rc2))
651 {
652 CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: skipping mapping. vaddr=%#x rc=%Rrc\n", pCur->pMap.pr_vaddr, rc2));
653
654 /*
655 * Instead of storing the actual mapping data which we failed to read, the core
656 * will contain an errno in place. So we adjust the prmap_t's size field too
657 * so the program header offsets match.
658 */
659 pCur->pMap.pr_size = RT_ALIGN_Z(sizeof(int), 8);
660 pCur->fError = errno;
661 if (pCur->fError == 0) /* huh!? somehow errno got reset? fake one! EFAULT is nice. */
662 pCur->fError = EFAULT;
663 break;
664 }
665 k += cb;
666 }
667
668 pPrev = pCur;
669 }
670 if (pPrev)
671 pPrev->pNext = NULL;
672
673 RTFileClose(hFile);
674 RTFileClose(pVBoxProc->hAs);
675 pVBoxProc->hAs = NIL_RTFILE;
676 CORELOG((CORELOG_NAME "ProcReadMappings: successfully read in %u mappings\n", pVBoxProc->cMappings));
677 return VINF_SUCCESS;
678 }
679 else
680 {
681 CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: GetMemoryChunk failed %u\n", pVBoxProc->cMappings * sizeof(VBOXSOLMAPINFO)));
682 rc = VERR_NO_MEMORY;
683 }
684 }
685 else
686 {
687 CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: Invalid mapping count %u\n", pVBoxProc->cMappings));
688 rc = VERR_READ_ERROR;
689 }
690 }
691 else
692 CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: FileReadNoIntr failed. rc=%Rrc cbMapFile=%u\n", rc, cbMapFile));
693 }
694 else
695 {
696 CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: GetMemoryChunk failed. cbMapFile=%u\n", cbMapFile));
697 rc = VERR_NO_MEMORY;
698 }
699 }
700
701 RTFileClose(pVBoxProc->hAs);
702 pVBoxProc->hAs = NIL_RTFILE;
703 }
704 else
705 CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: failed to open %s. rc=%Rrc\n", szPath, rc));
706
707 RTFileClose(hFile);
708 return rc;
709}
710
711
712/**
713 * Reads the thread information for all threads in the process.
714 *
715 * @param pVBoxCore Pointer to the core object.
716 *
717 * @remarks Should not be called before successful call to @see AllocMemoryArea()
718 * @return VBox status code.
719 */
720static int ProcReadThreads(PVBOXCORE pVBoxCore)
721{
722 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
723
724 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
725 AssertReturn(pVBoxProc->pCurThreadCtx, VERR_NO_DATA);
726
727 /*
728 * Read the information for threads.
729 * Format: prheader_t + array of lwpsinfo_t's.
730 */
731 size_t cbInfoHdrAndData;
732 void *pvInfoHdr = NULL;
733 int rc = ProcReadFileInto(pVBoxCore, "lpsinfo", &pvInfoHdr, &cbInfoHdrAndData);
734 if (RT_SUCCESS(rc))
735 {
736 /*
737 * Read the status of threads.
738 * Format: prheader_t + array of lwpstatus_t's.
739 */
740 void *pvStatusHdr = NULL;
741 size_t cbStatusHdrAndData;
742 rc = ProcReadFileInto(pVBoxCore, "lstatus", &pvStatusHdr, &cbStatusHdrAndData);
743 if (RT_SUCCESS(rc))
744 {
745 prheader_t *pInfoHdr = (prheader_t *)pvInfoHdr;
746 prheader_t *pStatusHdr = (prheader_t *)pvStatusHdr;
747 lwpstatus_t *pStatus = (lwpstatus_t *)((uintptr_t)pStatusHdr + sizeof(prheader_t));
748 lwpsinfo_t *pInfo = (lwpsinfo_t *)((uintptr_t)pInfoHdr + sizeof(prheader_t));
749 uint64_t cStatus = pStatusHdr->pr_nent;
750 uint64_t cInfo = pInfoHdr->pr_nent;
751
752 CORELOG((CORELOG_NAME "ProcReadThreads: read info(%u) status(%u), threads:cInfo=%u cStatus=%u\n", cbInfoHdrAndData,
753 cbStatusHdrAndData, cInfo, cStatus));
754
755 /*
756 * Minor sanity size check (remember sizeof lwpstatus_t & lwpsinfo_t is <= size in file per entry).
757 */
758 if ( (cbStatusHdrAndData - sizeof(prheader_t)) % pStatusHdr->pr_entsize == 0
759 && (cbInfoHdrAndData - sizeof(prheader_t)) % pInfoHdr->pr_entsize == 0)
760 {
761 /*
762 * Make sure we have a matching lstatus entry for an lpsinfo entry unless
763 * it is a zombie thread, in which case we will not have a matching lstatus entry.
764 */
765 for (; cInfo != 0; cInfo--)
766 {
767 if (pInfo->pr_sname != 'Z') /* zombie */
768 {
769 if ( cStatus == 0
770 || pStatus->pr_lwpid != pInfo->pr_lwpid)
771 {
772 CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: cStatus = %u pStatuslwpid=%d infolwpid=%d\n", cStatus,
773 pStatus->pr_lwpid, pInfo->pr_lwpid));
774 rc = VERR_INVALID_STATE;
775 break;
776 }
777 pStatus = (lwpstatus_t *)((uintptr_t)pStatus + pStatusHdr->pr_entsize);
778 cStatus--;
779 }
780 pInfo = (lwpsinfo_t *)((uintptr_t)pInfo + pInfoHdr->pr_entsize);
781 }
782
783 if (RT_SUCCESS(rc))
784 {
785 /*
786 * Threre can still be more lwpsinfo_t's than lwpstatus_t's, build the
787 * lists accordingly.
788 */
789 pStatus = (lwpstatus_t *)((uintptr_t)pStatusHdr + sizeof(prheader_t));
790 pInfo = (lwpsinfo_t *)((uintptr_t)pInfoHdr + sizeof(prheader_t));
791 cInfo = pInfoHdr->pr_nent;
792 cStatus = pInfoHdr->pr_nent;
793
794 size_t cbThreadInfo = RT_MAX(cStatus, cInfo) * sizeof(VBOXSOLTHREADINFO);
795 pVBoxProc->pThreadInfoHead = (PVBOXSOLTHREADINFO)GetMemoryChunk(pVBoxCore, cbThreadInfo);
796 if (pVBoxProc->pThreadInfoHead)
797 {
798 PVBOXSOLTHREADINFO pCur = pVBoxProc->pThreadInfoHead;
799 PVBOXSOLTHREADINFO pPrev = NULL;
800 for (uint64_t i = 0; i < cInfo; i++, pCur++)
801 {
802 pCur->Info = *pInfo;
803 if ( pInfo->pr_sname != 'Z'
804 && pInfo->pr_lwpid == pStatus->pr_lwpid)
805 {
806 /*
807 * Adjust the context of the dumping thread to reflect the context
808 * when the core dump got initiated before whatever signal caused it.
809 */
810 if ( pStatus /* noid droid */
811 && pStatus->pr_lwpid == (id_t)pVBoxProc->hCurThread)
812 {
813 AssertCompile(sizeof(pStatus->pr_reg) == sizeof(pVBoxProc->pCurThreadCtx->uc_mcontext.gregs));
814 AssertCompile(sizeof(pStatus->pr_fpreg) == sizeof(pVBoxProc->pCurThreadCtx->uc_mcontext.fpregs));
815 memcpy(&pStatus->pr_reg, &pVBoxProc->pCurThreadCtx->uc_mcontext.gregs, sizeof(pStatus->pr_reg));
816 memcpy(&pStatus->pr_fpreg, &pVBoxProc->pCurThreadCtx->uc_mcontext.fpregs, sizeof(pStatus->pr_fpreg));
817
818 AssertCompile(sizeof(pStatus->pr_lwphold) == sizeof(pVBoxProc->pCurThreadCtx->uc_sigmask));
819 memcpy(&pStatus->pr_lwphold, &pVBoxProc->pCurThreadCtx->uc_sigmask, sizeof(pStatus->pr_lwphold));
820 pStatus->pr_ustack = (uintptr_t)&pVBoxProc->pCurThreadCtx->uc_stack;
821
822 CORELOG((CORELOG_NAME "ProcReadThreads: patched dumper thread context with pre-dump time context.\n"));
823 }
824
825 pCur->pStatus = pStatus;
826 pStatus = (lwpstatus_t *)((uintptr_t)pStatus + pStatusHdr->pr_entsize);
827 }
828 else
829 {
830 CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: missing status for lwp %d\n", pInfo->pr_lwpid));
831 pCur->pStatus = NULL;
832 }
833
834 if (pPrev)
835 pPrev->pNext = pCur;
836 pPrev = pCur;
837 pInfo = (lwpsinfo_t *)((uintptr_t)pInfo + pInfoHdr->pr_entsize);
838 }
839 if (pPrev)
840 pPrev->pNext = NULL;
841
842 CORELOG((CORELOG_NAME "ProcReadThreads: successfully read %u threads.\n", cInfo));
843 pVBoxProc->cThreads = cInfo;
844 return VINF_SUCCESS;
845 }
846 else
847 {
848 CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: GetMemoryChunk failed for %u bytes\n", cbThreadInfo));
849 rc = VERR_NO_MEMORY;
850 }
851 }
852 else
853 CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: Invalid state information for threads.\n", rc));
854 }
855 else
856 {
857 CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: huh!? cbStatusHdrAndData=%u prheader_t=%u entsize=%u\n", cbStatusHdrAndData,
858 sizeof(prheader_t), pStatusHdr->pr_entsize));
859 CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: huh!? cbInfoHdrAndData=%u entsize=%u\n", cbInfoHdrAndData, pStatusHdr->pr_entsize));
860 rc = VERR_INVALID_STATE;
861 }
862 }
863 else
864 CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: ReadFileNoIntr failed for \"lpsinfo\" rc=%Rrc\n", rc));
865 }
866 else
867 CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: ReadFileNoIntr failed for \"lstatus\" rc=%Rrc\n", rc));
868 return rc;
869}
870
871
872/**
873 * Reads miscellaneous information that is collected as part of a core file.
874 * This may include platform name, zone name and other OS-specific information.
875 *
876 * @param pVBoxCore Pointer to the core object.
877 *
878 * @return VBox status code.
879 */
880static int ProcReadMiscInfo(PVBOXCORE pVBoxCore)
881{
882 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
883
884 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
885
886#ifdef RT_OS_SOLARIS
887 /*
888 * Read the platform name, uname string and zone name.
889 */
890 int rc = sysinfo(SI_PLATFORM, pVBoxProc->szPlatform, sizeof(pVBoxProc->szPlatform));
891 if (rc == -1)
892 {
893 CORELOGRELSYS((CORELOG_NAME "ProcReadMiscInfo: sysinfo failed. rc=%d errno=%d\n", rc, errno));
894 return VERR_GENERAL_FAILURE;
895 }
896 pVBoxProc->szPlatform[sizeof(pVBoxProc->szPlatform) - 1] = '\0';
897
898 rc = uname(&pVBoxProc->UtsName);
899 if (rc == -1)
900 {
901 CORELOGRELSYS((CORELOG_NAME "ProcReadMiscInfo: uname failed. rc=%d errno=%d\n", rc, errno));
902 return VERR_GENERAL_FAILURE;
903 }
904
905 rc = getzonenamebyid(pVBoxProc->ProcInfo.pr_zoneid, pVBoxProc->szZoneName, sizeof(pVBoxProc->szZoneName));
906 if (rc < 0)
907 {
908 CORELOGRELSYS((CORELOG_NAME "ProcReadMiscInfo: getzonenamebyid failed. rc=%d errno=%d zoneid=%d\n", rc, errno, pVBoxProc->ProcInfo.pr_zoneid));
909 return VERR_GENERAL_FAILURE;
910 }
911 pVBoxProc->szZoneName[sizeof(pVBoxProc->szZoneName) - 1] = '\0';
912 rc = VINF_SUCCESS;
913
914#else
915# error Port Me!
916#endif
917 return rc;
918}
919
920
921/**
922 * On Solaris use the old-style procfs interfaces but the core file still should have this
923 * info. for backward and GDB compatibility, hence the need for this ugly function.
924 *
925 * @param pVBoxCore Pointer to the core object.
926 * @param pInfo Pointer to the old prpsinfo_t structure to update.
927 */
928static void GetOldProcessInfo(PVBOXCORE pVBoxCore, prpsinfo_t *pInfo)
929{
930 AssertReturnVoid(pVBoxCore);
931 AssertReturnVoid(pInfo);
932
933 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
934 psinfo_t *pSrc = &pVBoxProc->ProcInfo;
935 memset(pInfo, 0, sizeof(prpsinfo_t));
936 pInfo->pr_state = pSrc->pr_lwp.pr_state;
937 pInfo->pr_zomb = (pInfo->pr_state == SZOMB);
938 RTStrCopy(pInfo->pr_clname, sizeof(pInfo->pr_clname), pSrc->pr_lwp.pr_clname);
939 RTStrCopy(pInfo->pr_fname, sizeof(pInfo->pr_fname), pSrc->pr_fname);
940 memcpy(&pInfo->pr_psargs, &pSrc->pr_psargs, sizeof(pInfo->pr_psargs));
941 pInfo->pr_nice = pSrc->pr_lwp.pr_nice;
942 pInfo->pr_flag = pSrc->pr_lwp.pr_flag;
943 pInfo->pr_uid = pSrc->pr_uid;
944 pInfo->pr_gid = pSrc->pr_gid;
945 pInfo->pr_pid = pSrc->pr_pid;
946 pInfo->pr_ppid = pSrc->pr_ppid;
947 pInfo->pr_pgrp = pSrc->pr_pgid;
948 pInfo->pr_sid = pSrc->pr_sid;
949 pInfo->pr_addr = (caddr_t)pSrc->pr_addr;
950 pInfo->pr_size = pSrc->pr_size;
951 pInfo->pr_rssize = pSrc->pr_rssize;
952 pInfo->pr_wchan = (caddr_t)pSrc->pr_lwp.pr_wchan;
953 pInfo->pr_start = pSrc->pr_start;
954 pInfo->pr_time = pSrc->pr_time;
955 pInfo->pr_pri = pSrc->pr_lwp.pr_pri;
956 pInfo->pr_oldpri = pSrc->pr_lwp.pr_oldpri;
957 pInfo->pr_cpu = pSrc->pr_lwp.pr_cpu;
958 pInfo->pr_ottydev = cmpdev(pSrc->pr_ttydev);
959 pInfo->pr_lttydev = pSrc->pr_ttydev;
960 pInfo->pr_syscall = pSrc->pr_lwp.pr_syscall;
961 pInfo->pr_ctime = pSrc->pr_ctime;
962 pInfo->pr_bysize = pSrc->pr_size * PAGESIZE;
963 pInfo->pr_byrssize = pSrc->pr_rssize * PAGESIZE;
964 pInfo->pr_argc = pSrc->pr_argc;
965 pInfo->pr_argv = (char **)pSrc->pr_argv;
966 pInfo->pr_envp = (char **)pSrc->pr_envp;
967 pInfo->pr_wstat = pSrc->pr_wstat;
968 pInfo->pr_pctcpu = pSrc->pr_pctcpu;
969 pInfo->pr_pctmem = pSrc->pr_pctmem;
970 pInfo->pr_euid = pSrc->pr_euid;
971 pInfo->pr_egid = pSrc->pr_egid;
972 pInfo->pr_aslwpid = 0;
973 pInfo->pr_dmodel = pSrc->pr_dmodel;
974}
975
976
977/**
978 * On Solaris use the old-style procfs interfaces but the core file still should have this
979 * info. for backward and GDB compatibility, hence the need for this ugly function.
980 *
981 * @param pVBoxCore Pointer to the core object.
982 * @param pInfo Pointer to the thread info.
983 * @param pStatus Pointer to the thread status.
984 * @param pDst Pointer to the old-style status structure to update.
985 *
986 */
987static void GetOldProcessStatus(PVBOXCORE pVBoxCore, lwpsinfo_t *pInfo, lwpstatus_t *pStatus, prstatus_t *pDst)
988{
989 AssertReturnVoid(pVBoxCore);
990 AssertReturnVoid(pInfo);
991 AssertReturnVoid(pStatus);
992 AssertReturnVoid(pDst);
993
994 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
995 memset(pDst, 0, sizeof(prstatus_t));
996 if (pStatus->pr_flags & PR_STOPPED)
997 pDst->pr_flags = 0x0001;
998 if (pStatus->pr_flags & PR_ISTOP)
999 pDst->pr_flags = 0x0002;
1000 if (pStatus->pr_flags & PR_DSTOP)
1001 pDst->pr_flags = 0x0004;
1002 if (pStatus->pr_flags & PR_ASLEEP)
1003 pDst->pr_flags = 0x0008;
1004 if (pStatus->pr_flags & PR_FORK)
1005 pDst->pr_flags = 0x0010;
1006 if (pStatus->pr_flags & PR_RLC)
1007 pDst->pr_flags = 0x0020;
1008 /* PR_PTRACE is never set */
1009 if (pStatus->pr_flags & PR_PCINVAL)
1010 pDst->pr_flags = 0x0080;
1011 if (pStatus->pr_flags & PR_ISSYS)
1012 pDst->pr_flags = 0x0100;
1013 if (pStatus->pr_flags & PR_STEP)
1014 pDst->pr_flags = 0x0200;
1015 if (pStatus->pr_flags & PR_KLC)
1016 pDst->pr_flags = 0x0400;
1017 if (pStatus->pr_flags & PR_ASYNC)
1018 pDst->pr_flags = 0x0800;
1019 if (pStatus->pr_flags & PR_PTRACE)
1020 pDst->pr_flags = 0x1000;
1021 if (pStatus->pr_flags & PR_MSACCT)
1022 pDst->pr_flags = 0x2000;
1023 if (pStatus->pr_flags & PR_BPTADJ)
1024 pDst->pr_flags = 0x4000;
1025 if (pStatus->pr_flags & PR_ASLWP)
1026 pDst->pr_flags = 0x8000;
1027
1028 pDst->pr_who = pStatus->pr_lwpid;
1029 pDst->pr_why = pStatus->pr_why;
1030 pDst->pr_what = pStatus->pr_what;
1031 pDst->pr_info = pStatus->pr_info;
1032 pDst->pr_cursig = pStatus->pr_cursig;
1033 pDst->pr_sighold = pStatus->pr_lwphold;
1034 pDst->pr_altstack = pStatus->pr_altstack;
1035 pDst->pr_action = pStatus->pr_action;
1036 pDst->pr_syscall = pStatus->pr_syscall;
1037 pDst->pr_nsysarg = pStatus->pr_nsysarg;
1038 pDst->pr_lwppend = pStatus->pr_lwppend;
1039 pDst->pr_oldcontext = (ucontext_t *)pStatus->pr_oldcontext;
1040 memcpy(pDst->pr_reg, pStatus->pr_reg, sizeof(pDst->pr_reg));
1041 memcpy(pDst->pr_sysarg, pStatus->pr_sysarg, sizeof(pDst->pr_sysarg));
1042 RTStrCopy(pDst->pr_clname, sizeof(pDst->pr_clname), pStatus->pr_clname);
1043
1044 pDst->pr_nlwp = pVBoxProc->ProcStatus.pr_nlwp;
1045 pDst->pr_sigpend = pVBoxProc->ProcStatus.pr_sigpend;
1046 pDst->pr_pid = pVBoxProc->ProcStatus.pr_pid;
1047 pDst->pr_ppid = pVBoxProc->ProcStatus.pr_ppid;
1048 pDst->pr_pgrp = pVBoxProc->ProcStatus.pr_pgid;
1049 pDst->pr_sid = pVBoxProc->ProcStatus.pr_sid;
1050 pDst->pr_utime = pVBoxProc->ProcStatus.pr_utime;
1051 pDst->pr_stime = pVBoxProc->ProcStatus.pr_stime;
1052 pDst->pr_cutime = pVBoxProc->ProcStatus.pr_cutime;
1053 pDst->pr_cstime = pVBoxProc->ProcStatus.pr_cstime;
1054 pDst->pr_brkbase = (caddr_t)pVBoxProc->ProcStatus.pr_brkbase;
1055 pDst->pr_brksize = pVBoxProc->ProcStatus.pr_brksize;
1056 pDst->pr_stkbase = (caddr_t)pVBoxProc->ProcStatus.pr_stkbase;
1057 pDst->pr_stksize = pVBoxProc->ProcStatus.pr_stksize;
1058
1059 pDst->pr_processor = (short)pInfo->pr_onpro;
1060 pDst->pr_bind = (short)pInfo->pr_bindpro;
1061 pDst->pr_instr = pStatus->pr_instr;
1062}
1063
1064
1065/**
1066 * Resume all threads of this process.
1067 *
1068 * @param pVBoxProc Pointer to the VBox process.
1069 *
1070 * @return VBox error code.
1071 */
1072static int rtCoreDumperResumeThreads(PVBOXPROCESS pVBoxProc)
1073{
1074 AssertReturn(pVBoxProc, VERR_INVALID_POINTER);
1075
1076 char szCurThread[128];
1077 char szPath[PATH_MAX];
1078 PRTDIR pDir = NULL;
1079
1080 RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/lwp", (int)pVBoxProc->Process);
1081 RTStrPrintf(szCurThread, sizeof(szCurThread), "%d", (int)pVBoxProc->hCurThread);
1082
1083 int32_t cRunningThreads = 0;
1084 int rc = RTDirOpen(&pDir, szPath);
1085 if (RT_SUCCESS(rc))
1086 {
1087 /*
1088 * Loop through all our threads & resume them.
1089 */
1090 RTDIRENTRY DirEntry;
1091 while (RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))
1092 {
1093 if ( !strcmp(DirEntry.szName, ".")
1094 || !strcmp(DirEntry.szName, ".."))
1095 continue;
1096
1097 if ( !strcmp(DirEntry.szName, szCurThread))
1098 continue;
1099
1100 int32_t ThreadId = RTStrToInt32(DirEntry.szName);
1101 _lwp_continue((lwpid_t)ThreadId);
1102 ++cRunningThreads;
1103 }
1104
1105 CORELOG((CORELOG_NAME "ResumeAllThreads: resumed %d threads\n", cRunningThreads));
1106 RTDirClose(pDir);
1107 }
1108 else
1109 {
1110 CORELOGRELSYS((CORELOG_NAME "ResumeAllThreads: Failed to open %s\n", szPath));
1111 rc = VERR_READ_ERROR;
1112 }
1113
1114 return rc;
1115}
1116
1117
1118/**
1119 * Stop all running threads of this process. Before dumping any
1120 * core we need to make sure the process is quiesced.
1121 *
1122 * @param pVBoxProc Pointer to the VBox process.
1123 *
1124 * @return VBox error code.
1125 */
1126static int rtCoreDumperSuspendThreads(PVBOXPROCESS pVBoxProc)
1127{
1128 char szCurThread[128];
1129 char szPath[PATH_MAX];
1130 PRTDIR pDir = NULL;
1131
1132 RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/lwp", (int)pVBoxProc->Process);
1133 RTStrPrintf(szCurThread, sizeof(szCurThread), "%d", (int)pVBoxProc->hCurThread);
1134
1135 int rc = -1;
1136 uint32_t cThreads = 0;
1137 uint16_t cTries = 0;
1138 for (cTries = 0; cTries < 10; cTries++)
1139 {
1140 uint32_t cRunningThreads = 0;
1141 rc = RTDirOpen(&pDir, szPath);
1142 if (RT_SUCCESS(rc))
1143 {
1144 /*
1145 * Loop through all our threads & suspend them, multiple calls to _lwp_suspend() are okay.
1146 */
1147 RTDIRENTRY DirEntry;
1148 while (RT_SUCCESS(RTDirRead(pDir, &DirEntry, NULL)))
1149 {
1150 if ( !strcmp(DirEntry.szName, ".")
1151 || !strcmp(DirEntry.szName, ".."))
1152 continue;
1153
1154 if ( !strcmp(DirEntry.szName, szCurThread))
1155 continue;
1156
1157 int32_t ThreadId = RTStrToInt32(DirEntry.szName);
1158 _lwp_suspend((lwpid_t)ThreadId);
1159 ++cRunningThreads;
1160 }
1161
1162 if (cTries > 5 && cThreads == cRunningThreads)
1163 {
1164 rc = VINF_SUCCESS;
1165 break;
1166 }
1167 cThreads = cRunningThreads;
1168 RTDirClose(pDir);
1169 }
1170 else
1171 {
1172 CORELOGRELSYS((CORELOG_NAME "SuspendThreads: Failed to open %s cTries=%d\n", szPath, cTries));
1173 rc = VERR_READ_ERROR;
1174 break;
1175 }
1176 }
1177
1178 if (RT_SUCCESS(rc))
1179 CORELOG((CORELOG_NAME "SuspendThreads: Stopped %u threads successfully with %u tries\n", cThreads, cTries));
1180
1181 return rc;
1182}
1183
1184
1185/**
1186 * Returns size of an ELF NOTE header given the size of data the NOTE section will contain.
1187 *
1188 * @param cb Size of the data.
1189 *
1190 * @return Size of data actually used for NOTE header and section.
1191 */
1192static inline size_t ElfNoteHeaderSize(size_t cb)
1193{
1194 return sizeof(ELFNOTEHDR) + RT_ALIGN_Z(cb, 4);
1195}
1196
1197
1198/**
1199 * Write an ELF NOTE header into the core file.
1200 *
1201 * @param pVBoxCore Pointer to the core object.
1202 * @param Type Type of this NOTE section.
1203 * @param pcv Opaque pointer to the data, if NULL only computes size.
1204 * @param cb Size of the data.
1205 *
1206 * @return VBox status code.
1207 */
1208static int ElfWriteNoteHeader(PVBOXCORE pVBoxCore, uint_t Type, const void *pcv, size_t cb)
1209{
1210 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
1211 AssertReturn(pcv, VERR_INVALID_POINTER);
1212 AssertReturn(cb > 0, VERR_NO_DATA);
1213 AssertReturn(pVBoxCore->pfnWriter, VERR_WRITE_ERROR);
1214 AssertReturn(pVBoxCore->hCoreFile, VERR_INVALID_HANDLE);
1215
1216 int rc = VERR_GENERAL_FAILURE;
1217#ifdef RT_OS_SOLARIS
1218 ELFNOTEHDR ElfNoteHdr;
1219 RT_ZERO(ElfNoteHdr);
1220 ElfNoteHdr.achName[0] = 'C';
1221 ElfNoteHdr.achName[1] = 'O';
1222 ElfNoteHdr.achName[2] = 'R';
1223 ElfNoteHdr.achName[3] = 'E';
1224 ElfNoteHdr.Hdr.n_namesz = 5;
1225 ElfNoteHdr.Hdr.n_type = Type;
1226 ElfNoteHdr.Hdr.n_descsz = RT_ALIGN_Z(cb, 4);
1227
1228 /*
1229 * Write note header and description.
1230 */
1231 rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &ElfNoteHdr, sizeof(ElfNoteHdr));
1232 if (RT_SUCCESS(rc))
1233 rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, pcv, ElfNoteHdr.Hdr.n_descsz);
1234
1235 if (RT_FAILURE(rc))
1236 CORELOGRELSYS((CORELOG_NAME "ElfWriteNote: pfnWriter failed. Type=%d rc=%Rrc\n", Type, rc));
1237#else
1238#error Port Me!
1239#endif
1240 return rc;
1241}
1242
1243
1244/**
1245 * Computes the size of NOTE section for the given core type.
1246 * Solaris has two types of program header information (new and old).
1247 *
1248 * @param pVBoxCore Pointer to the core object.
1249 * @param enmType Type of core file information required.
1250 *
1251 * @return Size of NOTE section.
1252 */
1253static size_t ElfNoteSectionSize(PVBOXCORE pVBoxCore, VBOXSOLCORETYPE enmType)
1254{
1255 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
1256 size_t cb = 0;
1257 switch (enmType)
1258 {
1259 case enmOldEra:
1260 {
1261 cb += ElfNoteHeaderSize(sizeof(prpsinfo_t));
1262 cb += ElfNoteHeaderSize(pVBoxProc->cAuxVecs * sizeof(auxv_t));
1263 cb += ElfNoteHeaderSize(strlen(pVBoxProc->szPlatform));
1264
1265 PVBOXSOLTHREADINFO pThreadInfo = pVBoxProc->pThreadInfoHead;
1266 while (pThreadInfo)
1267 {
1268 if (pThreadInfo->pStatus)
1269 {
1270 cb += ElfNoteHeaderSize(sizeof(prstatus_t));
1271 cb += ElfNoteHeaderSize(sizeof(prfpregset_t));
1272 }
1273 pThreadInfo = pThreadInfo->pNext;
1274 }
1275
1276 break;
1277 }
1278
1279 case enmNewEra:
1280 {
1281 cb += ElfNoteHeaderSize(sizeof(psinfo_t));
1282 cb += ElfNoteHeaderSize(sizeof(pstatus_t));
1283 cb += ElfNoteHeaderSize(pVBoxProc->cAuxVecs * sizeof(auxv_t));
1284 cb += ElfNoteHeaderSize(strlen(pVBoxProc->szPlatform) + 1);
1285 cb += ElfNoteHeaderSize(sizeof(struct utsname));
1286 cb += ElfNoteHeaderSize(sizeof(core_content_t));
1287 cb += ElfNoteHeaderSize(pVBoxProc->cbCred);
1288
1289 if (pVBoxProc->pPriv)
1290 cb += ElfNoteHeaderSize(PRIV_PRPRIV_SIZE(pVBoxProc->pPriv)); /* Ought to be same as cbPriv!? */
1291
1292 if (pVBoxProc->pcPrivImpl)
1293 cb += ElfNoteHeaderSize(PRIV_IMPL_INFO_SIZE(pVBoxProc->pcPrivImpl));
1294
1295 cb += ElfNoteHeaderSize(strlen(pVBoxProc->szZoneName) + 1);
1296 if (pVBoxProc->cbLdt > 0)
1297 cb += ElfNoteHeaderSize(pVBoxProc->cbLdt);
1298
1299 PVBOXSOLTHREADINFO pThreadInfo = pVBoxProc->pThreadInfoHead;
1300 while (pThreadInfo)
1301 {
1302 cb += ElfNoteHeaderSize(sizeof(lwpsinfo_t));
1303 if (pThreadInfo->pStatus)
1304 cb += ElfNoteHeaderSize(sizeof(lwpstatus_t));
1305
1306 pThreadInfo = pThreadInfo->pNext;
1307 }
1308
1309 break;
1310 }
1311
1312 default:
1313 {
1314 CORELOGRELSYS((CORELOG_NAME "ElfNoteSectionSize: Unknown segment era %d\n", enmType));
1315 break;
1316 }
1317 }
1318
1319 return cb;
1320}
1321
1322
1323/**
1324 * Write the note section for the given era into the core file.
1325 * Solaris has two types of program header information (new and old).
1326 *
1327 * @param pVBoxCore Pointer to the core object.
1328 * @param enmType Type of core file information required.
1329 *
1330 * @return VBox status code.
1331 */
1332static int ElfWriteNoteSection(PVBOXCORE pVBoxCore, VBOXSOLCORETYPE enmType)
1333{
1334 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
1335
1336 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
1337 int rc = VERR_GENERAL_FAILURE;
1338
1339#ifdef RT_OS_SOLARIS
1340 typedef int (*PFNELFWRITENOTEHDR)(PVBOXCORE pVBoxCore, uint_t, const void *pcv, size_t cb);
1341 typedef struct ELFWRITENOTE
1342 {
1343 const char *pszType;
1344 uint_t Type;
1345 const void *pcv;
1346 size_t cb;
1347 } ELFWRITENOTE;
1348
1349 switch (enmType)
1350 {
1351 case enmOldEra:
1352 {
1353 ELFWRITENOTE aElfNotes[] =
1354 {
1355 { "NT_PRPSINFO", NT_PRPSINFO, &pVBoxProc->ProcInfoOld, sizeof(prpsinfo_t) },
1356 { "NT_AUXV", NT_AUXV, pVBoxProc->pAuxVecs, pVBoxProc->cAuxVecs * sizeof(auxv_t) },
1357 { "NT_PLATFORM", NT_PLATFORM, pVBoxProc->szPlatform, strlen(pVBoxProc->szPlatform) + 1 }
1358 };
1359
1360 for (unsigned i = 0; i < RT_ELEMENTS(aElfNotes); i++)
1361 {
1362 rc = ElfWriteNoteHeader(pVBoxCore, aElfNotes[i].Type, aElfNotes[i].pcv, aElfNotes[i].cb);
1363 if (RT_FAILURE(rc))
1364 {
1365 CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader failed for %s. rc=%Rrc\n", aElfNotes[i].pszType, rc));
1366 break;
1367 }
1368 }
1369
1370 /*
1371 * Write old-style thread info., they contain nothing about zombies,
1372 * so we just skip if there is no status information for them.
1373 */
1374 PVBOXSOLTHREADINFO pThreadInfo = pVBoxProc->pThreadInfoHead;
1375 for (; pThreadInfo; pThreadInfo = pThreadInfo->pNext)
1376 {
1377 if (!pThreadInfo->pStatus)
1378 continue;
1379
1380 prstatus_t OldProcessStatus;
1381 GetOldProcessStatus(pVBoxCore, &pThreadInfo->Info, pThreadInfo->pStatus, &OldProcessStatus);
1382 rc = ElfWriteNoteHeader(pVBoxCore, NT_PRSTATUS, &OldProcessStatus, sizeof(prstatus_t));
1383 if (RT_SUCCESS(rc))
1384 {
1385 rc = ElfWriteNoteHeader(pVBoxCore, NT_PRFPREG, &pThreadInfo->pStatus->pr_fpreg, sizeof(prfpregset_t));
1386 if (RT_FAILURE(rc))
1387 {
1388 CORELOGRELSYS((CORELOG_NAME "ElfWriteSegment: ElfWriteNote failed for NT_PRFPREF. rc=%Rrc\n", rc));
1389 break;
1390 }
1391 }
1392 else
1393 {
1394 CORELOGRELSYS((CORELOG_NAME "ElfWriteSegment: ElfWriteNote failed for NT_PRSTATUS. rc=%Rrc\n", rc));
1395 break;
1396 }
1397 }
1398 break;
1399 }
1400
1401 case enmNewEra:
1402 {
1403 ELFWRITENOTE aElfNotes[] =
1404 {
1405 { "NT_PSINFO", NT_PSINFO, &pVBoxProc->ProcInfo, sizeof(psinfo_t) },
1406 { "NT_PSTATUS", NT_PSTATUS, &pVBoxProc->ProcStatus, sizeof(pstatus_t) },
1407 { "NT_AUXV", NT_AUXV, pVBoxProc->pAuxVecs, pVBoxProc->cAuxVecs * sizeof(auxv_t) },
1408 { "NT_PLATFORM", NT_PLATFORM, pVBoxProc->szPlatform, strlen(pVBoxProc->szPlatform) + 1 },
1409 { "NT_UTSNAME", NT_UTSNAME, &pVBoxProc->UtsName, sizeof(struct utsname) },
1410 { "NT_CONTENT", NT_CONTENT, &pVBoxProc->CoreContent, sizeof(core_content_t) },
1411 { "NT_PRCRED", NT_PRCRED, pVBoxProc->pvCred, pVBoxProc->cbCred },
1412 { "NT_PRPRIV", NT_PRPRIV, pVBoxProc->pPriv, PRIV_PRPRIV_SIZE(pVBoxProc->pPriv) },
1413 { "NT_PRPRIVINFO", NT_PRPRIVINFO, pVBoxProc->pcPrivImpl, PRIV_IMPL_INFO_SIZE(pVBoxProc->pcPrivImpl) },
1414 { "NT_ZONENAME", NT_ZONENAME, pVBoxProc->szZoneName, strlen(pVBoxProc->szZoneName) + 1 }
1415 };
1416
1417 for (unsigned i = 0; i < RT_ELEMENTS(aElfNotes); i++)
1418 {
1419 rc = ElfWriteNoteHeader(pVBoxCore, aElfNotes[i].Type, aElfNotes[i].pcv, aElfNotes[i].cb);
1420 if (RT_FAILURE(rc))
1421 {
1422 CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader failed for %s. rc=%Rrc\n", aElfNotes[i].pszType, rc));
1423 break;
1424 }
1425 }
1426
1427 /*
1428 * Write new-style thread info., missing lwpstatus_t indicates it's a zombie thread
1429 * we only dump the lwpsinfo_t in that case.
1430 */
1431 PVBOXSOLTHREADINFO pThreadInfo = pVBoxProc->pThreadInfoHead;
1432 for (; pThreadInfo; pThreadInfo = pThreadInfo->pNext)
1433 {
1434 rc = ElfWriteNoteHeader(pVBoxCore, NT_LWPSINFO, &pThreadInfo->Info, sizeof(lwpsinfo_t));
1435 if (RT_FAILURE(rc))
1436 {
1437 CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader for NT_LWPSINFO failed. rc=%Rrc\n", rc));
1438 break;
1439 }
1440
1441 if (pThreadInfo->pStatus)
1442 {
1443 rc = ElfWriteNoteHeader(pVBoxCore, NT_LWPSTATUS, pThreadInfo->pStatus, sizeof(lwpstatus_t));
1444 if (RT_FAILURE(rc))
1445 {
1446 CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader for NT_LWPSTATUS failed. rc=%Rrc\n", rc));
1447 break;
1448 }
1449 }
1450 }
1451 break;
1452 }
1453
1454 default:
1455 {
1456 CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: Invalid type %d\n", enmType));
1457 rc = VERR_GENERAL_FAILURE;
1458 break;
1459 }
1460 }
1461#else
1462# error Port Me!
1463#endif
1464 return rc;
1465}
1466
1467
1468/**
1469 * Write mappings into the core file.
1470 *
1471 * @param pVBoxCore Pointer to the core object.
1472 *
1473 * @return VBox status code.
1474 */
1475static int ElfWriteMappings(PVBOXCORE pVBoxCore)
1476{
1477 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
1478
1479 int rc = VERR_GENERAL_FAILURE;
1480 PVBOXSOLMAPINFO pMapInfo = pVBoxProc->pMapInfoHead;
1481 while (pMapInfo)
1482 {
1483 if (!pMapInfo->fError)
1484 {
1485 uint64_t k = 0;
1486 char achBuf[PAGE_SIZE];
1487 while (k < pMapInfo->pMap.pr_size)
1488 {
1489 size_t cb = RT_MIN(sizeof(achBuf), pMapInfo->pMap.pr_size - k);
1490 int rc2 = ProcReadAddrSpace(pVBoxProc, pMapInfo->pMap.pr_vaddr + k, &achBuf, cb);
1491 if (RT_FAILURE(rc2))
1492 {
1493 CORELOGRELSYS((CORELOG_NAME "ElfWriteMappings: Failed to read mapping, can't recover. Bye. rc=%Rrc\n", rc));
1494 return VERR_INVALID_STATE;
1495 }
1496
1497 rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, achBuf, sizeof(achBuf));
1498 if (RT_FAILURE(rc))
1499 {
1500 CORELOGRELSYS((CORELOG_NAME "ElfWriteMappings: pfnWriter failed. rc=%Rrc\n", rc));
1501 return rc;
1502 }
1503 k += cb;
1504 }
1505 }
1506 else
1507 {
1508 char achBuf[RT_ALIGN_Z(sizeof(int), 8)];
1509 RT_ZERO(achBuf);
1510 memcpy(achBuf, &pMapInfo->fError, sizeof(pMapInfo->fError));
1511 if (sizeof(achBuf) != pMapInfo->pMap.pr_size)
1512 CORELOGRELSYS((CORELOG_NAME "ElfWriteMappings: Huh!? something is wrong!\n"));
1513 rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &achBuf, sizeof(achBuf));
1514 if (RT_FAILURE(rc))
1515 {
1516 CORELOGRELSYS((CORELOG_NAME "ElfWriteMappings: pfnWriter(2) failed. rc=%Rrc\n", rc));
1517 return rc;
1518 }
1519 }
1520
1521 pMapInfo = pMapInfo->pNext;
1522 }
1523
1524 return VINF_SUCCESS;
1525}
1526
1527
1528/**
1529 * Write program headers for all mappings into the core file.
1530 *
1531 * @param pVBoxCore Pointer to the core object.
1532 *
1533 * @return VBox status code.
1534 */
1535static int ElfWriteMappingHeaders(PVBOXCORE pVBoxCore)
1536{
1537 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
1538
1539 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
1540 Phdr ProgHdr;
1541 RT_ZERO(ProgHdr);
1542 ProgHdr.p_type = PT_LOAD;
1543
1544 int rc = VERR_GENERAL_FAILURE;
1545 PVBOXSOLMAPINFO pMapInfo = pVBoxProc->pMapInfoHead;
1546 while (pMapInfo)
1547 {
1548 ProgHdr.p_vaddr = pMapInfo->pMap.pr_vaddr; /* Virtual address of this mapping in the process address space */
1549 ProgHdr.p_offset = pVBoxCore->offWrite; /* Where this mapping is located in the core file */
1550 ProgHdr.p_memsz = pMapInfo->pMap.pr_size; /* Size of the memory image of the mapping */
1551 ProgHdr.p_filesz = pMapInfo->pMap.pr_size; /* Size of the file image of the mapping */
1552
1553 ProgHdr.p_flags = 0; /* Reset fields in a loop when needed! */
1554 if (pMapInfo->pMap.pr_mflags & MA_READ)
1555 ProgHdr.p_flags |= PF_R;
1556 if (pMapInfo->pMap.pr_mflags & MA_WRITE)
1557 ProgHdr.p_flags |= PF_W;
1558 if (pMapInfo->pMap.pr_mflags & MA_EXEC)
1559 ProgHdr.p_flags |= PF_X;
1560
1561 if (pMapInfo->fError)
1562 ProgHdr.p_flags |= PF_SUNW_FAILURE;
1563
1564 rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &ProgHdr, sizeof(ProgHdr));
1565 if (RT_FAILURE(rc))
1566 {
1567 CORELOGRELSYS((CORELOG_NAME "ElfWriteMappingHeaders: pfnWriter failed. rc=%Rrc\n", rc));
1568 return rc;
1569 }
1570
1571 pVBoxCore->offWrite += ProgHdr.p_filesz;
1572 pMapInfo = pMapInfo->pNext;
1573 }
1574 return rc;
1575}
1576
1577
1578/**
1579 * Write a prepared core file using a user-passed in writer function, requires all threads
1580 * to be in suspended state (i.e. called after CreateCore).
1581 *
1582 * @param pVBoxCore Pointer to the core object.
1583 * @param pfnWriter Pointer to the writer function to override default writer (NULL uses default).
1584 *
1585 * @remarks Resumes all suspended threads, unless it's an invalid core.
1586 * @return VBox status.
1587 */
1588static int rtCoreDumperWriteCore(PVBOXCORE pVBoxCore, PFNCOREWRITER pfnWriter)
1589{
1590 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
1591
1592 if (!pVBoxCore->fIsValid)
1593 return VERR_INVALID_STATE;
1594
1595 if (pfnWriter)
1596 pVBoxCore->pfnWriter = pfnWriter;
1597
1598 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
1599 char szPath[PATH_MAX];
1600
1601 /*
1602 * Open the process address space file.
1603 */
1604 RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/as", (int)pVBoxProc->Process);
1605 int rc = RTFileOpen(&pVBoxProc->hAs, szPath, RTFILE_O_OPEN | RTFILE_O_READ);
1606 if (RT_FAILURE(rc))
1607 {
1608 CORELOGRELSYS((CORELOG_NAME "WriteCore: Failed to open address space, %s. rc=%Rrc\n", szPath, rc));
1609 goto WriteCoreDone;
1610 }
1611
1612 /*
1613 * Create the core file.
1614 */
1615 rc = RTFileOpen(&pVBoxCore->hCoreFile, pVBoxCore->szCorePath, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE | RTFILE_O_READWRITE | RTFILE_O_DENY_ALL);
1616 if (RT_FAILURE(rc))
1617 {
1618 CORELOGRELSYS((CORELOG_NAME "WriteCore: failed to open %s. rc=%Rrc\n", pVBoxCore->szCorePath, rc));
1619 goto WriteCoreDone;
1620 }
1621
1622 pVBoxCore->offWrite = 0;
1623 uint32_t cProgHdrs = pVBoxProc->cMappings + 2; /* two PT_NOTE program headers (old, new style) */
1624
1625 /*
1626 * Write the ELF header.
1627 */
1628 Ehdr ElfHdr;
1629 RT_ZERO(ElfHdr);
1630 ElfHdr.e_ident[EI_MAG0] = ELFMAG0;
1631 ElfHdr.e_ident[EI_MAG1] = ELFMAG1;
1632 ElfHdr.e_ident[EI_MAG2] = ELFMAG2;
1633 ElfHdr.e_ident[EI_MAG3] = ELFMAG3;
1634 ElfHdr.e_ident[EI_DATA] = IsBigEndian() ? ELFDATA2MSB : ELFDATA2LSB;
1635 ElfHdr.e_type = ET_CORE;
1636 ElfHdr.e_version = EV_CURRENT;
1637#ifdef RT_ARCH_AMD64
1638 ElfHdr.e_machine = EM_AMD64;
1639 ElfHdr.e_ident[EI_CLASS] = ELFCLASS64;
1640#else
1641 ElfHdr.e_machine = EM_386;
1642 ElfHdr.e_ident[EI_CLASS] = ELFCLASS32;
1643#endif
1644 if (cProgHdrs >= PN_XNUM)
1645 ElfHdr.e_phnum = PN_XNUM;
1646 else
1647 ElfHdr.e_phnum = cProgHdrs;
1648 ElfHdr.e_ehsize = sizeof(ElfHdr);
1649 ElfHdr.e_phoff = sizeof(ElfHdr);
1650 ElfHdr.e_phentsize = sizeof(Phdr);
1651 ElfHdr.e_shentsize = sizeof(Shdr);
1652 rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &ElfHdr, sizeof(ElfHdr));
1653 if (RT_FAILURE(rc))
1654 {
1655 CORELOGRELSYS((CORELOG_NAME "WriteCore: pfnWriter failed writing ELF header. rc=%Rrc\n", rc));
1656 goto WriteCoreDone;
1657 }
1658
1659 /*
1660 * Setup program header.
1661 */
1662 Phdr ProgHdr;
1663 RT_ZERO(ProgHdr);
1664 ProgHdr.p_type = PT_NOTE;
1665 ProgHdr.p_flags = PF_R;
1666
1667 /*
1668 * Write old-style NOTE program header.
1669 */
1670 pVBoxCore->offWrite += sizeof(ElfHdr) + cProgHdrs * sizeof(ProgHdr);
1671 ProgHdr.p_offset = pVBoxCore->offWrite;
1672 ProgHdr.p_filesz = ElfNoteSectionSize(pVBoxCore, enmOldEra);
1673 rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &ProgHdr, sizeof(ProgHdr));
1674 if (RT_FAILURE(rc))
1675 {
1676 CORELOGRELSYS((CORELOG_NAME "WriteCore: pfnWriter failed writing old-style ELF program Header. rc=%Rrc\n", rc));
1677 goto WriteCoreDone;
1678 }
1679
1680 /*
1681 * Write new-style NOTE program header.
1682 */
1683 pVBoxCore->offWrite += ProgHdr.p_filesz;
1684 ProgHdr.p_offset = pVBoxCore->offWrite;
1685 ProgHdr.p_filesz = ElfNoteSectionSize(pVBoxCore, enmNewEra);
1686 rc = pVBoxCore->pfnWriter(pVBoxCore->hCoreFile, &ProgHdr, sizeof(ProgHdr));
1687 if (RT_FAILURE(rc))
1688 {
1689 CORELOGRELSYS((CORELOG_NAME "WriteCore: pfnWriter failed writing new-style ELF program header. rc=%Rrc\n", rc));
1690 goto WriteCoreDone;
1691 }
1692
1693 /*
1694 * Write program headers per mapping.
1695 */
1696 pVBoxCore->offWrite += ProgHdr.p_filesz;
1697 rc = ElfWriteMappingHeaders(pVBoxCore);
1698 if (RT_FAILURE(rc))
1699 {
1700 CORELOGRELSYS((CORELOG_NAME "Write: ElfWriteMappings failed. rc=%Rrc\n", rc));
1701 goto WriteCoreDone;
1702 }
1703
1704 /*
1705 * Write old-style note section.
1706 */
1707 rc = ElfWriteNoteSection(pVBoxCore, enmOldEra);
1708 if (RT_FAILURE(rc))
1709 {
1710 CORELOGRELSYS((CORELOG_NAME "WriteCore: ElfWriteNoteSection old-style failed. rc=%Rrc\n", rc));
1711 goto WriteCoreDone;
1712 }
1713
1714 /*
1715 * Write new-style section.
1716 */
1717 rc = ElfWriteNoteSection(pVBoxCore, enmNewEra);
1718 if (RT_FAILURE(rc))
1719 {
1720 CORELOGRELSYS((CORELOG_NAME "WriteCore: ElfWriteNoteSection new-style failed. rc=%Rrc\n", rc));
1721 goto WriteCoreDone;
1722 }
1723
1724 /*
1725 * Write all mappings.
1726 */
1727 rc = ElfWriteMappings(pVBoxCore);
1728 if (RT_FAILURE(rc))
1729 {
1730 CORELOGRELSYS((CORELOG_NAME "WriteCore: ElfWriteMappings failed. rc=%Rrc\n", rc));
1731 goto WriteCoreDone;
1732 }
1733
1734
1735WriteCoreDone:
1736 if (pVBoxCore->hCoreFile != NIL_RTFILE)
1737 {
1738 RTFileClose(pVBoxCore->hCoreFile);
1739 pVBoxCore->hCoreFile = NIL_RTFILE;
1740 }
1741
1742 if (pVBoxProc->hAs != NIL_RTFILE)
1743 {
1744 RTFileClose(pVBoxProc->hAs);
1745 pVBoxProc->hAs = NIL_RTFILE;
1746 }
1747
1748 rtCoreDumperResumeThreads(pVBoxProc);
1749 return rc;
1750}
1751
1752
1753/**
1754 * Takes a process snapshot into a passed-in core object. It has the side-effect of halting
1755 * all threads which can lead to things like spurious wakeups of threads (if and when threads
1756 * are ultimately resumed en-masse) already suspended while calling this function.
1757 *
1758 * @param pVBoxCore Pointer to a core object.
1759 * @param pContext Pointer to the caller context thread.
1760 *
1761 * @remarks Halts all threads.
1762 * @return VBox status code.
1763 */
1764static int rtCoreDumperCreateCore(PVBOXCORE pVBoxCore, ucontext_t *pContext)
1765{
1766 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
1767 AssertReturn(pContext, VERR_INVALID_POINTER);
1768
1769 /*
1770 * Initialize core structures.
1771 */
1772 memset(pVBoxCore, 0, sizeof(VBOXCORE));
1773 pVBoxCore->pfnReader = &ReadFileNoIntr;
1774 pVBoxCore->pfnWriter = &WriteFileNoIntr;
1775 pVBoxCore->fIsValid = false;
1776 pVBoxCore->hCoreFile = NIL_RTFILE;
1777
1778 PVBOXPROCESS pVBoxProc = &pVBoxCore->VBoxProc;
1779 pVBoxProc->Process = RTProcSelf();
1780 pVBoxProc->hCurThread = _lwp_self(); /* thr_self() */
1781 pVBoxProc->hAs = NIL_RTFILE;
1782 pVBoxProc->pCurThreadCtx = pContext;
1783 pVBoxProc->CoreContent = CC_CONTENT_DEFAULT;
1784
1785 RTProcGetExecutableName(pVBoxProc->szExecPath, sizeof(pVBoxProc->szExecPath)); /* this gets full path not just name */
1786 pVBoxProc->pszExecName = RTPathFilename(pVBoxProc->szExecPath);
1787
1788 /*
1789 * If no output directory is specified, use current directory.
1790 */
1791 if (g_szCoreDumpDir[0] == '\0')
1792 g_szCoreDumpDir[0] = '.';
1793
1794 if (g_szCoreDumpFile[0] == '\0')
1795 {
1796 /* We cannot call RTPathAbs*() as they call getcwd() which calls malloc. */
1797 RTStrPrintf(pVBoxCore->szCorePath, sizeof(pVBoxCore->szCorePath), "%s/core.vb.%s.%d",
1798 g_szCoreDumpDir, pVBoxProc->pszExecName, (int)pVBoxProc->Process);
1799 }
1800 else
1801 RTStrPrintf(pVBoxCore->szCorePath, sizeof(pVBoxCore->szCorePath), "%s/core.vb.%s", g_szCoreDumpDir, g_szCoreDumpFile);
1802
1803 CORELOG((CORELOG_NAME "CreateCore: Taking Core %s from Thread %d\n", pVBoxCore->szCorePath, (int)pVBoxProc->hCurThread));
1804
1805 /*
1806 * Quiesce the process.
1807 */
1808 int rc = rtCoreDumperSuspendThreads(pVBoxProc);
1809 if (RT_SUCCESS(rc))
1810 {
1811 rc = ProcReadInfo(pVBoxCore);
1812 if (RT_SUCCESS(rc))
1813 {
1814 GetOldProcessInfo(pVBoxCore, &pVBoxProc->ProcInfoOld);
1815 if (IsProcessArchNative(pVBoxProc))
1816 {
1817 /*
1818 * Read process status, information such as number of active LWPs will be invalid since we just quiesced the process.
1819 */
1820 rc = ProcReadStatus(pVBoxCore);
1821 if (RT_SUCCESS(rc))
1822 {
1823 rc = AllocMemoryArea(pVBoxCore);
1824 if (RT_SUCCESS(rc))
1825 {
1826 struct COREACCUMULATOR
1827 {
1828 const char *pszName;
1829 PFNCOREACCUMULATOR pfnAcc;
1830 bool fOptional;
1831 } aAccumulators[] =
1832 {
1833 { "ProcReadLdt", &ProcReadLdt, false },
1834 { "ProcReadCred", &ProcReadCred, false },
1835 { "ProcReadPriv", &ProcReadPriv, false },
1836 { "ProcReadAuxVecs", &ProcReadAuxVecs, false },
1837 { "ProcReadMappings", &ProcReadMappings, false },
1838 { "ProcReadThreads", &ProcReadThreads, false },
1839 { "ProcReadMiscInfo", &ProcReadMiscInfo, false }
1840 };
1841
1842 for (unsigned i = 0; i < RT_ELEMENTS(aAccumulators); i++)
1843 {
1844 rc = aAccumulators[i].pfnAcc(pVBoxCore);
1845 if (RT_FAILURE(rc))
1846 {
1847 CORELOGRELSYS((CORELOG_NAME "CreateCore: %s failed. rc=%Rrc\n", aAccumulators[i].pszName, rc));
1848 if (!aAccumulators[i].fOptional)
1849 break;
1850 }
1851 }
1852
1853 if (RT_SUCCESS(rc))
1854 {
1855 pVBoxCore->fIsValid = true;
1856 return VINF_SUCCESS;
1857 }
1858
1859 FreeMemoryArea(pVBoxCore);
1860 }
1861 else
1862 CORELOGRELSYS((CORELOG_NAME "CreateCore: AllocMemoryArea failed. rc=%Rrc\n", rc));
1863 }
1864 else
1865 CORELOGRELSYS((CORELOG_NAME "CreateCore: ProcReadStatus failed. rc=%Rrc\n", rc));
1866 }
1867 else
1868 {
1869 CORELOGRELSYS((CORELOG_NAME "CreateCore: IsProcessArchNative failed.\n"));
1870 rc = VERR_BAD_EXE_FORMAT;
1871 }
1872 }
1873 else
1874 CORELOGRELSYS((CORELOG_NAME "CreateCore: ProcReadInfo failed. rc=%Rrc\n", rc));
1875
1876 /*
1877 * Resume threads on failure.
1878 */
1879 rtCoreDumperResumeThreads(pVBoxProc);
1880 }
1881 else
1882 CORELOG((CORELOG_NAME "CreateCore: SuspendAllThreads failed. Thread bomb!?! rc=%Rrc\n", rc));
1883
1884 return rc;
1885}
1886
1887
1888/**
1889 * Destroy an existing core object.
1890 *
1891 * @param pVBoxCore Pointer to the core object.
1892 *
1893 * @return VBox status code.
1894 */
1895static int rtCoreDumperDestroyCore(PVBOXCORE pVBoxCore)
1896{
1897 AssertReturn(pVBoxCore, VERR_INVALID_POINTER);
1898 if (!pVBoxCore->fIsValid)
1899 return VERR_INVALID_STATE;
1900
1901 FreeMemoryArea(pVBoxCore);
1902 pVBoxCore->fIsValid = false;
1903 return VINF_SUCCESS;
1904}
1905
1906
1907/**
1908 * Takes a core dump. This function has no other parameters than the context
1909 * because it can be called from signal handlers.
1910 *
1911 * @param pContext The context of the caller.
1912 * @returns VBox status code.
1913 */
1914static int rtCoreDumperTakeDump(ucontext_t *pContext)
1915{
1916 if (!pContext)
1917 {
1918 CORELOGRELSYS((CORELOG_NAME "TakeDump: Missing context.\n"));
1919 return VERR_INVALID_POINTER;
1920 }
1921
1922 /*
1923 * Take a snapshot, then dump core to disk, all threads except this one are halted
1924 * from before taking the snapshot until writing the core is completely finished.
1925 * Any errors would resume all threads if they were halted.
1926 */
1927 VBOXCORE VBoxCore;
1928 RT_ZERO(VBoxCore);
1929 int rc = rtCoreDumperCreateCore(&VBoxCore, pContext);
1930 if (RT_SUCCESS(rc))
1931 {
1932 rc = rtCoreDumperWriteCore(&VBoxCore, &WriteFileNoIntr);
1933 if (RT_SUCCESS(rc))
1934 CORELOGRELSYS((CORELOG_NAME "Success! Core written to %s\n", VBoxCore.szCorePath));
1935 else
1936 CORELOGRELSYS((CORELOG_NAME "TakeDump: WriteCore failed. szCorePath=%s rc=%Rrc\n", VBoxCore.szCorePath, rc));
1937
1938 rtCoreDumperDestroyCore(&VBoxCore);
1939 }
1940 else
1941 CORELOGRELSYS((CORELOG_NAME "TakeDump: CreateCore failed. rc=%Rrc\n", rc));
1942
1943 return rc;
1944}
1945
1946
1947/**
1948 * The signal handler that will be invoked to take core dumps.
1949 *
1950 * @param Sig The signal that invoked us.
1951 * @param pSigInfo The signal information.
1952 * @param pvArg Opaque pointer to the caller context structure,
1953 * this cannot be NULL.
1954 */
1955static void rtCoreDumperSignalHandler(int Sig, siginfo_t *pSigInfo, void *pvArg)
1956{
1957 CORELOG((CORELOG_NAME "SignalHandler Sig=%d pvArg=%p\n", Sig, pvArg));
1958
1959 int rc = VERR_GENERAL_FAILURE;
1960 bool fCallSystemDump = false;
1961 if (ASMAtomicUoReadBool(&g_fCoreDumpInProgress) == false)
1962 {
1963 ASMAtomicWriteBool(&g_fCoreDumpInProgress, true);
1964 ASMAtomicWriteU64(&g_CoreDumpThread, (uint64_t)RTThreadSelf());
1965
1966 rc = rtCoreDumperTakeDump((ucontext_t *)pvArg);
1967
1968 ASMAtomicWriteU64(&g_CoreDumpThread, NIL_RTTHREAD);
1969 ASMAtomicWriteBool(&g_fCoreDumpInProgress, false);
1970
1971 if (RT_FAILURE(rc))
1972 {
1973 /*
1974 * If it is NOT a deliberate dump taken by us & our handler fails we assume the
1975 * worst, try to use the system signal handler and abort the process.
1976 */
1977 CORELOGRELSYS((CORELOG_NAME "TakeDump failed! rc=%Rrc\n", rc));
1978 if (ASMAtomicReadBool(&g_fCoreDumpDeliberate) == false)
1979 fCallSystemDump = true;
1980 }
1981 }
1982 else
1983 {
1984 /*
1985 * Core dumping is already in progress and we've somehow ended up being
1986 * signalled again.
1987 */
1988 rc = VERR_INTERNAL_ERROR;
1989
1990 /*
1991 * If our dumper has crashed. No point in waiting, trigger the system one.
1992 * Wait only when the dumping thread is not the one generating this signal.
1993 */
1994 if (ASMAtomicReadU64(&g_CoreDumpThread) != (uint64_t)RTThreadSelf())
1995 fCallSystemDump = true;
1996 else
1997 {
1998 CORELOGRELSYS((CORELOG_NAME "SignalHandler: Core dump already in progress! Waiting before signalling Sig=%d.\n", Sig));
1999 int64_t iTimeout = 10000; /* timeout (ms) */
2000 while (ASMAtomicUoReadBool(&g_fCoreDumpInProgress) == true)
2001 {
2002 RTThreadSleep(200);
2003 iTimeout -= 200;
2004 if (iTimeout <= 0)
2005 break;
2006 }
2007 if (iTimeout <= 0)
2008 {
2009 fCallSystemDump = true;
2010 CORELOGRELSYS((CORELOG_NAME "SignalHandler: Core dump seems to be stuck. Signalling new signal %d\n", Sig));
2011 }
2012 }
2013 }
2014
2015 if (fCallSystemDump)
2016 {
2017 signal(Sig, SIG_DFL);
2018 raise(Sig);
2019 }
2020}
2021
2022
2023/**
2024 * Take a core dump of the current process without terminating it.
2025 *
2026 * @returns IPRT status code.
2027 * @param pszOutputFile Name of the core file. If NULL use the
2028 * default naming scheme.
2029 */
2030RTDECL(int) RTCoreDumperTakeDump(const char *pszOutputFile)
2031{
2032 if (ASMAtomicReadBool(&g_fCoreDumpSignalSetup) == false)
2033 return VERR_WRONG_ORDER;
2034
2035 RT_ZERO(g_szCoreDumpFile);
2036 if (pszOutputFile)
2037 RTStrCopy(g_szCoreDumpFile, sizeof(g_szCoreDumpFile), pszOutputFile);
2038
2039 ASMAtomicWriteBool(&g_fCoreDumpDeliberate, true);
2040 raise(SIGSEGV);
2041 ASMAtomicWriteBool(&g_fCoreDumpDeliberate, false);
2042 return VINF_SUCCESS;
2043}
2044
2045
2046/**
2047 * Sets up and enables the core dumper.
2048 *
2049 * Installs signal / unhandled exception handlers for catching fatal errors
2050 * that should result in a core dump. If you wish to install your own handlers
2051 * you should do that after calling this function and make sure you pass on
2052 * events you don't handle.
2053 *
2054 * This can be called multiple times to change the settings without needing to
2055 * call RTCoreDumperDisable in between.
2056 *
2057 * @param pszOutputDir The directory to store the cores in. If NULL
2058 * the current directory will be used.
2059 * @param pszBaseName Base file name, no directory. If NULL the
2060 * dumper will generate an appropriate name.
2061 * @param fFlags Reserved for later, MBZ.
2062 */
2063RTDECL(int) RTCoreDumperSetup(const char *pszOutputDir, uint32_t fFlags)
2064{
2065 /*
2066 * Validate flags.
2067 */
2068 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
2069
2070 /*
2071 * Install core dump signal handler.
2072 */
2073 struct sigaction sigAct;
2074 sigAct.sa_sigaction = &rtCoreDumperSignalHandler;
2075 sigemptyset(&sigAct.sa_mask);
2076 sigAct.sa_flags = SA_RESTART | SA_SIGINFO;
2077 sigaction(SIGSEGV, &sigAct, NULL);
2078 sigaction(SIGBUS, &sigAct, NULL);
2079
2080 ASMAtomicWriteBool(&g_fCoreDumpSignalSetup, true);
2081
2082 RT_ZERO(g_szCoreDumpDir);
2083 if (pszOutputDir)
2084 RTStrCopy(g_szCoreDumpDir, sizeof(g_szCoreDumpDir), pszOutputDir);
2085
2086 ASMAtomicWriteU32(&g_fCoreDumpFlags, fFlags);
2087
2088 return VINF_SUCCESS;
2089}
2090
2091
2092/**
2093 * Disables the core dumper, i.e. undoes what RTCoreDumperSetup did.
2094 *
2095 * @returns IPRT status code.
2096 */
2097RTDECL(int) RTCoreDumperDisable(void)
2098{
2099 /*
2100 * Remove core dump signal handler & reset variables.
2101 */
2102 signal(SIGSEGV, SIG_DFL);
2103 signal(SIGBUS, SIG_DFL);
2104 ASMAtomicWriteBool(&g_fCoreDumpSignalSetup, false);
2105
2106 RT_ZERO(g_szCoreDumpDir);
2107 RT_ZERO(g_szCoreDumpFile);
2108 return VINF_SUCCESS;
2109}
2110
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