1 | /* $Id: dbg-r0drv-solaris.c 40966 2012-04-17 16:43:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Kernel debug information, Ring-0 Driver, Solaris Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 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 | */
|
---|
52 | typedef 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. */
|
---|
64 | typedef 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 | */
|
---|
76 | static 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 | */
|
---|
128 | static 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 | RTR0DECL(int) RTR0DbgKrnlInfoOpen(PRTDBGKRNLINFO phKrnlInfo, uint32_t fFlags)
|
---|
138 | {
|
---|
139 | AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
|
---|
140 | AssertPtrReturn(phKrnlInfo, VERR_INVALID_POINTER);
|
---|
141 | RT_ASSERT_PREEMPTIBLE();
|
---|
142 |
|
---|
143 | PRTDBGKRNLINFOINT pThis = (PRTDBGKRNLINFOINT)RTMemAllocZ(sizeof(*pThis));
|
---|
144 | if (!pThis)
|
---|
145 | return VERR_NO_MEMORY;
|
---|
146 |
|
---|
147 | char szGenUnixModName[] = "genunix";
|
---|
148 | int rc = rtR0DbgKrnlInfoModRetain(szGenUnixModName, &pThis->pGenUnixMod, &pThis->pGenUnixCTF);
|
---|
149 | if (RT_SUCCESS(rc))
|
---|
150 | {
|
---|
151 | pThis->u32Magic = RTDBGKRNLINFO_MAGIC;
|
---|
152 | pThis->cRefs = 1;
|
---|
153 |
|
---|
154 | *phKrnlInfo = pThis;
|
---|
155 | return VINF_SUCCESS;
|
---|
156 | }
|
---|
157 |
|
---|
158 | LogRel(("RTR0DbgKrnlInfoOpen: rtR0DbgKrnlInfoModRetain failed rc=%d.\n", rc));
|
---|
159 | RTMemFree(pThis);
|
---|
160 | return rc;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | RTR0DECL(uint32_t) RTR0DbgKrnlInfoRetain(RTDBGKRNLINFO hKrnlInfo)
|
---|
165 | {
|
---|
166 | PRTDBGKRNLINFOINT pThis = hKrnlInfo;
|
---|
167 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
168 | AssertMsgReturn(pThis->u32Magic == RTDBGKRNLINFO_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), UINT32_MAX);
|
---|
169 |
|
---|
170 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
171 | Assert(cRefs && cRefs < 100000);
|
---|
172 | return cRefs;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | RTR0DECL(uint32_t) RTR0DbgKrnlInfoRelease(RTDBGKRNLINFO hKrnlInfo)
|
---|
177 | {
|
---|
178 | PRTDBGKRNLINFOINT pThis = hKrnlInfo;
|
---|
179 | if (pThis == NIL_RTDBGKRNLINFO)
|
---|
180 | return 0;
|
---|
181 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
182 | AssertMsgReturn(pThis->u32Magic == RTDBGKRNLINFO_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), UINT32_MAX);
|
---|
183 | RT_ASSERT_PREEMPTIBLE();
|
---|
184 |
|
---|
185 | uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
186 | if (cRefs == 0)
|
---|
187 | {
|
---|
188 | pThis->u32Magic = ~RTDBGKRNLINFO_MAGIC;
|
---|
189 | rtR0DbgKrnlInfoModRelease(pThis->pGenUnixMod, pThis->pGenUnixCTF);
|
---|
190 | RTMemFree(pThis);
|
---|
191 | }
|
---|
192 | return cRefs;
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | RTR0DECL(int) RTR0DbgKrnlInfoQueryMember(RTDBGKRNLINFO hKrnlInfo, const char *pszStructure,
|
---|
197 | const char *pszMember, size_t *poffMember)
|
---|
198 | {
|
---|
199 | PRTDBGKRNLINFOINT pThis = hKrnlInfo;
|
---|
200 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
201 | AssertMsgReturn(pThis->u32Magic == RTDBGKRNLINFO_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
202 | AssertPtrReturn(pszMember, VERR_INVALID_PARAMETER);
|
---|
203 | AssertPtrReturn(pszStructure, VERR_INVALID_PARAMETER);
|
---|
204 | AssertPtrReturn(poffMember, VERR_INVALID_PARAMETER);
|
---|
205 | RT_ASSERT_PREEMPTIBLE();
|
---|
206 |
|
---|
207 | int rc = VERR_NOT_FOUND;
|
---|
208 | ctf_id_t TypeIdent = ctf_lookup_by_name(pThis->pGenUnixCTF, pszStructure);
|
---|
209 | if (TypeIdent != CTF_ERR)
|
---|
210 | {
|
---|
211 | ctf_membinfo_t MemberInfo;
|
---|
212 | RT_ZERO(MemberInfo);
|
---|
213 | if (ctf_member_info(pThis->pGenUnixCTF, TypeIdent, pszMember, &MemberInfo) != CTF_ERR)
|
---|
214 | {
|
---|
215 | *poffMember = (MemberInfo.ctm_offset >> 3);
|
---|
216 | return VINF_SUCCESS;
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | return rc;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | RTR0DECL(int) RTR0DbgKrnlInfoQuerySymbol(RTDBGKRNLINFO hKrnlInfo, const char *pszModule,
|
---|
225 | const char *pszSymbol, void **ppvSymbol)
|
---|
226 | {
|
---|
227 | PRTDBGKRNLINFOINT pThis = hKrnlInfo;
|
---|
228 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
229 | AssertMsgReturn(pThis->u32Magic == RTDBGKRNLINFO_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
230 | AssertPtrReturn(pszSymbol, VERR_INVALID_PARAMETER);
|
---|
231 | AssertPtrNullReturn(ppvSymbol, VERR_INVALID_PARAMETER);
|
---|
232 | AssertReturn(!pszModule, VERR_MODULE_NOT_FOUND);
|
---|
233 | RT_ASSERT_PREEMPTIBLE();
|
---|
234 |
|
---|
235 | uintptr_t uValue = kobj_getsymvalue((char *)pszSymbol, 1 /* only kernel */);
|
---|
236 | if (ppvSymbol)
|
---|
237 | *ppvSymbol = (void *)uValue;
|
---|
238 | if (uValue)
|
---|
239 | return VINF_SUCCESS;
|
---|
240 | return VERR_SYMBOL_NOT_FOUND;
|
---|
241 | }
|
---|
242 |
|
---|