VirtualBox

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

Last change on this file since 62096 was 62096, checked in by vboxsync, 9 years ago

Runtime/r3/solaris: Fix coredumper to workaround psinfo_t structure layout differences across known Solaris versions.

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