VirtualBox

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

Last change on this file was 107550, checked in by vboxsync, 4 months ago

Runtime/r3/solaris/coredumper-solaris: bugref:3409 Unused variables and redundant assigment.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette