1 | /* $Id: DBGPlugInLinux.cpp 35346 2010-12-27 16:13:13Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DBGPlugInLinux - Debugger and Guest OS Digger Plugin For Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DBGF ///@todo add new log group.
|
---|
23 | #include "DBGPlugIns.h"
|
---|
24 | #include "DBGPlugInCommonELF.h"
|
---|
25 | #include <VBox/vmm/dbgf.h>
|
---|
26 | #include <iprt/string.h>
|
---|
27 | #include <iprt/mem.h>
|
---|
28 | #include <iprt/stream.h>
|
---|
29 | #include <iprt/ctype.h>
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Structures and Typedefs *
|
---|
34 | *******************************************************************************/
|
---|
35 |
|
---|
36 | /** @name InternalLinux structures
|
---|
37 | * @{ */
|
---|
38 |
|
---|
39 |
|
---|
40 | /** @} */
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Linux guest OS digger instance data.
|
---|
45 | */
|
---|
46 | typedef struct DBGDIGGERLINUX
|
---|
47 | {
|
---|
48 | /** Whether the information is valid or not.
|
---|
49 | * (For fending off illegal interface method calls.) */
|
---|
50 | bool fValid;
|
---|
51 |
|
---|
52 | /** The address of the linux banner.
|
---|
53 | * This is set during probing. */
|
---|
54 | DBGFADDRESS AddrLinuxBanner;
|
---|
55 | /** Kernel base address.
|
---|
56 | * This is set during probing. */
|
---|
57 | DBGFADDRESS AddrKernelBase;
|
---|
58 | } DBGDIGGERLINUX;
|
---|
59 | /** Pointer to the linux guest OS digger instance data. */
|
---|
60 | typedef DBGDIGGERLINUX *PDBGDIGGERLINUX;
|
---|
61 |
|
---|
62 |
|
---|
63 | /*******************************************************************************
|
---|
64 | * Defined Constants And Macros *
|
---|
65 | *******************************************************************************/
|
---|
66 | /** Validates a 32-bit linux kernel address */
|
---|
67 | #define LNX32_VALID_ADDRESS(Addr) ((Addr) > UINT32_C(0x80000000) && (Addr) < UINT32_C(0xfffff000))
|
---|
68 |
|
---|
69 | /** The max kernel size. */
|
---|
70 | #define LNX_MAX_KERNEL_SIZE 0x0f000000
|
---|
71 |
|
---|
72 |
|
---|
73 | /*******************************************************************************
|
---|
74 | * Internal Functions *
|
---|
75 | *******************************************************************************/
|
---|
76 | static DECLCALLBACK(int) dbgDiggerLinuxInit(PVM pVM, void *pvData);
|
---|
77 |
|
---|
78 |
|
---|
79 | /*******************************************************************************
|
---|
80 | * Global Variables *
|
---|
81 | *******************************************************************************/
|
---|
82 | /** Table of common linux kernel addresses. */
|
---|
83 | static uint64_t g_au64LnxKernelAddresses[] =
|
---|
84 | {
|
---|
85 | UINT64_C(0xc0100000),
|
---|
86 | UINT64_C(0x90100000),
|
---|
87 | UINT64_C(0xffffffff80200000)
|
---|
88 | };
|
---|
89 |
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * @copydoc DBGFOSREG::pfnQueryInterface
|
---|
93 | */
|
---|
94 | static DECLCALLBACK(void *) dbgDiggerLinuxQueryInterface(PVM pVM, void *pvData, DBGFOSINTERFACE enmIf)
|
---|
95 | {
|
---|
96 | return NULL;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * @copydoc DBGFOSREG::pfnQueryVersion
|
---|
102 | */
|
---|
103 | static DECLCALLBACK(int) dbgDiggerLinuxQueryVersion(PVM pVM, void *pvData, char *pszVersion, size_t cchVersion)
|
---|
104 | {
|
---|
105 | PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
|
---|
106 | Assert(pThis->fValid);
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * It's all in the linux banner.
|
---|
110 | */
|
---|
111 | int rc = DBGFR3MemReadString(pVM, 0, &pThis->AddrLinuxBanner, pszVersion, cchVersion);
|
---|
112 | if (RT_SUCCESS(rc))
|
---|
113 | {
|
---|
114 | char *pszEnd = RTStrEnd(pszVersion, cchVersion);
|
---|
115 | AssertReturn(pszEnd, VERR_BUFFER_OVERFLOW);
|
---|
116 | while ( pszEnd > pszVersion
|
---|
117 | && RT_C_IS_SPACE(pszEnd[-1]))
|
---|
118 | pszEnd--;
|
---|
119 | *pszEnd = '\0';
|
---|
120 | }
|
---|
121 | else
|
---|
122 | RTStrPrintf(pszVersion, cchVersion, "DBGFR3MemRead -> %Rrc", rc);
|
---|
123 |
|
---|
124 | return rc;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * @copydoc DBGFOSREG::pfnTerm
|
---|
130 | */
|
---|
131 | static DECLCALLBACK(void) dbgDiggerLinuxTerm(PVM pVM, void *pvData)
|
---|
132 | {
|
---|
133 | PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
|
---|
134 | Assert(pThis->fValid);
|
---|
135 |
|
---|
136 | pThis->fValid = false;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * @copydoc DBGFOSREG::pfnRefresh
|
---|
142 | */
|
---|
143 | static DECLCALLBACK(int) dbgDiggerLinuxRefresh(PVM pVM, void *pvData)
|
---|
144 | {
|
---|
145 | PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
|
---|
146 | NOREF(pThis);
|
---|
147 | Assert(pThis->fValid);
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * For now we'll flush and reload everything.
|
---|
151 | */
|
---|
152 | dbgDiggerLinuxTerm(pVM, pvData);
|
---|
153 | return dbgDiggerLinuxInit(pVM, pvData);
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * @copydoc DBGFOSREG::pfnInit
|
---|
159 | */
|
---|
160 | static DECLCALLBACK(int) dbgDiggerLinuxInit(PVM pVM, void *pvData)
|
---|
161 | {
|
---|
162 | PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
|
---|
163 | Assert(!pThis->fValid);
|
---|
164 | #if 0 /* later */
|
---|
165 | int rc;
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Algorithm to find the ksymtab:
|
---|
169 | * 1. Find a known export string in kstrtab ("init_task", "enable_hlt" or something).
|
---|
170 | * 2. Search for the address its at, this should give you the corresponding ksymtab entry.
|
---|
171 | * 3. Search backwards assuming that kstrtab is corresponding to ksymtab.
|
---|
172 | */
|
---|
173 | DBGFADDRESS AddrKernelKsymTab;
|
---|
174 |
|
---|
175 |
|
---|
176 | #endif
|
---|
177 | pThis->fValid = true;
|
---|
178 | return VINF_SUCCESS;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * @copydoc DBGFOSREG::pfnProbe
|
---|
184 | */
|
---|
185 | static DECLCALLBACK(bool) dbgDiggerLinuxProbe(PVM pVM, void *pvData)
|
---|
186 | {
|
---|
187 | PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * Look for "Linux version " at the start of the rodata segment.
|
---|
191 | * Hope that this comes before any message buffer or other similar string.
|
---|
192 | * .
|
---|
193 | * Note! Only Linux version 2.x.y, where x in {0..6}. .
|
---|
194 | */
|
---|
195 | for (unsigned i = 0; i < RT_ELEMENTS(g_au64LnxKernelAddresses); i++)
|
---|
196 | {
|
---|
197 | DBGFADDRESS KernelAddr;
|
---|
198 | DBGFR3AddrFromFlat(pVM, &KernelAddr, g_au64LnxKernelAddresses[i]);
|
---|
199 | DBGFADDRESS HitAddr;
|
---|
200 | static const uint8_t s_abLinuxVersion[] = "Linux version 2.";
|
---|
201 | int rc = DBGFR3MemScan(pVM, 0, &KernelAddr, LNX_MAX_KERNEL_SIZE, 1,
|
---|
202 | s_abLinuxVersion, sizeof(s_abLinuxVersion) - 1, &HitAddr);
|
---|
203 | if (RT_SUCCESS(rc))
|
---|
204 | {
|
---|
205 | char szTmp[128];
|
---|
206 | char const *pszY = &szTmp[sizeof(s_abLinuxVersion) - 1];
|
---|
207 | rc = DBGFR3MemReadString(pVM, 0, &HitAddr, szTmp, sizeof(szTmp));
|
---|
208 | if ( RT_SUCCESS(rc)
|
---|
209 | && *pszY >= '0'
|
---|
210 | && *pszY <= '6')
|
---|
211 | {
|
---|
212 | pThis->AddrKernelBase = KernelAddr;
|
---|
213 | pThis->AddrLinuxBanner = HitAddr;
|
---|
214 | return true;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * @copydoc DBGFOSREG::pfnDestruct
|
---|
224 | */
|
---|
225 | static DECLCALLBACK(void) dbgDiggerLinuxDestruct(PVM pVM, void *pvData)
|
---|
226 | {
|
---|
227 |
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * @copydoc DBGFOSREG::pfnConstruct
|
---|
233 | */
|
---|
234 | static DECLCALLBACK(int) dbgDiggerLinuxConstruct(PVM pVM, void *pvData)
|
---|
235 | {
|
---|
236 | return VINF_SUCCESS;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | const DBGFOSREG g_DBGDiggerLinux =
|
---|
241 | {
|
---|
242 | /* .u32Magic = */ DBGFOSREG_MAGIC,
|
---|
243 | /* .fFlags = */ 0,
|
---|
244 | /* .cbData = */ sizeof(DBGDIGGERLINUX),
|
---|
245 | /* .szName = */ "Linux",
|
---|
246 | /* .pfnConstruct = */ dbgDiggerLinuxConstruct,
|
---|
247 | /* .pfnDestruct = */ dbgDiggerLinuxDestruct,
|
---|
248 | /* .pfnProbe = */ dbgDiggerLinuxProbe,
|
---|
249 | /* .pfnInit = */ dbgDiggerLinuxInit,
|
---|
250 | /* .pfnRefresh = */ dbgDiggerLinuxRefresh,
|
---|
251 | /* .pfnTerm = */ dbgDiggerLinuxTerm,
|
---|
252 | /* .pfnQueryVersion = */ dbgDiggerLinuxQueryVersion,
|
---|
253 | /* .pfnQueryInterface = */ dbgDiggerLinuxQueryInterface,
|
---|
254 | /* .u32EndMagic = */ DBGFOSREG_MAGIC
|
---|
255 | };
|
---|
256 |
|
---|