VirtualBox

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

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

Runtime/r3/solaris: signal handler fixes.

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