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