VirtualBox

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

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

RTCoreDumper: debug build fix.

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