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