VirtualBox

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

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

coredumper-solaris: minor change.

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