summaryrefslogtreecommitdiffstats
path: root/src/VBox/Debugger/DBGPlugInLinuxModuleCodeTmpl.cpp.h
blob: 876bbac1d2db6a37aed57485a9e24a1aea2419cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* $Id: DBGPlugInLinuxModuleCodeTmpl.cpp.h $ */
/** @file
 * DBGPlugInLinux - Code template for struct module processing.
 */

/*
 * Copyright (C) 2019-2020 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
#ifndef LNX_MK_VER
# define LNX_MK_VER(major, minor, build) (((major) << 22) | ((minor) << 12) | (build))
#endif
#if LNX_64BIT
# define LNX_ULONG_T uint64_t
#else
# define LNX_ULONG_T uint32_t
#endif
#if LNX_64BIT
# define PAD32ON64(seq) uint32_t RT_CONCAT(u32Padding,seq);
#else
# define PAD32ON64(seq)
#endif


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
#if LNX_VER >= LNX_MK_VER(2,6,11)
typedef struct RT_CONCAT(LNXMODKOBJECT,LNX_SUFFIX)
{
    LNX_PTR_T           uPtrKName;
# if LNX_VER < LNX_MK_VER(2,6,24)
    char                name[20];
# endif
# if LNX_VER < LNX_MK_VER(2,6,27)
    int32_t             cRefs;
#  if LNX_VER >= LNX_MK_VER(2,6,24)
    PAD32ON64(0)
#  endif
# endif
    LNX_PTR_T           uPtrNext;
    LNX_PTR_T           uPtrPrev;
    LNX_PTR_T           uPtrParent; /**< struct kobject pointer */
    LNX_PTR_T           uPtrKset;   /**< struct kset pointer */
    LNX_PTR_T           uPtrKtype;  /**< struct kobj_type pointer */
    LNX_PTR_T           uPtrDirEntry; /**< struct dentry pointer; 2.6.23+ sysfs_dirent. */
# if LNX_VER >= LNX_MK_VER(2,6,17) && LNX_VER < LNX_MK_VER(2,6,24)
    LNX_PTR_T           aPtrWaitQueueHead[3];
# endif
# if LNX_VER >= LNX_MK_VER(2,6,27)
    int32_t             cRefs;
    uint32_t            uStateStuff;
# elif LNX_VER >= LNX_MK_VER(2,6,25)
    LNX_ULONG_T         uStateStuff;
# endif
    /* non-kobject: */
    LNX_PTR_T           uPtrModule;     /**< struct module pointer. */
#  if LNX_VER >= LNX_MK_VER(2,6,21)
    LNX_PTR_T           uPtrDriverDir;  /**< Points to struct kobject. */
#  endif
} RT_CONCAT(LNXMODKOBJECT,LNX_SUFFIX);
#endif
#if LNX_VER == LNX_MK_VER(2,6,24) && LNX_64BIT
AssertCompileMemberOffset(RT_CONCAT(LNXMODKOBJECT,LNX_SUFFIX), uPtrParent, 32);
AssertCompileMemberOffset(RT_CONCAT(LNXMODKOBJECT,LNX_SUFFIX), uPtrParent, 32);
AssertCompileSize(RT_CONCAT(LNXMODKOBJECT,LNX_SUFFIX), 80);
#endif



/**
 * Maps to the start of struct module in include/linux/module.h.
 */
typedef struct RT_CONCAT(LNXKMODULE,LNX_SUFFIX)
{
#if LNX_VER >= LNX_MK_VER(2,5,48)
    /*
     * This first part is mostly always the same.
     */
    int32_t     state;
    PAD32ON64(0)
    LNX_PTR_T   uPtrNext;
    LNX_PTR_T   uPtrPrev;
    char        name[64 - sizeof(LNX_PTR_T)];

    /*
     * Here be spaghetti dragons.
     */
# if LNX_VER >= LNX_MK_VER(2,6,11)
    RT_CONCAT(LNXMODKOBJECT,LNX_SUFFIX) mkobj; /**< Was just kobj for a while. */
    LNX_PTR_T   uPtrParamAttrs;     /**< Points to struct module_param_attrs. */
#  if LNX_VER >= LNX_MK_VER(2,6,17)
    LNX_PTR_T   uPtrModInfoAttrs;   /**< Points to struct module_attribute. */
#  endif
#  if LNX_VER == LNX_MK_VER(2,6,20)
    LNX_PTR_T   uPtrDriverDir;      /**< Points to struct kobject. */
#  elif LNX_VER >= LNX_MK_VER(2,6,21)
    LNX_PTR_T   uPtrHolderDir;      /**< Points to struct kobject. */
#  endif
#  if LNX_VER >= LNX_MK_VER(2,6,13)
    LNX_PTR_T   uPtrVersion;        /**< String pointers. */
    LNX_PTR_T   uPtrSrcVersion;     /**< String pointers. */
#  endif
# else
#  if LNX_VER >= LNX_MK_VER(2,6,7)
    LNX_PTR_T   uPtrMkObj;
#  endif
#  if LNX_VER >= LNX_MK_VER(2,6,10)
    LNX_PTR_T   uPtrParamsKobject;
#  endif
# endif

    /** @name Exported Symbols
     * @{ */
# if LNX_VER < LNX_MK_VER(2,5,67)
    LNX_PTR_T   uPtrSymsNext, uPtrSymsPrev, uPtrSymsOwner;
#  if LNX_VER >= LNX_MK_VER(2,5,55)
    int32_t     syms_gplonly;
    uint32_t    num_syms;
# else
    uint32_t    num_syms;
    PAD32ON64(1)
#  endif
# endif
    LNX_PTR_T   uPtrSyms; /**< Array of struct kernel_symbol. */
# if LNX_VER >= LNX_MK_VER(2,5,67)
    uint32_t    num_syms;
    PAD32ON64(1)
# endif
# if LNX_VER >= LNX_MK_VER(2,5,60)
    LNX_PTR_T   uPtrCrcs; /**< unsigned long array */
# endif
    /** @} */

    /** @name GPL Symbols
     * @since 2.5.55
     * @{ */
# if LNX_VER >= LNX_MK_VER(2,5,55)
#  if LNX_VER < LNX_MK_VER(2,5,67)
    LNX_PTR_T   uPtrGplSymsNext, uPtrGplSymsPrev, uPtrGplSymsOwner;
#   if LNX_VER >= LNX_MK_VER(2,5,55)
    int32_t     gpl_syms_gplonly;
    uint32_t    num_gpl_syms;
#  else
    uint32_t    num_gpl_syms;
    PAD32ON64(2)
#   endif
#  endif
    LNX_PTR_T   uPtrGplSyms; /**< Array of struct kernel_symbol. */
#  if LNX_VER >= LNX_MK_VER(2,5,67)
    uint32_t    num_gpl_syms;
    PAD32ON64(2)
#  endif
#  if LNX_VER >= LNX_MK_VER(2,5,60)
    LNX_PTR_T   uPtrGplCrcs; /**< unsigned long array */
#  endif
# endif /* > 2.5.55 */
    /** @} */

    /** @name Unused Exported Symbols
     * @since 2.6.18
     * @{ */
# if LNX_VER >= LNX_MK_VER(2,6,18)
    LNX_PTR_T   uPtrUnusedSyms; /**< Array of struct kernel_symbol. */
    uint32_t    num_unused_syms;
    PAD32ON64(4)
    LNX_PTR_T   uPtrUnusedCrcs; /**< unsigned long array */
# endif
    /** @} */

    /** @name Unused GPL Symbols
     * @since 2.6.18
     * @{ */
# if LNX_VER >= LNX_MK_VER(2,6,18)
    LNX_PTR_T   uPtrUnusedGplSyms; /**< Array of struct kernel_symbol. */
    uint32_t    num_unused_gpl_syms;
    PAD32ON64(5)
    LNX_PTR_T   uPtrUnusedGplCrcs; /**< unsigned long array */
# endif
    /** @} */

    /** @name Future GPL Symbols
     * @since 2.6.17
     * @{ */
# if LNX_VER >= LNX_MK_VER(2,6,17)
    LNX_PTR_T   uPtrGplFutureSyms; /**< Array of struct kernel_symbol. */
    uint32_t    num_gpl_future_syms;
    PAD32ON64(3)
    LNX_PTR_T   uPtrGplFutureCrcs; /**< unsigned long array */
# endif
    /** @} */

    /** @name Exception table.
     * @{ */
# if LNX_VER < LNX_MK_VER(2,5,67)
    LNX_PTR_T   uPtrXcptTabNext, uPtrXcptTabPrev;
# endif
    uint32_t    num_exentries;
    PAD32ON64(6)
    LNX_PTR_T   uPtrEntries; /**< struct exception_table_entry array. */
    /** @} */

    /*
     * Hopefully less spaghetti from here on...
     */
    LNX_PTR_T   pfnInit;
    LNX_PTR_T   uPtrModuleInit;
    LNX_PTR_T   uPtrModuleCore;
    LNX_ULONG_T cbInit;
    LNX_ULONG_T cbCore;
# if LNX_VER >= LNX_MK_VER(2,5,74)
    LNX_ULONG_T cbInitText;
    LNX_ULONG_T cbCoreText;
# endif

# if LNX_VER >= LNX_MK_VER(2,6,18)
    LNX_PTR_T   uPtrUnwindInfo;
# endif
#else
   uint32_t     structure_size;

#endif
} RT_CONCAT(LNXKMODULE,LNX_SUFFIX);

# if LNX_VER == LNX_MK_VER(2,6,24) && LNX_64BIT
AssertCompileMemberOffset(RT_CONCAT(LNXKMODULE,LNX_SUFFIX), uPtrParamAttrs, 160);
AssertCompileMemberOffset(RT_CONCAT(LNXKMODULE,LNX_SUFFIX), num_syms, 208);
AssertCompileMemberOffset(RT_CONCAT(LNXKMODULE,LNX_SUFFIX), num_gpl_syms, 232);
AssertCompileMemberOffset(RT_CONCAT(LNXKMODULE,LNX_SUFFIX), num_unused_syms, 256);
AssertCompileMemberOffset(RT_CONCAT(LNXKMODULE,LNX_SUFFIX), num_unused_gpl_syms, 280);
AssertCompileMemberOffset(RT_CONCAT(LNXKMODULE,LNX_SUFFIX), num_gpl_future_syms, 304);
AssertCompileMemberOffset(RT_CONCAT(LNXKMODULE,LNX_SUFFIX), num_exentries, 320);
AssertCompileMemberOffset(RT_CONCAT(LNXKMODULE,LNX_SUFFIX), uPtrModuleCore, 352);
AssertCompileMemberOffset(RT_CONCAT(LNXKMODULE,LNX_SUFFIX), uPtrUnwindInfo, 392);
#endif



/**
 * Version specific module processing code.
 */
static uint64_t RT_CONCAT(dbgDiggerLinuxLoadModule,LNX_SUFFIX)(PDBGDIGGERLINUX pThis, PUVM pUVM, PDBGFADDRESS pAddrModule)
{
    RT_CONCAT(LNXKMODULE,LNX_SUFFIX) Module;

    int rc = DBGFR3MemRead(pUVM, 0, DBGFR3AddrSub(pAddrModule, RT_UOFFSETOF(RT_CONCAT(LNXKMODULE,LNX_SUFFIX), uPtrNext)),
                           &Module, sizeof(Module));
    if (RT_FAILURE(rc))
    {
        LogRelFunc(("Failed to read module structure at %#RX64: %Rrc\n", pAddrModule->FlatPtr, rc));
        return 0;
    }

    /*
     * Check the module name.
     */
#if LNX_VER >= LNX_MK_VER(2,5,48)
    const char  *pszName = Module.name;
    size_t const cbName  = sizeof(Module.name);
#else

#endif
    if (   RTStrNLen(pszName, cbName) >= cbName
        || RT_FAILURE(RTStrValidateEncoding(pszName))
        || *pszName == '\0')
    {
        LogRelFunc(("%#RX64: Bad name: %.*Rhxs\n", pAddrModule->FlatPtr, (int)cbName, pszName));
        return 0;
    }

    /*
     * Create a simple module for it.
     */
    LogRelFunc((" %#RX64: %#RX64 LB %#RX64 %s\n", pAddrModule->FlatPtr, Module.uPtrModuleCore, Module.cbCore, pszName));

    RTDBGMOD hDbgMod;
    rc = RTDbgModCreate(&hDbgMod, pszName, Module.cbCore, 0 /*fFlags*/);
    if (RT_SUCCESS(rc))
    {
        rc = RTDbgModSetTag(hDbgMod, DIG_LNX_MOD_TAG);
        if (RT_SUCCESS(rc))
        {
            RTDBGAS hAs = DBGFR3AsResolveAndRetain(pUVM, DBGF_AS_KERNEL);
            rc = RTDbgAsModuleLink(hAs, hDbgMod, Module.uPtrModuleCore, RTDBGASLINK_FLAGS_REPLACE /*fFlags*/);
            RTDbgAsRelease(hAs);
        }
        else
            LogRel(("DbgDiggerOs2: RTDbgModSetTag failed: %Rrc\n", rc));
        RTDbgModRelease(hDbgMod);
    }

    RT_NOREF(pThis);
    return Module.uPtrNext;
}

#undef LNX_VER
#undef LNX_SUFFIX
#undef LNX_ULONG_T
#undef PAD32ON64