VirtualBox

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

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

IPRT/coredumper: remove release log entry, far too dangerous to create a self-deadlock with the signal handler

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