1 | /* $Id: dbgkrnlinfo-r0drv-solaris.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Kernel debug information, Ring-0 Driver, Solaris Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "the-solaris-kernel.h"
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #include <iprt/dbg.h>
|
---|
45 | #include <iprt/asm.h>
|
---|
46 | #include <iprt/assert.h>
|
---|
47 | #include <iprt/err.h>
|
---|
48 | #include <iprt/log.h>
|
---|
49 | #include <iprt/mem.h>
|
---|
50 | #include <iprt/string.h>
|
---|
51 | #include <iprt/thread.h>
|
---|
52 |
|
---|
53 | #include "internal/magics.h"
|
---|
54 |
|
---|
55 |
|
---|
56 | /*********************************************************************************************************************************
|
---|
57 | * Structures and Typedefs *
|
---|
58 | *********************************************************************************************************************************/
|
---|
59 | /**
|
---|
60 | * Solaris kernel debug info instance data.
|
---|
61 | */
|
---|
62 | typedef struct RTDBGKRNLINFOINT
|
---|
63 | {
|
---|
64 | /** Magic value (RTDBGKRNLINFO_MAGIC). */
|
---|
65 | uint32_t volatile u32Magic;
|
---|
66 | /** The number of threads referencing this object. */
|
---|
67 | uint32_t volatile cRefs;
|
---|
68 | /** Pointer to the genunix CTF handle. */
|
---|
69 | ctf_file_t *pGenUnixCTF;
|
---|
70 | /** Pointer to the genunix module handle. */
|
---|
71 | modctl_t *pGenUnixMod;
|
---|
72 | } RTDBGKRNLINFOINT;
|
---|
73 | /** Pointer to the solaris kernel debug info instance data. */
|
---|
74 | typedef struct RTDBGKRNLINFOINT *PRTDBGKRNLINFOINT;
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Retains a kernel module and opens the CTF data associated with it.
|
---|
79 | *
|
---|
80 | * @param pszModule The name of the module to open.
|
---|
81 | * @param ppMod Where to store the module handle.
|
---|
82 | * @param ppCTF Where to store the module's CTF handle.
|
---|
83 | *
|
---|
84 | * @return IPRT status code.
|
---|
85 | */
|
---|
86 | static int rtR0DbgKrnlInfoModRetain(char *pszModule, modctl_t **ppMod, ctf_file_t **ppCTF)
|
---|
87 | {
|
---|
88 | AssertPtrReturn(pszModule, VERR_INVALID_PARAMETER);
|
---|
89 | AssertPtrReturn(ppMod, VERR_INVALID_PARAMETER);
|
---|
90 | AssertPtrReturn(ppCTF, VERR_INVALID_PARAMETER);
|
---|
91 |
|
---|
92 | int rc = VINF_SUCCESS;
|
---|
93 | modid_t ModId = mod_name_to_modid(pszModule);
|
---|
94 | if (ModId != -1)
|
---|
95 | {
|
---|
96 | *ppMod = mod_hold_by_id(ModId);
|
---|
97 | if (*ppMod)
|
---|
98 | {
|
---|
99 | /*
|
---|
100 | * Hold mod_lock as ctf_modopen may update the module with uncompressed CTF data.
|
---|
101 | */
|
---|
102 | int err;
|
---|
103 | mutex_enter(&mod_lock);
|
---|
104 | *ppCTF = ctf_modopen(((modctl_t *)*ppMod)->mod_mp, &err);
|
---|
105 | mutex_exit(&mod_lock);
|
---|
106 | mod_release_mod(*ppMod);
|
---|
107 |
|
---|
108 | if (*ppCTF)
|
---|
109 | return VINF_SUCCESS;
|
---|
110 | else
|
---|
111 | {
|
---|
112 | LogRel(("rtR0DbgKrnlInfoModRetain: ctf_modopen failed for '%s' err=%d\n", pszModule, err));
|
---|
113 | rc = VERR_INTERNAL_ERROR_3;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | LogRel(("rtR0DbgKrnlInfoModRetain: mod_hold_by_id failed for '%s'\n", pszModule));
|
---|
119 | rc = VERR_INTERNAL_ERROR_2;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | else
|
---|
123 | {
|
---|
124 | LogRel(("rtR0DbgKrnlInfoModRetain: mod_name_to_modid failed for '%s'\n", pszModule));
|
---|
125 | rc = VERR_INTERNAL_ERROR;
|
---|
126 | }
|
---|
127 |
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Releases the kernel module and closes its CTF data.
|
---|
134 | *
|
---|
135 | * @param pMod Pointer to the module handle.
|
---|
136 | * @param pCTF Pointer to the module's CTF handle.
|
---|
137 | */
|
---|
138 | static void rtR0DbgKrnlInfoModRelease(modctl_t *pMod, ctf_file_t *pCTF)
|
---|
139 | {
|
---|
140 | AssertPtrReturnVoid(pMod);
|
---|
141 | AssertPtrReturnVoid(pCTF);
|
---|
142 |
|
---|
143 | ctf_close(pCTF);
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Helper for opening the specified kernel module.
|
---|
149 | *
|
---|
150 | * @param pszModule The name of the module.
|
---|
151 | * @param ppMod Where to store the module handle.
|
---|
152 | * @param ppCtf Where to store the module's CTF handle.
|
---|
153 | *
|
---|
154 | * @returns Pointer to the CTF structure for the module.
|
---|
155 | */
|
---|
156 | static int rtR0DbgKrnlInfoModRetainEx(const char *pszModule, modctl_t **ppMod, ctf_file_t **ppCtf)
|
---|
157 | {
|
---|
158 | char *pszMod = RTStrDup(pszModule);
|
---|
159 | if (RT_LIKELY(pszMod))
|
---|
160 | {
|
---|
161 | int rc = rtR0DbgKrnlInfoModRetain(pszMod, ppMod, ppCtf);
|
---|
162 | RTStrFree(pszMod);
|
---|
163 | if (RT_SUCCESS(rc))
|
---|
164 | {
|
---|
165 | AssertPtrReturn(*ppMod, VERR_INTERNAL_ERROR_2);
|
---|
166 | AssertPtrReturn(*ppCtf, VERR_INTERNAL_ERROR_3);
|
---|
167 | }
|
---|
168 | return rc;
|
---|
169 | }
|
---|
170 | return VERR_NO_MEMORY;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | RTR0DECL(int) RTR0DbgKrnlInfoOpen(PRTDBGKRNLINFO phKrnlInfo, uint32_t fFlags)
|
---|
175 | {
|
---|
176 | AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
|
---|
177 | AssertPtrReturn(phKrnlInfo, VERR_INVALID_POINTER);
|
---|
178 | /* This can be called as part of IPRT init, in which case we have no thread preempt information yet. */
|
---|
179 | if (g_frtSolInitDone)
|
---|
180 | RT_ASSERT_PREEMPTIBLE();
|
---|
181 |
|
---|
182 | *phKrnlInfo = NIL_RTDBGKRNLINFO;
|
---|
183 | PRTDBGKRNLINFOINT pThis = (PRTDBGKRNLINFOINT)RTMemAllocZ(sizeof(*pThis));
|
---|
184 | if (!pThis)
|
---|
185 | return VERR_NO_MEMORY;
|
---|
186 |
|
---|
187 | char szGenUnixModName[] = "genunix";
|
---|
188 | int rc = rtR0DbgKrnlInfoModRetain(szGenUnixModName, &pThis->pGenUnixMod, &pThis->pGenUnixCTF);
|
---|
189 | if (RT_SUCCESS(rc))
|
---|
190 | {
|
---|
191 | pThis->u32Magic = RTDBGKRNLINFO_MAGIC;
|
---|
192 | pThis->cRefs = 1;
|
---|
193 |
|
---|
194 | *phKrnlInfo = pThis;
|
---|
195 | return VINF_SUCCESS;
|
---|
196 | }
|
---|
197 |
|
---|
198 | LogRel(("RTR0DbgKrnlInfoOpen: rtR0DbgKrnlInfoModRetain failed rc=%d.\n", rc));
|
---|
199 | RTMemFree(pThis);
|
---|
200 | return rc;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | RTR0DECL(uint32_t) RTR0DbgKrnlInfoRetain(RTDBGKRNLINFO hKrnlInfo)
|
---|
205 | {
|
---|
206 | PRTDBGKRNLINFOINT pThis = hKrnlInfo;
|
---|
207 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
208 | AssertMsgReturn(pThis->u32Magic == RTDBGKRNLINFO_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), UINT32_MAX);
|
---|
209 |
|
---|
210 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
211 | Assert(cRefs && cRefs < 100000);
|
---|
212 | return cRefs;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | RTR0DECL(uint32_t) RTR0DbgKrnlInfoRelease(RTDBGKRNLINFO hKrnlInfo)
|
---|
217 | {
|
---|
218 | PRTDBGKRNLINFOINT pThis = hKrnlInfo;
|
---|
219 | if (pThis == NIL_RTDBGKRNLINFO)
|
---|
220 | return 0;
|
---|
221 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
222 | AssertMsgReturn(pThis->u32Magic == RTDBGKRNLINFO_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), UINT32_MAX);
|
---|
223 | if (g_frtSolInitDone)
|
---|
224 | RT_ASSERT_PREEMPTIBLE();
|
---|
225 |
|
---|
226 | uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
227 | if (cRefs == 0)
|
---|
228 | {
|
---|
229 | pThis->u32Magic = ~RTDBGKRNLINFO_MAGIC;
|
---|
230 | rtR0DbgKrnlInfoModRelease(pThis->pGenUnixMod, pThis->pGenUnixCTF);
|
---|
231 | RTMemFree(pThis);
|
---|
232 | }
|
---|
233 | return cRefs;
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | RTR0DECL(int) RTR0DbgKrnlInfoQueryMember(RTDBGKRNLINFO hKrnlInfo, const char *pszModule, const char *pszStructure,
|
---|
238 | const char *pszMember, size_t *poffMember)
|
---|
239 | {
|
---|
240 | PRTDBGKRNLINFOINT pThis = hKrnlInfo;
|
---|
241 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
242 | AssertMsgReturn(pThis->u32Magic == RTDBGKRNLINFO_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
243 | AssertPtrReturn(pszMember, VERR_INVALID_PARAMETER);
|
---|
244 | AssertPtrReturn(pszStructure, VERR_INVALID_PARAMETER);
|
---|
245 | AssertPtrReturn(poffMember, VERR_INVALID_PARAMETER);
|
---|
246 | if (g_frtSolInitDone)
|
---|
247 | RT_ASSERT_PREEMPTIBLE();
|
---|
248 |
|
---|
249 | ctf_file_t *pCtf = NULL;
|
---|
250 | modctl_t *pMod = NULL;
|
---|
251 | if (!pszModule)
|
---|
252 | {
|
---|
253 | pCtf = pThis->pGenUnixCTF;
|
---|
254 | pMod = pThis->pGenUnixMod;
|
---|
255 | }
|
---|
256 | else
|
---|
257 | {
|
---|
258 | int rc2 = rtR0DbgKrnlInfoModRetainEx(pszModule, &pMod, &pCtf);
|
---|
259 | if (RT_FAILURE(rc2))
|
---|
260 | return rc2;
|
---|
261 | Assert(pMod);
|
---|
262 | Assert(pCtf);
|
---|
263 | }
|
---|
264 |
|
---|
265 | int rc = VERR_NOT_FOUND;
|
---|
266 | ctf_id_t TypeIdent = ctf_lookup_by_name(pCtf, pszStructure);
|
---|
267 | if (TypeIdent != CTF_ERR)
|
---|
268 | {
|
---|
269 | ctf_membinfo_t MemberInfo;
|
---|
270 | RT_ZERO(MemberInfo);
|
---|
271 | if (ctf_member_info(pCtf, TypeIdent, pszMember, &MemberInfo) != CTF_ERR)
|
---|
272 | {
|
---|
273 | *poffMember = (MemberInfo.ctm_offset >> 3);
|
---|
274 | rc = VINF_SUCCESS;
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | if (pszModule)
|
---|
279 | rtR0DbgKrnlInfoModRelease(pMod, pCtf);
|
---|
280 | return rc;
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | RTR0DECL(int) RTR0DbgKrnlInfoQuerySymbol(RTDBGKRNLINFO hKrnlInfo, const char *pszModule,
|
---|
285 | const char *pszSymbol, void **ppvSymbol)
|
---|
286 | {
|
---|
287 | PRTDBGKRNLINFOINT pThis = hKrnlInfo;
|
---|
288 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
289 | AssertMsgReturn(pThis->u32Magic == RTDBGKRNLINFO_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
290 | AssertPtrReturn(pszSymbol, VERR_INVALID_PARAMETER);
|
---|
291 | AssertPtrNullReturn(ppvSymbol, VERR_INVALID_PARAMETER);
|
---|
292 | AssertReturn(!pszModule, VERR_MODULE_NOT_FOUND);
|
---|
293 | if (g_frtSolInitDone)
|
---|
294 | RT_ASSERT_PREEMPTIBLE();
|
---|
295 |
|
---|
296 | uintptr_t uValue = kobj_getsymvalue((char *)pszSymbol, 1 /* only kernel */);
|
---|
297 | if (ppvSymbol)
|
---|
298 | *ppvSymbol = (void *)uValue;
|
---|
299 | if (uValue)
|
---|
300 | return VINF_SUCCESS;
|
---|
301 | return VERR_SYMBOL_NOT_FOUND;
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | RTR0DECL(int) RTR0DbgKrnlInfoQuerySize(RTDBGKRNLINFO hKrnlInfo, const char *pszModule, const char *pszType, size_t *pcbType)
|
---|
306 | {
|
---|
307 | PRTDBGKRNLINFOINT pThis = hKrnlInfo;
|
---|
308 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
309 | AssertMsgReturn(pThis->u32Magic == RTDBGKRNLINFO_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
310 | AssertPtrReturn(pszType, VERR_INVALID_PARAMETER);
|
---|
311 | AssertPtrReturn(pcbType, VERR_INVALID_PARAMETER);
|
---|
312 | if (g_frtSolInitDone)
|
---|
313 | RT_ASSERT_PREEMPTIBLE();
|
---|
314 |
|
---|
315 | modctl_t *pMod = NULL;
|
---|
316 | ctf_file_t *pCtf = NULL;
|
---|
317 | if (!pszModule)
|
---|
318 | {
|
---|
319 | pCtf = pThis->pGenUnixCTF;
|
---|
320 | pMod = pThis->pGenUnixMod;
|
---|
321 | }
|
---|
322 | else
|
---|
323 | {
|
---|
324 | int rc2 = rtR0DbgKrnlInfoModRetainEx(pszModule, &pMod, &pCtf);
|
---|
325 | if (RT_FAILURE(rc2))
|
---|
326 | return rc2;
|
---|
327 | Assert(pMod);
|
---|
328 | Assert(pCtf);
|
---|
329 | }
|
---|
330 |
|
---|
331 | int rc = VERR_NOT_FOUND;
|
---|
332 | ctf_id_t TypeIdent = ctf_lookup_by_name(pCtf, pszType);
|
---|
333 | if (TypeIdent != CTF_ERR)
|
---|
334 | {
|
---|
335 | ssize_t cbType = ctf_type_size(pCtf, TypeIdent);
|
---|
336 | if (cbType > 0)
|
---|
337 | {
|
---|
338 | *pcbType = cbType;
|
---|
339 | rc = VINF_SUCCESS;
|
---|
340 | }
|
---|
341 | else
|
---|
342 | rc = VERR_WRONG_TYPE;
|
---|
343 | }
|
---|
344 |
|
---|
345 | if (pszModule)
|
---|
346 | rtR0DbgKrnlInfoModRelease(pMod, pCtf);
|
---|
347 | return rc;
|
---|
348 | }
|
---|
349 |
|
---|