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