VirtualBox

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

Last change on this file since 32347 was 32347, checked in by vboxsync, 14 years ago

Runtime/r3/coredumper: Setup SIGUSR2 and flags, allow taking live cores.

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

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