VirtualBox

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

Last change on this file since 46646 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

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