VirtualBox

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

Last change on this file since 40504 was 40504, checked in by vboxsync, 13 years ago

Solaris 11 build fixes.

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