VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/dbgkrnlinfo-r0drv-solaris.c@ 73265

Last change on this file since 73265 was 69111, checked in by vboxsync, 7 years ago

(C) year

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette