VirtualBox

source: vbox/trunk/src/VBox/Debugger/DBGPlugInLinux.cpp@ 85629

Last change on this file since 85629 was 84989, checked in by vboxsync, 4 years ago

Debugger/DBGPlugInLinux: Straighten the code to adjust the kernel log buffer size to not require a loop, bugref:1098

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 118.0 KB
Line 
1/* $Id: DBGPlugInLinux.cpp 84989 2020-06-29 11:12:40Z vboxsync $ */
2/** @file
3 * DBGPlugInLinux - Debugger and Guest OS Digger Plugin For Linux.
4 */
5
6/*
7 * Copyright (C) 2008-2020 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 <VBox/dis.h>
27#include <iprt/ctype.h>
28#include <iprt/file.h>
29#include <iprt/err.h>
30#include <iprt/mem.h>
31#include <iprt/stream.h>
32#include <iprt/string.h>
33#include <iprt/vfs.h>
34#include <iprt/zip.h>
35
36
37/*********************************************************************************************************************************
38* Structures and Typedefs *
39*********************************************************************************************************************************/
40
41/** @name InternalLinux structures
42 * @{ */
43
44
45/** @} */
46
47
48/**
49 * Config item type.
50 */
51typedef enum DBGDIGGERLINUXCFGITEMTYPE
52{
53 /** Invalid type. */
54 DBGDIGGERLINUXCFGITEMTYPE_INVALID = 0,
55 /** String. */
56 DBGDIGGERLINUXCFGITEMTYPE_STRING,
57 /** Number. */
58 DBGDIGGERLINUXCFGITEMTYPE_NUMBER,
59 /** Flag whether this feature is included in the
60 * kernel or as a module. */
61 DBGDIGGERLINUXCFGITEMTYPE_FLAG
62} DBGDIGGERLINUXCFGITEMTYPE;
63
64/**
65 * Item in the config database.
66 */
67typedef struct DBGDIGGERLINUXCFGITEM
68{
69 /** String space core. */
70 RTSTRSPACECORE Core;
71 /** Config item type. */
72 DBGDIGGERLINUXCFGITEMTYPE enmType;
73 /** Data based on the type. */
74 union
75 {
76 /** Number. */
77 int64_t i64Num;
78 /** Flag. */
79 bool fModule;
80 /** String - variable in size. */
81 char aszString[1];
82 } u;
83} DBGDIGGERLINUXCFGITEM;
84/** Pointer to a config database item. */
85typedef DBGDIGGERLINUXCFGITEM *PDBGDIGGERLINUXCFGITEM;
86/** Pointer to a const config database item. */
87typedef const DBGDIGGERLINUXCFGITEM *PCDBGDIGGERLINUXCFGITEM;
88
89/**
90 * Linux guest OS digger instance data.
91 */
92typedef struct DBGDIGGERLINUX
93{
94 /** Whether the information is valid or not.
95 * (For fending off illegal interface method calls.) */
96 bool fValid;
97 /** Set if 64-bit, clear if 32-bit. */
98 bool f64Bit;
99 /** Set if the kallsyms table uses relative addressing, clear
100 * if absolute addresses are used. */
101 bool fRelKrnlAddr;
102 /** The relative base when kernel symbols use offsets rather than
103 * absolute addresses. */
104 RTGCUINTPTR uKernelRelativeBase;
105
106 /** The address of the linux banner.
107 * This is set during probing. */
108 DBGFADDRESS AddrLinuxBanner;
109 /** Kernel base address.
110 * This is set during probing, refined during kallsyms parsing. */
111 DBGFADDRESS AddrKernelBase;
112 /** The kernel size. */
113 uint32_t cbKernel;
114
115 /** The number of kernel symbols (kallsyms_num_syms).
116 * This is set during init. */
117 uint32_t cKernelSymbols;
118 /** The size of the kernel name table (sizeof(kallsyms_names)). */
119 uint32_t cbKernelNames;
120 /** Number of entries in the kernel_markers table. */
121 uint32_t cKernelNameMarkers;
122 /** The size of the kernel symbol token table. */
123 uint32_t cbKernelTokenTable;
124 /** The address of the encoded kernel symbol names (kallsyms_names). */
125 DBGFADDRESS AddrKernelNames;
126 /** The address of the kernel symbol addresses (kallsyms_addresses). */
127 DBGFADDRESS AddrKernelAddresses;
128 /** The address of the kernel symbol name markers (kallsyms_markers). */
129 DBGFADDRESS AddrKernelNameMarkers;
130 /** The address of the kernel symbol token table (kallsyms_token_table). */
131 DBGFADDRESS AddrKernelTokenTable;
132 /** The address of the kernel symbol token index table (kallsyms_token_index). */
133 DBGFADDRESS AddrKernelTokenIndex;
134
135 /** The kernel message log interface. */
136 DBGFOSIDMESG IDmesg;
137
138 /** The config database root. */
139 RTSTRSPACE hCfgDb;
140} DBGDIGGERLINUX;
141/** Pointer to the linux guest OS digger instance data. */
142typedef DBGDIGGERLINUX *PDBGDIGGERLINUX;
143
144
145/**
146 * The current printk_log structure.
147 */
148typedef struct LNXPRINTKHDR
149{
150 /** Monotonic timestamp. */
151 uint64_t nsTimestamp;
152 /** The total size of this message record. */
153 uint16_t cbTotal;
154 /** The size of the text part (immediately follows the header). */
155 uint16_t cbText;
156 /** The size of the optional dictionary part (follows the text). */
157 uint16_t cbDict;
158 /** The syslog facility number. */
159 uint8_t bFacility;
160 /** First 5 bits are internal flags, next 3 bits are log level. */
161 uint8_t fFlagsAndLevel;
162} LNXPRINTKHDR;
163AssertCompileSize(LNXPRINTKHDR, 2*sizeof(uint64_t));
164/** Pointer to linux printk_log header. */
165typedef LNXPRINTKHDR *PLNXPRINTKHDR;
166/** Pointer to linux const printk_log header. */
167typedef LNXPRINTKHDR const *PCLNXPRINTKHDR;
168
169
170/*********************************************************************************************************************************
171* Defined Constants And Macros *
172*********************************************************************************************************************************/
173/** First kernel map address for 32bit Linux hosts (__START_KERNEL_map). */
174#define LNX32_KERNEL_ADDRESS_START UINT32_C(0xc0000000)
175/** First kernel map address for 64bit Linux hosts (__START_KERNEL_map). */
176#define LNX64_KERNEL_ADDRESS_START UINT64_C(0xffffffff80000000)
177/** Validates a 32-bit linux kernel address */
178#define LNX32_VALID_ADDRESS(Addr) ((Addr) > UINT32_C(0x80000000) && (Addr) < UINT32_C(0xfffff000))
179/** Validates a 64-bit linux kernel address */
180#define LNX64_VALID_ADDRESS(Addr) ((Addr) > UINT64_C(0xffff800000000000) && (Addr) < UINT64_C(0xfffffffffffff000))
181
182/** The max kernel size. */
183#define LNX_MAX_KERNEL_SIZE UINT32_C(0x0f000000)
184/** Maximum kernel log buffer size. */
185#define LNX_MAX_KERNEL_LOG_SIZE (16 * _1M)
186
187/** The maximum size we expect for kallsyms_names. */
188#define LNX_MAX_KALLSYMS_NAMES_SIZE UINT32_C(0x200000)
189/** The maximum size we expect for kallsyms_token_table. */
190#define LNX_MAX_KALLSYMS_TOKEN_TABLE_SIZE UINT32_C(0x10000)
191/** The minimum number of symbols we expect in kallsyms_num_syms. */
192#define LNX_MIN_KALLSYMS_SYMBOLS UINT32_C(2048)
193/** The maximum number of symbols we expect in kallsyms_num_syms. */
194#define LNX_MAX_KALLSYMS_SYMBOLS UINT32_C(1048576)
195/** The min length an encoded symbol in kallsyms_names is expected to have. */
196#define LNX_MIN_KALLSYMS_ENC_LENGTH UINT8_C(1)
197/** The max length an encoded symbol in kallsyms_names is expected to have.
198 * @todo check real life here. */
199#define LNX_MAX_KALLSYMS_ENC_LENGTH UINT8_C(28)
200/** The approximate maximum length of a string token. */
201#define LNX_MAX_KALLSYMS_TOKEN_LEN UINT16_C(32)
202/** Maximum compressed config size expected. */
203#define LNX_MAX_COMPRESSED_CFG_SIZE _1M
204
205/** Module tag for linux ('linuxmod' on little endian ASCII systems). */
206#define DIG_LNX_MOD_TAG UINT64_C(0x545f5d78758e898c)
207
208
209/*********************************************************************************************************************************
210* Internal Functions *
211*********************************************************************************************************************************/
212static DECLCALLBACK(int) dbgDiggerLinuxInit(PUVM pUVM, void *pvData);
213
214
215/*********************************************************************************************************************************
216* Global Variables *
217*********************************************************************************************************************************/
218/** Table of common linux kernel addresses. */
219static uint64_t g_au64LnxKernelAddresses[] =
220{
221 UINT64_C(0xc0100000),
222 UINT64_C(0x90100000),
223 UINT64_C(0xffffffff80200000)
224};
225
226static const uint8_t g_abLinuxVersion[] = "Linux version ";
227/** The needle for searching for the kernel log area (the value is observed in pretty much all 32bit and 64bit x86 kernels).
228 * This needle should appear only once in the memory due to the address being filled in by a format string. */
229static const uint8_t g_abKrnlLogNeedle[] = "BIOS-e820: [mem 0x0000000000000000";
230
231
232/**
233 * Tries to resolve the kernel log buffer start and end by searching for needle.
234 *
235 * @returns VBox status code.
236 * @param pThis The Linux digger data.
237 * @param pUVM The VM handle.
238 * @param pGCPtrLogBuf Where to store the start of the kernel log buffer on success.
239 * @param pcbLogBuf Where to store the size of the kernel log buffer on success.
240 */
241static int dbgDiggerLinuxKrnlLogBufFindByNeedle(PDBGDIGGERLINUX pThis, PUVM pUVM, RTGCPTR *pGCPtrLogBuf, uint32_t *pcbLogBuf)
242{
243 int rc = VINF_SUCCESS;
244
245 /* Try to find the needle, it should be very early in the kernel log buffer. */
246 DBGFADDRESS AddrScan;
247 DBGFADDRESS AddrHit;
248 DBGFR3AddrFromFlat(pUVM, &AddrScan, pThis->f64Bit ? LNX64_KERNEL_ADDRESS_START : LNX32_KERNEL_ADDRESS_START);
249
250 rc = DBGFR3MemScan(pUVM, 0 /*idCpu*/, &AddrScan, ~(RTGCUINTPTR)0, 1 /*uAlign*/,
251 g_abKrnlLogNeedle, sizeof(g_abKrnlLogNeedle) - 1, &AddrHit);
252 if (RT_SUCCESS(rc))
253 {
254 uint32_t cbLogBuf = 0;
255 uint64_t tsLastNs = 0;
256 DBGFADDRESS AddrCur;
257
258 DBGFR3AddrSub(&AddrHit, sizeof(LNXPRINTKHDR));
259 AddrCur = AddrHit;
260
261 /* Try to find the end of the kernel log buffer. */
262 for (;;)
263 {
264 if (cbLogBuf >= LNX_MAX_KERNEL_LOG_SIZE)
265 break;
266
267 LNXPRINTKHDR Hdr;
268 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &AddrCur, &Hdr, sizeof(Hdr));
269 if (RT_SUCCESS(rc))
270 {
271 uint32_t const cbLogAlign = 4;
272
273 /*
274 * If the header does not look valid anymore we stop.
275 * Timestamps are monotonically increasing.
276 */
277 if ( !Hdr.cbTotal /* Zero entry size means there is no record anymore, doesn't make sense to look futher. */
278 || Hdr.cbText + Hdr.cbDict + sizeof(Hdr) > Hdr.cbTotal
279 || (Hdr.cbTotal & (cbLogAlign - 1)) != 0
280 || tsLastNs > Hdr.nsTimestamp)
281 break;
282
283 /** @todo Maybe read text part and verify it is all ASCII. */
284
285 cbLogBuf += Hdr.cbTotal;
286 DBGFR3AddrAdd(&AddrCur, Hdr.cbTotal);
287 }
288
289 if (RT_FAILURE(rc))
290 break;
291 }
292
293 /** @todo Go back to find the start address of the kernel log (or we loose potential kernel log messages). */
294
295 if ( RT_SUCCESS(rc)
296 && cbLogBuf)
297 {
298 /* Align log buffer size to a power of two. */
299 uint32_t idxBitLast = ASMBitLastSetU32(cbLogBuf);
300 idxBitLast--; /* There is at least one bit set, see check above. */
301
302 if (cbLogBuf & (RT_BIT_32(idxBitLast) - 1))
303 idxBitLast++;
304
305 *pGCPtrLogBuf = AddrHit.FlatPtr;
306 *pcbLogBuf = RT_MIN(RT_BIT_32(idxBitLast), LNX_MAX_KERNEL_LOG_SIZE);
307 }
308 else if (RT_SUCCESS(rc))
309 rc = VERR_NOT_FOUND;
310 }
311
312 return rc;
313}
314
315
316/**
317 * Converts a given offset into an absolute address if relative kernel offsets are used for
318 * kallsyms.
319 *
320 * @returns The absolute kernel address.
321 * @param pThis The Linux digger data.
322 * @param uOffset The offset to convert.
323 */
324DECLINLINE(RTGCUINTPTR) dbgDiggerLinuxConvOffsetToAddr(PDBGDIGGERLINUX pThis, int32_t uOffset)
325{
326 RTGCUINTPTR uAddr;
327
328 /*
329 * How the absolute address is calculated from the offset depends on the
330 * CONFIG_KALLSYMS_ABSOLUTE_PERCPU config which is only set for 64bit
331 * SMP kernels (we assume that all 64bit kernels always have SMP enabled too).
332 */
333 if (pThis->f64Bit)
334 {
335 if (uOffset >= 0)
336 uAddr = uOffset;
337 else
338 uAddr = pThis->uKernelRelativeBase - 1 - uOffset;
339 }
340 else
341 uAddr = pThis->uKernelRelativeBase + (uint32_t)uOffset;
342
343 return uAddr;
344}
345
346/**
347 * Disassembles a simple getter returning the value for it.
348 *
349 * @returns VBox status code.
350 * @param pThis The Linux digger data.
351 * @param pUVM The VM handle.
352 * @param hMod The module to use.
353 * @param pszSymbol The symbol of the getter.
354 * @param pvVal Where to store the value on success.
355 * @param cbVal Size of the value in bytes.
356 */
357static int dbgDiggerLinuxDisassembleSimpleGetter(PDBGDIGGERLINUX pThis, PUVM pUVM, RTDBGMOD hMod,
358 const char *pszSymbol, void *pvVal, uint32_t cbVal)
359{
360 int rc = VINF_SUCCESS;
361
362 RTDBGSYMBOL SymInfo;
363 rc = RTDbgModSymbolByName(hMod, pszSymbol, &SymInfo);
364 if (RT_SUCCESS(rc))
365 {
366 /*
367 * Do the diassembling. Disassemble until a ret instruction is encountered
368 * or a limit is reached (don't want to disassemble for too long as the getter
369 * should be short).
370 * push and pop instructions are skipped as well as any mov instructions not
371 * touching the rax or eax register (depending on the size of the value).
372 */
373 unsigned cInstrDisassembled = 0;
374 uint32_t offInstr = 0;
375 bool fRet = false;
376 DISSTATE DisState;
377 RT_ZERO(DisState);
378
379 do
380 {
381 DBGFADDRESS Addr;
382 RTGCPTR GCPtrCur = (RTGCPTR)SymInfo.Value + pThis->AddrKernelBase.FlatPtr + offInstr;
383 DBGFR3AddrFromFlat(pUVM, &Addr, GCPtrCur);
384
385 /* Prefetch the instruction. */
386 uint8_t abInstr[32];
387 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &Addr, &abInstr[0], sizeof(abInstr));
388 if (RT_SUCCESS(rc))
389 {
390 uint32_t cbInstr = 0;
391
392 rc = DISInstr(&abInstr[0], pThis->f64Bit ? DISCPUMODE_64BIT : DISCPUMODE_32BIT, &DisState, &cbInstr);
393 if (RT_SUCCESS(rc))
394 {
395 switch (DisState.pCurInstr->uOpcode)
396 {
397 case OP_PUSH:
398 case OP_POP:
399 case OP_NOP:
400 case OP_LEA:
401 break;
402 case OP_RETN:
403 /* Getter returned, abort disassembling. */
404 fRet = true;
405 break;
406 case OP_MOV:
407 /*
408 * Check that the destination is either rax or eax depending on the
409 * value size.
410 *
411 * Param1 is the destination and Param2 the source.
412 */
413 if ( ( ( (DisState.Param1.fUse & (DISUSE_BASE | DISUSE_REG_GEN32))
414 && cbVal == sizeof(uint32_t))
415 || ( (DisState.Param1.fUse & (DISUSE_BASE | DISUSE_REG_GEN64))
416 && cbVal == sizeof(uint64_t)))
417 && DisState.Param1.Base.idxGenReg == DISGREG_RAX)
418 {
419 /* Parse the source. */
420 if (DisState.Param2.fUse & (DISUSE_IMMEDIATE32 | DISUSE_IMMEDIATE64))
421 memcpy(pvVal, &DisState.Param2.uValue, cbVal);
422 else if (DisState.Param2.fUse & (DISUSE_RIPDISPLACEMENT32|DISUSE_DISPLACEMENT32|DISUSE_DISPLACEMENT64))
423 {
424 RTGCPTR GCPtrVal = 0;
425
426 if (DisState.Param2.fUse & DISUSE_RIPDISPLACEMENT32)
427 GCPtrVal = GCPtrCur + DisState.Param2.uDisp.i32 + cbInstr;
428 else if (DisState.Param2.fUse & DISUSE_DISPLACEMENT32)
429 GCPtrVal = (RTGCPTR)DisState.Param2.uDisp.u32;
430 else if (DisState.Param2.fUse & DISUSE_DISPLACEMENT64)
431 GCPtrVal = (RTGCPTR)DisState.Param2.uDisp.u64;
432 else
433 AssertMsgFailedBreakStmt(("Invalid displacement\n"), rc = VERR_INVALID_STATE);
434
435 DBGFADDRESS AddrVal;
436 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/,
437 DBGFR3AddrFromFlat(pUVM, &AddrVal, GCPtrVal),
438 pvVal, cbVal);
439 }
440 }
441 break;
442 default:
443 /* All other instructions will cause an error for now (playing safe here). */
444 rc = VERR_INVALID_PARAMETER;
445 break;
446 }
447 cInstrDisassembled++;
448 offInstr += cbInstr;
449 }
450 }
451 } while ( RT_SUCCESS(rc)
452 && cInstrDisassembled < 20
453 && !fRet);
454 }
455
456 return rc;
457}
458
459/**
460 * Try to get at the log buffer starting address and size by disassembling emit_log_char.
461 *
462 * @returns VBox status code.
463 * @param pThis The Linux digger data.
464 * @param pUVM The VM handle.
465 * @param hMod The module to use.
466 * @param pGCPtrLogBuf Where to store the log buffer pointer on success.
467 * @param pcbLogBuf Where to store the size of the log buffer on success.
468 */
469static int dbgDiggerLinuxQueryAsciiLogBufferPtrs(PDBGDIGGERLINUX pThis, PUVM pUVM, RTDBGMOD hMod,
470 RTGCPTR *pGCPtrLogBuf, uint32_t *pcbLogBuf)
471{
472 int rc = VINF_SUCCESS;
473
474 /**
475 * We disassemble emit_log_char to get at the log buffer address and size.
476 * This is used in case the symbols are not exported in kallsyms.
477 *
478 * This is what it typically looks like:
479 * vmlinux!emit_log_char:
480 * %00000000c01204a1 56 push esi
481 * %00000000c01204a2 8b 35 d0 1c 34 c0 mov esi, dword [0c0341cd0h]
482 * %00000000c01204a8 53 push ebx
483 * %00000000c01204a9 8b 1d 74 3b 3e c0 mov ebx, dword [0c03e3b74h]
484 * %00000000c01204af 8b 0d d8 1c 34 c0 mov ecx, dword [0c0341cd8h]
485 * %00000000c01204b5 8d 56 ff lea edx, [esi-001h]
486 * %00000000c01204b8 21 da and edx, ebx
487 * %00000000c01204ba 88 04 11 mov byte [ecx+edx], al
488 * %00000000c01204bd 8d 53 01 lea edx, [ebx+001h]
489 * %00000000c01204c0 89 d0 mov eax, edx
490 * [...]
491 */
492 RTDBGSYMBOL SymInfo;
493 rc = RTDbgModSymbolByName(hMod, "emit_log_char", &SymInfo);
494 if (RT_SUCCESS(rc))
495 {
496 /*
497 * Do the diassembling. Disassemble until a ret instruction is encountered
498 * or a limit is reached (don't want to disassemble for too long as the getter
499 * should be short). Certain instructions found are ignored (push, nop, etc.).
500 */
501 unsigned cInstrDisassembled = 0;
502 uint32_t offInstr = 0;
503 bool fRet = false;
504 DISSTATE DisState;
505 unsigned cAddressesUsed = 0;
506 struct { size_t cb; RTGCPTR GCPtrOrigSrc; } aAddresses[5];
507 RT_ZERO(DisState);
508 RT_ZERO(aAddresses);
509
510 do
511 {
512 DBGFADDRESS Addr;
513 RTGCPTR GCPtrCur = (RTGCPTR)SymInfo.Value + pThis->AddrKernelBase.FlatPtr + offInstr;
514 DBGFR3AddrFromFlat(pUVM, &Addr, GCPtrCur);
515
516 /* Prefetch the instruction. */
517 uint8_t abInstr[32];
518 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &Addr, &abInstr[0], sizeof(abInstr));
519 if (RT_SUCCESS(rc))
520 {
521 uint32_t cbInstr = 0;
522
523 rc = DISInstr(&abInstr[0], pThis->f64Bit ? DISCPUMODE_64BIT : DISCPUMODE_32BIT, &DisState, &cbInstr);
524 if (RT_SUCCESS(rc))
525 {
526 switch (DisState.pCurInstr->uOpcode)
527 {
528 case OP_PUSH:
529 case OP_POP:
530 case OP_NOP:
531 case OP_LEA:
532 case OP_AND:
533 case OP_CBW:
534 case OP_DEC:
535 break;
536 case OP_RETN:
537 /* emit_log_char returned, abort disassembling. */
538 rc = VERR_NOT_FOUND;
539 fRet = true;
540 break;
541 case OP_MOV:
542 case OP_MOVSXD:
543 /*
544 * If a mov is encountered writing to memory with al (or dil for amd64) being the source the
545 * character is stored and we can infer the base address and size of the log buffer from
546 * the source addresses.
547 */
548 if ( (DisState.Param2.fUse & DISUSE_REG_GEN8)
549 && ( (DisState.Param2.Base.idxGenReg == DISGREG_AL && !pThis->f64Bit)
550 || (DisState.Param2.Base.idxGenReg == DISGREG_DIL && pThis->f64Bit))
551 && DISUSE_IS_EFFECTIVE_ADDR(DisState.Param1.fUse))
552 {
553 RTGCPTR GCPtrLogBuf = 0;
554 uint32_t cbLogBuf = 0;
555
556 /*
557 * We can stop disassembling now and inspect all registers, look for a valid kernel address first.
558 * Only one of the accessed registers should hold a valid kernel address.
559 * For the log size look for the biggest non kernel address.
560 */
561 for (unsigned i = 0; i < cAddressesUsed; i++)
562 {
563 DBGFADDRESS AddrVal;
564 union { uint8_t abVal[8]; uint32_t u32Val; uint64_t u64Val; } Val;
565
566 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/,
567 DBGFR3AddrFromFlat(pUVM, &AddrVal, aAddresses[i].GCPtrOrigSrc),
568 &Val.abVal[0], aAddresses[i].cb);
569 if (RT_SUCCESS(rc))
570 {
571 if (pThis->f64Bit && aAddresses[i].cb == sizeof(uint64_t))
572 {
573 if (LNX64_VALID_ADDRESS(Val.u64Val))
574 {
575 if (GCPtrLogBuf == 0)
576 GCPtrLogBuf = Val.u64Val;
577 else
578 {
579 rc = VERR_NOT_FOUND;
580 break;
581 }
582 }
583 }
584 else
585 {
586 AssertMsgBreakStmt(aAddresses[i].cb == sizeof(uint32_t),
587 ("Invalid value size\n"), rc = VERR_INVALID_STATE);
588
589 /* Might be a kernel address or a size indicator. */
590 if (!pThis->f64Bit && LNX32_VALID_ADDRESS(Val.u32Val))
591 {
592 if (GCPtrLogBuf == 0)
593 GCPtrLogBuf = Val.u32Val;
594 else
595 {
596 rc = VERR_NOT_FOUND;
597 break;
598 }
599 }
600 else
601 {
602 /*
603 * The highest value will be the log buffer because the other
604 * accessed variables are indexes into the buffer and hence
605 * always smaller than the size.
606 */
607 if (cbLogBuf < Val.u32Val)
608 cbLogBuf = Val.u32Val;
609 }
610 }
611 }
612 }
613
614 if ( RT_SUCCESS(rc)
615 && GCPtrLogBuf != 0
616 && cbLogBuf != 0)
617 {
618 *pGCPtrLogBuf = GCPtrLogBuf;
619 *pcbLogBuf = cbLogBuf;
620 }
621 else if (RT_SUCCESS(rc))
622 rc = VERR_NOT_FOUND;
623
624 fRet = true;
625 break;
626 }
627 else
628 {
629 /*
630 * In case of a memory to register move store the destination register index and the
631 * source address in the relation table for later processing.
632 */
633 if ( (DisState.Param1.fUse & (DISUSE_BASE | DISUSE_REG_GEN32 | DISUSE_REG_GEN64))
634 && (DisState.Param2.cb == sizeof(uint32_t) || DisState.Param2.cb == sizeof(uint64_t))
635 && (DisState.Param2.fUse & (DISUSE_RIPDISPLACEMENT32|DISUSE_DISPLACEMENT32|DISUSE_DISPLACEMENT64)))
636 {
637 RTGCPTR GCPtrVal = 0;
638
639 if (DisState.Param2.fUse & DISUSE_RIPDISPLACEMENT32)
640 GCPtrVal = GCPtrCur + DisState.Param2.uDisp.i32 + cbInstr;
641 else if (DisState.Param2.fUse & DISUSE_DISPLACEMENT32)
642 GCPtrVal = (RTGCPTR)DisState.Param2.uDisp.u32;
643 else if (DisState.Param2.fUse & DISUSE_DISPLACEMENT64)
644 GCPtrVal = (RTGCPTR)DisState.Param2.uDisp.u64;
645 else
646 AssertMsgFailedBreakStmt(("Invalid displacement\n"), rc = VERR_INVALID_STATE);
647
648 if (cAddressesUsed < RT_ELEMENTS(aAddresses))
649 {
650 /* movsxd reads always 32bits. */
651 if (DisState.pCurInstr->uOpcode == OP_MOVSXD)
652 aAddresses[cAddressesUsed].cb = sizeof(uint32_t);
653 else
654 aAddresses[cAddressesUsed].cb = DisState.Param2.cb;
655 aAddresses[cAddressesUsed].GCPtrOrigSrc = GCPtrVal;
656 cAddressesUsed++;
657 }
658 else
659 {
660 rc = VERR_INVALID_PARAMETER;
661 break;
662 }
663 }
664 }
665 break;
666 default:
667 /* All other instructions will cause an error for now (playing safe here). */
668 rc = VERR_INVALID_PARAMETER;
669 break;
670 }
671 cInstrDisassembled++;
672 offInstr += cbInstr;
673 }
674 }
675 } while ( RT_SUCCESS(rc)
676 && cInstrDisassembled < 20
677 && !fRet);
678 }
679
680 return rc;
681}
682
683/**
684 * Try to get at the log buffer starting address and size by disassembling some exposed helpers.
685 *
686 * @returns VBox status code.
687 * @param pThis The Linux digger data.
688 * @param pUVM The VM handle.
689 * @param hMod The module to use.
690 * @param pGCPtrLogBuf Where to store the log buffer pointer on success.
691 * @param pcbLogBuf Where to store the size of the log buffer on success.
692 */
693static int dbgDiggerLinuxQueryLogBufferPtrs(PDBGDIGGERLINUX pThis, PUVM pUVM, RTDBGMOD hMod,
694 RTGCPTR *pGCPtrLogBuf, uint32_t *pcbLogBuf)
695{
696 int rc = VINF_SUCCESS;
697
698 struct { void *pvVar; uint32_t cbHost, cbGuest; const char *pszSymbol; } aSymbols[] =
699 {
700 { pGCPtrLogBuf, (uint32_t)sizeof(RTGCPTR), (uint32_t)(pThis->f64Bit ? sizeof(uint64_t) : sizeof(uint32_t)), "log_buf_addr_get" },
701 { pcbLogBuf, (uint32_t)sizeof(uint32_t), (uint32_t)sizeof(uint32_t), "log_buf_len_get" }
702 };
703 for (uint32_t i = 0; i < RT_ELEMENTS(aSymbols) && RT_SUCCESS(rc); i++)
704 {
705 RT_BZERO(aSymbols[i].pvVar, aSymbols[i].cbHost);
706 Assert(aSymbols[i].cbHost >= aSymbols[i].cbGuest);
707 rc = dbgDiggerLinuxDisassembleSimpleGetter(pThis, pUVM, hMod, aSymbols[i].pszSymbol,
708 aSymbols[i].pvVar, aSymbols[i].cbGuest);
709 }
710
711 return rc;
712}
713
714/**
715 * Returns whether the log buffer is a simple ascii buffer or a record based implementation
716 * based on the kernel version found.
717 *
718 * @returns Flag whether the log buffer is the simple ascii buffer.
719 * @param pThis The Linux digger data.
720 * @param pUVM The user mode VM handle.
721 */
722static bool dbgDiggerLinuxLogBufferIsAsciiBuffer(PDBGDIGGERLINUX pThis, PUVM pUVM)
723{
724 char szTmp[128];
725 char const *pszVer = &szTmp[sizeof(g_abLinuxVersion) - 1];
726
727 RT_ZERO(szTmp);
728 int rc = DBGFR3MemReadString(pUVM, 0, &pThis->AddrLinuxBanner, szTmp, sizeof(szTmp) - 1);
729 if ( RT_SUCCESS(rc)
730 && RTStrVersionCompare(pszVer, "3.4") == -1)
731 return true;
732
733 return false;
734}
735
736/**
737 * Worker to get at the kernel log for pre 3.4 kernels where the log buffer was just a char buffer.
738 *
739 * @returns VBox status code.
740 * @param pThis The Linux digger data.
741 * @param pUVM The VM user mdoe handle.
742 * @param hMod The debug module handle.
743 * @param fFlags Flags reserved for future use, MBZ.
744 * @param cMessages The number of messages to retrieve, counting from the
745 * end of the log (i.e. like tail), use UINT32_MAX for all.
746 * @param pszBuf The output buffer.
747 * @param cbBuf The buffer size.
748 * @param pcbActual Where to store the number of bytes actually returned,
749 * including zero terminator. On VERR_BUFFER_OVERFLOW this
750 * holds the necessary buffer size. Optional.
751 */
752static int dbgDiggerLinuxLogBufferQueryAscii(PDBGDIGGERLINUX pThis, PUVM pUVM, RTDBGMOD hMod,
753 uint32_t fFlags, uint32_t cMessages,
754 char *pszBuf, size_t cbBuf, size_t *pcbActual)
755{
756 RT_NOREF2(fFlags, cMessages);
757 int rc = VINF_SUCCESS;
758 RTGCPTR GCPtrLogBuf;
759 uint32_t cbLogBuf;
760
761 struct { void *pvVar; size_t cbHost, cbGuest; const char *pszSymbol; } aSymbols[] =
762 {
763 { &GCPtrLogBuf, sizeof(GCPtrLogBuf), pThis->f64Bit ? sizeof(uint64_t) : sizeof(uint32_t), "log_buf" },
764 { &cbLogBuf, sizeof(cbLogBuf), sizeof(cbLogBuf), "log_buf_len" },
765 };
766 for (uint32_t i = 0; i < RT_ELEMENTS(aSymbols); i++)
767 {
768 RTDBGSYMBOL SymInfo;
769 rc = RTDbgModSymbolByName(hMod, aSymbols[i].pszSymbol, &SymInfo);
770 if (RT_SUCCESS(rc))
771 {
772 RT_BZERO(aSymbols[i].pvVar, aSymbols[i].cbHost);
773 Assert(aSymbols[i].cbHost >= aSymbols[i].cbGuest);
774 DBGFADDRESS Addr;
775 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/,
776 DBGFR3AddrFromFlat(pUVM, &Addr, (RTGCPTR)SymInfo.Value + pThis->AddrKernelBase.FlatPtr),
777 aSymbols[i].pvVar, aSymbols[i].cbGuest);
778 if (RT_SUCCESS(rc))
779 continue;
780 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: Reading '%s' at %RGv: %Rrc\n", aSymbols[i].pszSymbol, Addr.FlatPtr, rc));
781 }
782 else
783 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: Error looking up '%s': %Rrc\n", aSymbols[i].pszSymbol, rc));
784 rc = VERR_NOT_FOUND;
785 break;
786 }
787
788 /*
789 * Some kernels don't expose the variables in kallsyms so we have to try disassemble
790 * some public helpers to get at the addresses.
791 *
792 * @todo: Maybe cache those values so we don't have to do the heavy work every time?
793 */
794 if (rc == VERR_NOT_FOUND)
795 {
796 rc = dbgDiggerLinuxQueryAsciiLogBufferPtrs(pThis, pUVM, hMod, &GCPtrLogBuf, &cbLogBuf);
797 if (RT_FAILURE(rc))
798 return rc;
799 }
800
801 /*
802 * Check if the values make sense.
803 */
804 if (pThis->f64Bit ? !LNX64_VALID_ADDRESS(GCPtrLogBuf) : !LNX32_VALID_ADDRESS(GCPtrLogBuf))
805 {
806 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: 'log_buf' value %RGv is not valid.\n", GCPtrLogBuf));
807 return VERR_NOT_FOUND;
808 }
809 if ( cbLogBuf < 4096
810 || !RT_IS_POWER_OF_TWO(cbLogBuf)
811 || cbLogBuf > 16*_1M)
812 {
813 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: 'log_buf_len' value %#x is not valid.\n", cbLogBuf));
814 return VERR_NOT_FOUND;
815 }
816
817 /*
818 * Read the whole log buffer.
819 */
820 uint8_t *pbLogBuf = (uint8_t *)RTMemAlloc(cbLogBuf);
821 if (!pbLogBuf)
822 {
823 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: Failed to allocate %#x bytes for log buffer\n", cbLogBuf));
824 return VERR_NO_MEMORY;
825 }
826 DBGFADDRESS Addr;
827 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, DBGFR3AddrFromFlat(pUVM, &Addr, GCPtrLogBuf), pbLogBuf, cbLogBuf);
828 if (RT_FAILURE(rc))
829 {
830 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: Error reading %#x bytes of log buffer at %RGv: %Rrc\n",
831 cbLogBuf, Addr.FlatPtr, rc));
832 RTMemFree(pbLogBuf);
833 return VERR_NOT_FOUND;
834 }
835
836 /** @todo Try to parse where the single messages start to make use of cMessages. */
837 size_t cchLength = RTStrNLen((const char *)pbLogBuf, cbLogBuf);
838 memcpy(&pszBuf[0], pbLogBuf, RT_MIN(cbBuf, cchLength));
839
840 /* Done with the buffer. */
841 RTMemFree(pbLogBuf);
842
843 /* Set return size value. */
844 if (pcbActual)
845 *pcbActual = RT_MIN(cbBuf, cchLength);
846
847 return cbBuf <= cchLength ? VERR_BUFFER_OVERFLOW : VINF_SUCCESS;
848}
849
850
851/**
852 * Worker to process a given record based kernel log.
853 *
854 * @returns VBox status code.
855 * @param pThis The Linux digger data.
856 * @param pUVM The VM user mode handle.
857 * @param GCPtrLogBuf Flat guest address of the start of the log buffer.
858 * @param cbLogBuf Power of two aligned size of the log buffer.
859 * @param idxFirst Index in the log bfufer of the first message.
860 * @param idxNext Index where to write hte next message in the log buffer.
861 * @param fFlags Flags reserved for future use, MBZ.
862 * @param cMessages The number of messages to retrieve, counting from the
863 * end of the log (i.e. like tail), use UINT32_MAX for all.
864 * @param pszBuf The output buffer.
865 * @param cbBuf The buffer size.
866 * @param pcbActual Where to store the number of bytes actually returned,
867 * including zero terminator. On VERR_BUFFER_OVERFLOW this
868 * holds the necessary buffer size. Optional.
869 */
870static int dbgDiggerLinuxKrnLogBufferProcess(PDBGDIGGERLINUX pThis, PUVM pUVM, RTGCPTR GCPtrLogBuf,
871 uint32_t cbLogBuf, uint32_t idxFirst, uint32_t idxNext,
872 uint32_t fFlags, uint32_t cMessages, char *pszBuf, size_t cbBuf,
873 size_t *pcbActual)
874{
875 RT_NOREF(fFlags);
876
877 /*
878 * Check if the values make sense.
879 */
880 if (pThis->f64Bit ? !LNX64_VALID_ADDRESS(GCPtrLogBuf) : !LNX32_VALID_ADDRESS(GCPtrLogBuf))
881 {
882 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: 'log_buf' value %RGv is not valid.\n", GCPtrLogBuf));
883 return VERR_NOT_FOUND;
884 }
885 if ( cbLogBuf < _4K
886 || !RT_IS_POWER_OF_TWO(cbLogBuf)
887 || cbLogBuf > LNX_MAX_KERNEL_LOG_SIZE)
888 {
889 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: 'log_buf_len' value %#x is not valid.\n", cbLogBuf));
890 return VERR_NOT_FOUND;
891 }
892 uint32_t const cbLogAlign = 4;
893 if ( idxFirst > cbLogBuf - sizeof(LNXPRINTKHDR)
894 || (idxFirst & (cbLogAlign - 1)) != 0)
895 {
896 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: 'log_first_idx' value %#x is not valid.\n", idxFirst));
897 return VERR_NOT_FOUND;
898 }
899 if ( idxNext > cbLogBuf - sizeof(LNXPRINTKHDR)
900 || (idxNext & (cbLogAlign - 1)) != 0)
901 {
902 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: 'log_next_idx' value %#x is not valid.\n", idxNext));
903 return VERR_NOT_FOUND;
904 }
905
906 /*
907 * Read the whole log buffer.
908 */
909 uint8_t *pbLogBuf = (uint8_t *)RTMemAlloc(cbLogBuf);
910 if (!pbLogBuf)
911 {
912 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: Failed to allocate %#x bytes for log buffer\n", cbLogBuf));
913 return VERR_NO_MEMORY;
914 }
915 DBGFADDRESS Addr;
916 int rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, DBGFR3AddrFromFlat(pUVM, &Addr, GCPtrLogBuf), pbLogBuf, cbLogBuf);
917 if (RT_FAILURE(rc))
918 {
919 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: Error reading %#x bytes of log buffer at %RGv: %Rrc\n",
920 cbLogBuf, Addr.FlatPtr, rc));
921 RTMemFree(pbLogBuf);
922 return VERR_NOT_FOUND;
923 }
924
925 /*
926 * Count the messages in the buffer while doing some basic validation.
927 */
928 uint32_t const cbUsed = idxFirst == idxNext ? cbLogBuf /* could be empty... */
929 : idxFirst < idxNext ? idxNext - idxFirst : cbLogBuf - idxFirst + idxNext;
930 uint32_t cbLeft = cbUsed;
931 uint32_t offCur = idxFirst;
932 uint32_t cLogMsgs = 0;
933
934 while (cbLeft > 0)
935 {
936 PCLNXPRINTKHDR pHdr = (PCLNXPRINTKHDR)&pbLogBuf[offCur];
937 if (!pHdr->cbTotal)
938 {
939 /* Wrap around packet, most likely... */
940 if (cbLogBuf - offCur >= cbLeft)
941 break;
942 offCur = 0;
943 pHdr = (PCLNXPRINTKHDR)&pbLogBuf[offCur];
944 }
945 if (RT_UNLIKELY( pHdr->cbTotal > cbLogBuf - sizeof(*pHdr) - offCur
946 || pHdr->cbTotal > cbLeft
947 || (pHdr->cbTotal & (cbLogAlign - 1)) != 0
948 || pHdr->cbTotal < (uint32_t)pHdr->cbText + (uint32_t)pHdr->cbDict + sizeof(*pHdr) ))
949 {
950 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: Invalid printk_log record at %#x: cbTotal=%#x cbText=%#x cbDict=%#x cbLogBuf=%#x cbLeft=%#x\n",
951 offCur, pHdr->cbTotal, pHdr->cbText, pHdr->cbDict, cbLogBuf, cbLeft));
952 break;
953 }
954
955 if (pHdr->cbText > 0)
956 cLogMsgs++;
957
958 /* next */
959 offCur += pHdr->cbTotal;
960 cbLeft -= pHdr->cbTotal;
961 }
962 if (!cLogMsgs)
963 {
964 RTMemFree(pbLogBuf);
965 return VERR_NOT_FOUND;
966 }
967
968 /*
969 * Copy the messages into the output buffer.
970 */
971 offCur = idxFirst;
972 cbLeft = cbUsed - cbLeft;
973
974 /* Skip messages that the caller doesn't want. */
975 if (cMessages < cLogMsgs)
976 {
977 uint32_t cToSkip = cLogMsgs - cMessages;
978 cLogMsgs -= cToSkip;
979
980 while (cToSkip > 0)
981 {
982 PCLNXPRINTKHDR pHdr = (PCLNXPRINTKHDR)&pbLogBuf[offCur];
983 if (!pHdr->cbTotal)
984 {
985 offCur = 0;
986 pHdr = (PCLNXPRINTKHDR)&pbLogBuf[offCur];
987 }
988 if (pHdr->cbText > 0)
989 cToSkip--;
990
991 /* next */
992 offCur += pHdr->cbTotal;
993 cbLeft -= pHdr->cbTotal;
994 }
995 }
996
997 /* Now copy the messages. */
998 size_t offDst = 0;
999 while (cbLeft > 0)
1000 {
1001 PCLNXPRINTKHDR pHdr = (PCLNXPRINTKHDR)&pbLogBuf[offCur];
1002 if ( !pHdr->cbTotal
1003 || !cLogMsgs)
1004 {
1005 if (cbLogBuf - offCur >= cbLeft)
1006 break;
1007 offCur = 0;
1008 pHdr = (PCLNXPRINTKHDR)&pbLogBuf[offCur];
1009 }
1010
1011 if (pHdr->cbText > 0)
1012 {
1013 char *pchText = (char *)(pHdr + 1);
1014 size_t cchText = RTStrNLen(pchText, pHdr->cbText);
1015 if (offDst + cchText < cbBuf)
1016 {
1017 memcpy(&pszBuf[offDst], pHdr + 1, cchText);
1018 pszBuf[offDst + cchText] = '\n';
1019 }
1020 else if (offDst < cbBuf)
1021 memcpy(&pszBuf[offDst], pHdr + 1, cbBuf - offDst);
1022 offDst += cchText + 1;
1023 }
1024
1025 /* next */
1026 offCur += pHdr->cbTotal;
1027 cbLeft -= pHdr->cbTotal;
1028 }
1029
1030 /* Done with the buffer. */
1031 RTMemFree(pbLogBuf);
1032
1033 /* Make sure we've reserved a char for the terminator. */
1034 if (!offDst)
1035 offDst = 1;
1036
1037 /* Set return size value. */
1038 if (pcbActual)
1039 *pcbActual = offDst;
1040
1041 if (offDst <= cbBuf)
1042 return VINF_SUCCESS;
1043 return VERR_BUFFER_OVERFLOW;
1044}
1045
1046
1047/**
1048 * Worker to get at the kernel log for post 3.4 kernels where the log buffer contains records.
1049 *
1050 * @returns VBox status code.
1051 * @param pThis The Linux digger data.
1052 * @param pUVM The VM user mdoe handle.
1053 * @param hMod The debug module handle.
1054 * @param fFlags Flags reserved for future use, MBZ.
1055 * @param cMessages The number of messages to retrieve, counting from the
1056 * end of the log (i.e. like tail), use UINT32_MAX for all.
1057 * @param pszBuf The output buffer.
1058 * @param cbBuf The buffer size.
1059 * @param pcbActual Where to store the number of bytes actually returned,
1060 * including zero terminator. On VERR_BUFFER_OVERFLOW this
1061 * holds the necessary buffer size. Optional.
1062 */
1063static int dbgDiggerLinuxLogBufferQueryRecords(PDBGDIGGERLINUX pThis, PUVM pUVM, RTDBGMOD hMod,
1064 uint32_t fFlags, uint32_t cMessages,
1065 char *pszBuf, size_t cbBuf, size_t *pcbActual)
1066{
1067 int rc = VINF_SUCCESS;
1068 RTGCPTR GCPtrLogBuf;
1069 uint32_t cbLogBuf;
1070 uint32_t idxFirst;
1071 uint32_t idxNext;
1072
1073 struct { void *pvVar; size_t cbHost, cbGuest; const char *pszSymbol; } aSymbols[] =
1074 {
1075 { &GCPtrLogBuf, sizeof(GCPtrLogBuf), pThis->f64Bit ? sizeof(uint64_t) : sizeof(uint32_t), "log_buf" },
1076 { &cbLogBuf, sizeof(cbLogBuf), sizeof(cbLogBuf), "log_buf_len" },
1077 { &idxFirst, sizeof(idxFirst), sizeof(idxFirst), "log_first_idx" },
1078 { &idxNext, sizeof(idxNext), sizeof(idxNext), "log_next_idx" },
1079 };
1080 for (uint32_t i = 0; i < RT_ELEMENTS(aSymbols); i++)
1081 {
1082 RTDBGSYMBOL SymInfo;
1083 rc = RTDbgModSymbolByName(hMod, aSymbols[i].pszSymbol, &SymInfo);
1084 if (RT_SUCCESS(rc))
1085 {
1086 RT_BZERO(aSymbols[i].pvVar, aSymbols[i].cbHost);
1087 Assert(aSymbols[i].cbHost >= aSymbols[i].cbGuest);
1088 DBGFADDRESS Addr;
1089 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/,
1090 DBGFR3AddrFromFlat(pUVM, &Addr, (RTGCPTR)SymInfo.Value + pThis->AddrKernelBase.FlatPtr),
1091 aSymbols[i].pvVar, aSymbols[i].cbGuest);
1092 if (RT_SUCCESS(rc))
1093 continue;
1094 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: Reading '%s' at %RGv: %Rrc\n", aSymbols[i].pszSymbol, Addr.FlatPtr, rc));
1095 }
1096 else
1097 LogRel(("dbgDiggerLinuxIDmsg_QueryKernelLog: Error looking up '%s': %Rrc\n", aSymbols[i].pszSymbol, rc));
1098 rc = VERR_NOT_FOUND;
1099 break;
1100 }
1101
1102 /*
1103 * Some kernels don't expose the variables in kallsyms so we have to try disassemble
1104 * some public helpers to get at the addresses.
1105 *
1106 * @todo: Maybe cache those values so we don't have to do the heavy work every time?
1107 */
1108 if (rc == VERR_NOT_FOUND)
1109 {
1110 idxFirst = 0;
1111 idxNext = 0;
1112 rc = dbgDiggerLinuxQueryLogBufferPtrs(pThis, pUVM, hMod, &GCPtrLogBuf, &cbLogBuf);
1113 if (RT_FAILURE(rc))
1114 {
1115 /*
1116 * Last resort, scan for a known value which should appear only once in the kernel log buffer
1117 * and try to deduce the boundaries from there.
1118 */
1119 rc = dbgDiggerLinuxKrnlLogBufFindByNeedle(pThis, pUVM, &GCPtrLogBuf, &cbLogBuf);
1120 return rc;
1121 }
1122 }
1123
1124 return dbgDiggerLinuxKrnLogBufferProcess(pThis, pUVM, GCPtrLogBuf, cbLogBuf, idxFirst, idxNext,
1125 fFlags, cMessages, pszBuf, cbBuf, pcbActual);
1126}
1127
1128/**
1129 * @interface_method_impl{DBGFOSIDMESG,pfnQueryKernelLog}
1130 */
1131static DECLCALLBACK(int) dbgDiggerLinuxIDmsg_QueryKernelLog(PDBGFOSIDMESG pThis, PUVM pUVM, uint32_t fFlags, uint32_t cMessages,
1132 char *pszBuf, size_t cbBuf, size_t *pcbActual)
1133{
1134 PDBGDIGGERLINUX pData = RT_FROM_MEMBER(pThis, DBGDIGGERLINUX, IDmesg);
1135
1136 if (cMessages < 1)
1137 return VERR_INVALID_PARAMETER;
1138
1139 /*
1140 * Resolve the symbols we need and read their values.
1141 */
1142 RTDBGAS hAs = DBGFR3AsResolveAndRetain(pUVM, DBGF_AS_KERNEL);
1143 RTDBGMOD hMod;
1144 int rc = RTDbgAsModuleByName(hAs, "vmlinux", 0, &hMod);
1145 RTDbgAsRelease(hAs);
1146
1147 size_t cbActual = 0;
1148 if (RT_SUCCESS(rc))
1149 {
1150 /*
1151 * Check whether the kernel log buffer is a simple char buffer or the newer
1152 * record based implementation.
1153 * The record based implementation was presumably introduced with kernel 3.4,
1154 * see: http://thread.gmane.org/gmane.linux.kernel/1284184
1155 */
1156 if (dbgDiggerLinuxLogBufferIsAsciiBuffer(pData, pUVM))
1157 rc = dbgDiggerLinuxLogBufferQueryAscii(pData, pUVM, hMod, fFlags, cMessages, pszBuf, cbBuf, &cbActual);
1158 else
1159 rc = dbgDiggerLinuxLogBufferQueryRecords(pData, pUVM, hMod, fFlags, cMessages, pszBuf, cbBuf, &cbActual);
1160
1161 /* Release the module in any case. */
1162 RTDbgModRelease(hMod);
1163 }
1164 else
1165 {
1166 /*
1167 * For the record based kernel versions we have a last resort heuristic which doesn't
1168 * require any symbols, try that here.
1169 */
1170 if (!dbgDiggerLinuxLogBufferIsAsciiBuffer(pData, pUVM))
1171 {
1172 RTGCPTR GCPtrLogBuf = 0;
1173 uint32_t cbLogBuf = 0;
1174
1175 rc = dbgDiggerLinuxKrnlLogBufFindByNeedle(pData, pUVM, &GCPtrLogBuf, &cbLogBuf);
1176 if (RT_SUCCESS(rc))
1177 rc = dbgDiggerLinuxKrnLogBufferProcess(pData, pUVM, GCPtrLogBuf, cbLogBuf, 0 /*idxFirst*/, 0 /*idxNext*/,
1178 fFlags, cMessages, pszBuf, cbBuf, &cbActual);
1179 }
1180 else
1181 rc = VERR_NOT_FOUND;
1182 }
1183
1184 if (RT_FAILURE(rc) && rc != VERR_BUFFER_OVERFLOW)
1185 return rc;
1186
1187 if (pcbActual)
1188 *pcbActual = cbActual;
1189
1190 /*
1191 * All VBox strings are UTF-8 and bad things may in theory happen if we
1192 * pass bad UTF-8 to code which assumes it's all valid. So, we enforce
1193 * UTF-8 upon the guest kernel messages here even if they (probably) have
1194 * no defined code set in reality.
1195 */
1196 if ( RT_SUCCESS(rc)
1197 && cbActual <= cbBuf)
1198 {
1199 pszBuf[cbActual - 1] = '\0';
1200 RTStrPurgeEncoding(pszBuf);
1201 return VINF_SUCCESS;
1202 }
1203
1204 if (cbBuf)
1205 {
1206 pszBuf[cbBuf - 1] = '\0';
1207 RTStrPurgeEncoding(pszBuf);
1208 }
1209 return VERR_BUFFER_OVERFLOW;
1210}
1211
1212
1213/**
1214 * Worker destroying the config database.
1215 */
1216static DECLCALLBACK(int) dbgDiggerLinuxCfgDbDestroyWorker(PRTSTRSPACECORE pStr, void *pvUser)
1217{
1218 PDBGDIGGERLINUXCFGITEM pCfgItem = (PDBGDIGGERLINUXCFGITEM)pStr;
1219 RTStrFree((char *)pCfgItem->Core.pszString);
1220 RTMemFree(pCfgItem);
1221 NOREF(pvUser);
1222 return 0;
1223}
1224
1225
1226/**
1227 * Destroy the config database.
1228 *
1229 * @returns nothing.
1230 * @param pThis The Linux digger data.
1231 */
1232static void dbgDiggerLinuxCfgDbDestroy(PDBGDIGGERLINUX pThis)
1233{
1234 RTStrSpaceDestroy(&pThis->hCfgDb, dbgDiggerLinuxCfgDbDestroyWorker, NULL);
1235}
1236
1237
1238/**
1239 * @copydoc DBGFOSREG::pfnStackUnwindAssist
1240 */
1241static DECLCALLBACK(int) dbgDiggerLinuxStackUnwindAssist(PUVM pUVM, void *pvData, VMCPUID idCpu, PDBGFSTACKFRAME pFrame,
1242 PRTDBGUNWINDSTATE pState, PCCPUMCTX pInitialCtx, RTDBGAS hAs,
1243 uint64_t *puScratch)
1244{
1245 RT_NOREF(pUVM, pvData, idCpu, pFrame, pState, pInitialCtx, hAs, puScratch);
1246 return VINF_SUCCESS;
1247}
1248
1249
1250/**
1251 * @copydoc DBGFOSREG::pfnQueryInterface
1252 */
1253static DECLCALLBACK(void *) dbgDiggerLinuxQueryInterface(PUVM pUVM, void *pvData, DBGFOSINTERFACE enmIf)
1254{
1255 RT_NOREF1(pUVM);
1256 PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
1257 switch (enmIf)
1258 {
1259 case DBGFOSINTERFACE_DMESG:
1260 return &pThis->IDmesg;
1261
1262 default:
1263 return NULL;
1264 }
1265}
1266
1267
1268/**
1269 * @copydoc DBGFOSREG::pfnQueryVersion
1270 */
1271static DECLCALLBACK(int) dbgDiggerLinuxQueryVersion(PUVM pUVM, void *pvData, char *pszVersion, size_t cchVersion)
1272{
1273 PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
1274 Assert(pThis->fValid);
1275
1276 /*
1277 * It's all in the linux banner.
1278 */
1279 int rc = DBGFR3MemReadString(pUVM, 0, &pThis->AddrLinuxBanner, pszVersion, cchVersion);
1280 if (RT_SUCCESS(rc))
1281 {
1282 char *pszEnd = RTStrEnd(pszVersion, cchVersion);
1283 AssertReturn(pszEnd, VERR_BUFFER_OVERFLOW);
1284 while ( pszEnd > pszVersion
1285 && RT_C_IS_SPACE(pszEnd[-1]))
1286 pszEnd--;
1287 *pszEnd = '\0';
1288 }
1289 else
1290 RTStrPrintf(pszVersion, cchVersion, "DBGFR3MemRead -> %Rrc", rc);
1291
1292 return rc;
1293}
1294
1295
1296/**
1297 * @copydoc DBGFOSREG::pfnTerm
1298 */
1299static DECLCALLBACK(void) dbgDiggerLinuxTerm(PUVM pUVM, void *pvData)
1300{
1301 PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
1302 Assert(pThis->fValid);
1303
1304 /*
1305 * Destroy configuration database.
1306 */
1307 dbgDiggerLinuxCfgDbDestroy(pThis);
1308
1309 /*
1310 * Unlink and release our modules.
1311 */
1312 RTDBGAS hDbgAs = DBGFR3AsResolveAndRetain(pUVM, DBGF_AS_KERNEL);
1313 if (hDbgAs != NIL_RTDBGAS)
1314 {
1315 uint32_t iMod = RTDbgAsModuleCount(hDbgAs);
1316 while (iMod-- > 0)
1317 {
1318 RTDBGMOD hMod = RTDbgAsModuleByIndex(hDbgAs, iMod);
1319 if (hMod != NIL_RTDBGMOD)
1320 {
1321 if (RTDbgModGetTag(hMod) == DIG_LNX_MOD_TAG)
1322 {
1323 int rc = RTDbgAsModuleUnlink(hDbgAs, hMod);
1324 AssertRC(rc);
1325 }
1326 RTDbgModRelease(hMod);
1327 }
1328 }
1329 RTDbgAsRelease(hDbgAs);
1330 }
1331
1332 pThis->fValid = false;
1333}
1334
1335
1336/**
1337 * @copydoc DBGFOSREG::pfnRefresh
1338 */
1339static DECLCALLBACK(int) dbgDiggerLinuxRefresh(PUVM pUVM, void *pvData)
1340{
1341 PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
1342 NOREF(pThis);
1343 Assert(pThis->fValid);
1344
1345 /*
1346 * For now we'll flush and reload everything.
1347 */
1348 dbgDiggerLinuxTerm(pUVM, pvData);
1349 return dbgDiggerLinuxInit(pUVM, pvData);
1350}
1351
1352
1353/**
1354 * Worker for dbgDiggerLinuxFindStartOfNamesAndSymbolCount that update the
1355 * digger data.
1356 *
1357 * @returns VINF_SUCCESS.
1358 * @param pThis The Linux digger data to update.
1359 * @param pAddrKernelNames The kallsyms_names address.
1360 * @param cKernelSymbols The number of kernel symbol.
1361 * @param cbAddress The guest address size.
1362 */
1363static int dbgDiggerLinuxFoundStartOfNames(PDBGDIGGERLINUX pThis, PCDBGFADDRESS pAddrKernelNames,
1364 uint32_t cKernelSymbols, uint32_t cbAddress)
1365{
1366 pThis->cKernelSymbols = cKernelSymbols;
1367 pThis->AddrKernelNames = *pAddrKernelNames;
1368 pThis->AddrKernelAddresses = *pAddrKernelNames;
1369 uint32_t cbSymbolsSkip = (pThis->fRelKrnlAddr ? 2 : 1) * cbAddress; /* Relative addressing introduces kallsyms_relative_base. */
1370 uint32_t cbOffsets = pThis->fRelKrnlAddr ? sizeof(int32_t) : cbAddress; /* Offsets are always 32bits wide for relative addressing. */
1371 uint32_t cbAlign = 0;
1372
1373 /*
1374 * If the number of symbols is odd there is padding to align the following guest pointer
1375 * sized data properly on 64bit systems with relative addressing.
1376 */
1377 if ( pThis->fRelKrnlAddr
1378 && pThis->f64Bit
1379 && (pThis->cKernelSymbols & 1))
1380 cbAlign = sizeof(int32_t);
1381 DBGFR3AddrSub(&pThis->AddrKernelAddresses, cKernelSymbols * cbOffsets + cbSymbolsSkip + cbAlign);
1382
1383 Log(("dbgDiggerLinuxFoundStartOfNames: AddrKernelAddresses=%RGv\n"
1384 "dbgDiggerLinuxFoundStartOfNames: cKernelSymbols=%#x (at %RGv)\n"
1385 "dbgDiggerLinuxFoundStartOfNames: AddrKernelName=%RGv\n",
1386 pThis->AddrKernelAddresses.FlatPtr,
1387 pThis->cKernelSymbols, pThis->AddrKernelNames.FlatPtr - cbAddress,
1388 pThis->AddrKernelNames.FlatPtr));
1389 return VINF_SUCCESS;
1390}
1391
1392
1393/**
1394 * Tries to find the address of the kallsyms_names, kallsyms_num_syms and
1395 * kallsyms_addresses symbols.
1396 *
1397 * The kallsyms_num_syms is read and stored in pThis->cKernelSymbols, while the
1398 * addresses of the other two are stored as pThis->AddrKernelNames and
1399 * pThis->AddrKernelAddresses.
1400 *
1401 * @returns VBox status code, success indicating that all three variables have
1402 * been found and taken down.
1403 * @param pUVM The user mode VM handle.
1404 * @param pThis The Linux digger data.
1405 * @param pHitAddr An address we think is inside kallsyms_names.
1406 */
1407static int dbgDiggerLinuxFindStartOfNamesAndSymbolCount(PUVM pUVM, PDBGDIGGERLINUX pThis, PCDBGFADDRESS pHitAddr)
1408{
1409 /*
1410 * Search backwards in chunks.
1411 */
1412 union
1413 {
1414 uint8_t ab[0x1000];
1415 uint32_t au32[0x1000 / sizeof(uint32_t)];
1416 uint64_t au64[0x1000 / sizeof(uint64_t)];
1417 } uBuf;
1418 uint32_t cbLeft = LNX_MAX_KALLSYMS_NAMES_SIZE;
1419 uint32_t cbBuf = pHitAddr->FlatPtr & (sizeof(uBuf) - 1);
1420 DBGFADDRESS CurAddr = *pHitAddr;
1421 DBGFR3AddrSub(&CurAddr, cbBuf);
1422 cbBuf += sizeof(uint64_t) - 1; /* In case our kobj hit is in the first 4/8 bytes. */
1423 for (;;)
1424 {
1425 int rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &CurAddr, &uBuf, sizeof(uBuf));
1426 if (RT_FAILURE(rc))
1427 return rc;
1428
1429 /*
1430 * Since Linux 4.6 there are two different methods to store the kallsyms addresses
1431 * in the image.
1432 *
1433 * The first and longer existing method is to store the absolute addresses in an
1434 * array starting at kallsyms_addresses followed by a field which stores the number
1435 * of kernel symbols called kallsyms_num_syms.
1436 * The newer method is to use offsets stored in kallsyms_offsets and have a base pointer
1437 * to relate the offsets to called kallsyms_relative_base. One entry in kallsyms_offsets is
1438 * always 32bit wide regardless of the guest pointer size (this halves the table on 64bit
1439 * systems) but means more work for us for the 64bit case.
1440 *
1441 * When absolute addresses are used the following assumptions hold:
1442 *
1443 * We assume that the three symbols are aligned on guest pointer boundary.
1444 *
1445 * The boundary between the two tables should be noticable as the number
1446 * is unlikely to be more than 16 millions, there will be at least one zero
1447 * byte where it is, 64-bit will have 5 zero bytes. Zero bytes aren't all
1448 * that common in the kallsyms_names table.
1449 *
1450 * Also the kallsyms_names table starts with a length byte, which means
1451 * we're likely to see a byte in the range 1..31.
1452 *
1453 * The kallsyms_addresses are mostly sorted (except for the start where the
1454 * absolute symbols are), so we'll spot a bunch of kernel addresses
1455 * immediately preceeding the kallsyms_num_syms field.
1456 *
1457 * Lazy bird: If kallsyms_num_syms is on a buffer boundrary, we skip
1458 * the check for kernel addresses preceeding it.
1459 *
1460 * For relative offsets most of the assumptions from above are true too
1461 * except that we have to distinguish between the relative base address and the offsets.
1462 * Every observed kernel has a valid kernel address fo the relative base and kallsyms_relative_base
1463 * always comes before kallsyms_num_syms and is aligned on a guest pointer boundary.
1464 * Offsets are stored before kallsyms_relative_base and don't contain valid kernel addresses.
1465 *
1466 * To distinguish between absolute and relative offsetting we check the data before a candidate
1467 * for kallsyms_num_syms. If all entries before the kallsyms_num_syms candidate are valid kernel
1468 * addresses absolute addresses are assumed. If this is not the case but the first entry before
1469 * kallsyms_num_syms is a valid kernel address we check whether the data before and the possible
1470 * relative base form a valid kernel address and assume relative offsets.
1471 */
1472 if (pThis->f64Bit)
1473 {
1474 uint32_t i = cbBuf / sizeof(uint64_t) - 1;
1475 while (i-- > 0)
1476 if ( uBuf.au64[i] <= LNX_MAX_KALLSYMS_SYMBOLS
1477 && uBuf.au64[i] >= LNX_MIN_KALLSYMS_SYMBOLS)
1478 {
1479 uint8_t *pb = (uint8_t *)&uBuf.au64[i + 1];
1480 if ( pb[0] <= LNX_MAX_KALLSYMS_ENC_LENGTH
1481 && pb[0] >= LNX_MIN_KALLSYMS_ENC_LENGTH)
1482 {
1483 /*
1484 * Check whether we have a valid kernel address and try to distinguish
1485 * whether the kernel uses relative offsetting or absolute addresses.
1486 */
1487 if ( (i >= 1 && LNX64_VALID_ADDRESS(uBuf.au64[i - 1]))
1488 && (i >= 2 && !LNX64_VALID_ADDRESS(uBuf.au64[i - 2]))
1489 && (i >= 3 && !LNX64_VALID_ADDRESS(uBuf.au64[i - 3])))
1490 {
1491 RTGCUINTPTR uKrnlRelBase = uBuf.au64[i - 1];
1492 DBGFADDRESS RelAddr = CurAddr;
1493 int32_t aiRelOff[3];
1494 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, DBGFR3AddrAdd(&RelAddr, (i - 1) * sizeof(uint64_t) - sizeof(aiRelOff)),
1495 &aiRelOff[0], sizeof(aiRelOff));
1496 if ( RT_SUCCESS(rc)
1497 && LNX64_VALID_ADDRESS(uKrnlRelBase + aiRelOff[0])
1498 && LNX64_VALID_ADDRESS(uKrnlRelBase + aiRelOff[1])
1499 && LNX64_VALID_ADDRESS(uKrnlRelBase + aiRelOff[2]))
1500 {
1501 Log(("dbgDiggerLinuxFindStartOfNamesAndSymbolCount: relative base %RGv (at %RGv)\n",
1502 uKrnlRelBase, CurAddr.FlatPtr + (i - 1) * sizeof(uint64_t)));
1503 pThis->fRelKrnlAddr = true;
1504 pThis->uKernelRelativeBase = uKrnlRelBase;
1505 return dbgDiggerLinuxFoundStartOfNames(pThis,
1506 DBGFR3AddrAdd(&CurAddr, (i + 1) * sizeof(uint64_t)),
1507 (uint32_t)uBuf.au64[i], sizeof(uint64_t));
1508 }
1509 }
1510
1511 if ( (i <= 0 || LNX64_VALID_ADDRESS(uBuf.au64[i - 1]))
1512 && (i <= 1 || LNX64_VALID_ADDRESS(uBuf.au64[i - 2]))
1513 && (i <= 2 || LNX64_VALID_ADDRESS(uBuf.au64[i - 3])))
1514 return dbgDiggerLinuxFoundStartOfNames(pThis,
1515 DBGFR3AddrAdd(&CurAddr, (i + 1) * sizeof(uint64_t)),
1516 (uint32_t)uBuf.au64[i], sizeof(uint64_t));
1517 }
1518 }
1519 }
1520 else
1521 {
1522 uint32_t i = cbBuf / sizeof(uint32_t) - 1;
1523 while (i-- > 0)
1524 if ( uBuf.au32[i] <= LNX_MAX_KALLSYMS_SYMBOLS
1525 && uBuf.au32[i] >= LNX_MIN_KALLSYMS_SYMBOLS)
1526 {
1527 uint8_t *pb = (uint8_t *)&uBuf.au32[i + 1];
1528 if ( pb[0] <= LNX_MAX_KALLSYMS_ENC_LENGTH
1529 && pb[0] >= LNX_MIN_KALLSYMS_ENC_LENGTH)
1530 {
1531 /* Check for relative base addressing. */
1532 if (i >= 1 && LNX32_VALID_ADDRESS(uBuf.au32[i - 1]))
1533 {
1534 RTGCUINTPTR uKrnlRelBase = uBuf.au32[i - 1];
1535 if ( (i <= 1 || LNX32_VALID_ADDRESS(uKrnlRelBase + uBuf.au32[i - 2]))
1536 && (i <= 2 || LNX32_VALID_ADDRESS(uKrnlRelBase + uBuf.au32[i - 3])))
1537 {
1538 Log(("dbgDiggerLinuxFindStartOfNamesAndSymbolCount: relative base %RGv (at %RGv)\n",
1539 uKrnlRelBase, CurAddr.FlatPtr + (i - 1) * sizeof(uint32_t)));
1540 pThis->fRelKrnlAddr = true;
1541 pThis->uKernelRelativeBase = uKrnlRelBase;
1542 return dbgDiggerLinuxFoundStartOfNames(pThis,
1543 DBGFR3AddrAdd(&CurAddr, (i + 1) * sizeof(uint32_t)),
1544 uBuf.au32[i], sizeof(uint32_t));
1545 }
1546 }
1547
1548 if ( (i <= 0 || LNX32_VALID_ADDRESS(uBuf.au32[i - 1]))
1549 && (i <= 1 || LNX32_VALID_ADDRESS(uBuf.au32[i - 2]))
1550 && (i <= 2 || LNX32_VALID_ADDRESS(uBuf.au32[i - 3])))
1551 return dbgDiggerLinuxFoundStartOfNames(pThis,
1552 DBGFR3AddrAdd(&CurAddr, (i + 1) * sizeof(uint32_t)),
1553 uBuf.au32[i], sizeof(uint32_t));
1554 }
1555 }
1556 }
1557
1558 /*
1559 * Advance
1560 */
1561 if (RT_UNLIKELY(cbLeft <= sizeof(uBuf)))
1562 {
1563 Log(("dbgDiggerLinuxFindStartOfNamesAndSymbolCount: failed (pHitAddr=%RGv)\n", pHitAddr->FlatPtr));
1564 return VERR_NOT_FOUND;
1565 }
1566 cbLeft -= sizeof(uBuf);
1567 DBGFR3AddrSub(&CurAddr, sizeof(uBuf));
1568 cbBuf = sizeof(uBuf);
1569 }
1570}
1571
1572
1573/**
1574 * Worker for dbgDiggerLinuxFindEndNames that records the findings.
1575 *
1576 * @returns VINF_SUCCESS
1577 * @param pThis The linux digger data to update.
1578 * @param pAddrMarkers The address of the marker (kallsyms_markers).
1579 * @param cbMarkerEntry The size of a marker entry (32-bit or 64-bit).
1580 */
1581static int dbgDiggerLinuxFoundMarkers(PDBGDIGGERLINUX pThis, PCDBGFADDRESS pAddrMarkers, uint32_t cbMarkerEntry)
1582{
1583 pThis->cbKernelNames = pAddrMarkers->FlatPtr - pThis->AddrKernelNames.FlatPtr;
1584 pThis->AddrKernelNameMarkers = *pAddrMarkers;
1585 pThis->cKernelNameMarkers = RT_ALIGN_32(pThis->cKernelSymbols, 256) / 256;
1586 pThis->AddrKernelTokenTable = *pAddrMarkers;
1587 DBGFR3AddrAdd(&pThis->AddrKernelTokenTable, pThis->cKernelNameMarkers * cbMarkerEntry);
1588
1589 Log(("dbgDiggerLinuxFoundMarkers: AddrKernelNames=%RGv cbKernelNames=%#x\n"
1590 "dbgDiggerLinuxFoundMarkers: AddrKernelNameMarkers=%RGv cKernelNameMarkers=%#x\n"
1591 "dbgDiggerLinuxFoundMarkers: AddrKernelTokenTable=%RGv\n",
1592 pThis->AddrKernelNames.FlatPtr, pThis->cbKernelNames,
1593 pThis->AddrKernelNameMarkers.FlatPtr, pThis->cKernelNameMarkers,
1594 pThis->AddrKernelTokenTable.FlatPtr));
1595 return VINF_SUCCESS;
1596}
1597
1598
1599/**
1600 * Tries to find the end of kallsyms_names and thereby the start of
1601 * kallsyms_markers and kallsyms_token_table.
1602 *
1603 * The kallsyms_names size is stored in pThis->cbKernelNames, the addresses of
1604 * the two other symbols in pThis->AddrKernelNameMarkers and
1605 * pThis->AddrKernelTokenTable. The number of marker entries is stored in
1606 * pThis->cKernelNameMarkers.
1607 *
1608 * @returns VBox status code, success indicating that all three variables have
1609 * been found and taken down.
1610 * @param pUVM The user mode VM handle.
1611 * @param pThis The Linux digger data.
1612 * @param pHitAddr An address we think is inside kallsyms_names.
1613 */
1614static int dbgDiggerLinuxFindEndOfNamesAndMore(PUVM pUVM, PDBGDIGGERLINUX pThis, PCDBGFADDRESS pHitAddr)
1615{
1616 /*
1617 * Search forward in chunks.
1618 */
1619 union
1620 {
1621 uint8_t ab[0x1000];
1622 uint32_t au32[0x1000 / sizeof(uint32_t)];
1623 uint64_t au64[0x1000 / sizeof(uint64_t)];
1624 } uBuf;
1625 bool fPendingZeroHit = false;
1626 uint32_t cbLeft = LNX_MAX_KALLSYMS_NAMES_SIZE + sizeof(uBuf);
1627 uint32_t offBuf = pHitAddr->FlatPtr & (sizeof(uBuf) - 1);
1628 DBGFADDRESS CurAddr = *pHitAddr;
1629 DBGFR3AddrSub(&CurAddr, offBuf);
1630 for (;;)
1631 {
1632 int rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &CurAddr, &uBuf, sizeof(uBuf));
1633 if (RT_FAILURE(rc))
1634 return rc;
1635
1636 /*
1637 * The kallsyms_names table is followed by kallsyms_markers we assume,
1638 * using sizeof(unsigned long) alignment like the preceeding symbols.
1639 *
1640 * The kallsyms_markers table has entried sizeof(unsigned long) and
1641 * contains offsets into kallsyms_names. The kallsyms_markers used to
1642 * index kallsyms_names and reduce seek time when looking up the name
1643 * of an address/symbol. Each entry in kallsyms_markers covers 256
1644 * symbol names.
1645 *
1646 * Because of this, the first entry is always zero and all the entries
1647 * are ascending. It also follows that the size of the table can be
1648 * calculated from kallsyms_num_syms.
1649 *
1650 * Note! We could also have walked kallsyms_names by skipping
1651 * kallsyms_num_syms names, but this is faster and we will
1652 * validate the encoded names later.
1653 */
1654 if (pThis->f64Bit)
1655 {
1656 if ( RT_UNLIKELY(fPendingZeroHit)
1657 && uBuf.au64[0] >= (LNX_MIN_KALLSYMS_ENC_LENGTH + 1) * 256
1658 && uBuf.au64[0] <= (LNX_MAX_KALLSYMS_ENC_LENGTH + 1) * 256)
1659 return dbgDiggerLinuxFoundMarkers(pThis, DBGFR3AddrSub(&CurAddr, sizeof(uint64_t)), sizeof(uint64_t));
1660
1661 uint32_t const cEntries = sizeof(uBuf) / sizeof(uint64_t);
1662 for (uint32_t i = offBuf / sizeof(uint64_t); i < cEntries; i++)
1663 if (uBuf.au64[i] == 0)
1664 {
1665 if (RT_UNLIKELY(i + 1 >= cEntries))
1666 {
1667 fPendingZeroHit = true;
1668 break;
1669 }
1670 if ( uBuf.au64[i + 1] >= (LNX_MIN_KALLSYMS_ENC_LENGTH + 1) * 256
1671 && uBuf.au64[i + 1] <= (LNX_MAX_KALLSYMS_ENC_LENGTH + 1) * 256)
1672 return dbgDiggerLinuxFoundMarkers(pThis, DBGFR3AddrAdd(&CurAddr, i * sizeof(uint64_t)), sizeof(uint64_t));
1673 }
1674 }
1675 else
1676 {
1677 if ( RT_UNLIKELY(fPendingZeroHit)
1678 && uBuf.au32[0] >= (LNX_MIN_KALLSYMS_ENC_LENGTH + 1) * 256
1679 && uBuf.au32[0] <= (LNX_MAX_KALLSYMS_ENC_LENGTH + 1) * 256)
1680 return dbgDiggerLinuxFoundMarkers(pThis, DBGFR3AddrSub(&CurAddr, sizeof(uint32_t)), sizeof(uint32_t));
1681
1682 uint32_t const cEntries = sizeof(uBuf) / sizeof(uint32_t);
1683 for (uint32_t i = offBuf / sizeof(uint32_t); i < cEntries; i++)
1684 if (uBuf.au32[i] == 0)
1685 {
1686 if (RT_UNLIKELY(i + 1 >= cEntries))
1687 {
1688 fPendingZeroHit = true;
1689 break;
1690 }
1691 if ( uBuf.au32[i + 1] >= (LNX_MIN_KALLSYMS_ENC_LENGTH + 1) * 256
1692 && uBuf.au32[i + 1] <= (LNX_MAX_KALLSYMS_ENC_LENGTH + 1) * 256)
1693 return dbgDiggerLinuxFoundMarkers(pThis, DBGFR3AddrAdd(&CurAddr, i * sizeof(uint32_t)), sizeof(uint32_t));
1694 }
1695 }
1696
1697 /*
1698 * Advance
1699 */
1700 if (RT_UNLIKELY(cbLeft <= sizeof(uBuf)))
1701 {
1702 Log(("dbgDiggerLinuxFindEndOfNamesAndMore: failed (pHitAddr=%RGv)\n", pHitAddr->FlatPtr));
1703 return VERR_NOT_FOUND;
1704 }
1705 cbLeft -= sizeof(uBuf);
1706 DBGFR3AddrAdd(&CurAddr, sizeof(uBuf));
1707 offBuf = 0;
1708 }
1709}
1710
1711
1712/**
1713 * Locates the kallsyms_token_index table.
1714 *
1715 * Storing the address in pThis->AddrKernelTokenIndex and the size of the token
1716 * table in pThis->cbKernelTokenTable.
1717 *
1718 * @returns VBox status code.
1719 * @param pUVM The user mode VM handle.
1720 * @param pThis The Linux digger data.
1721 */
1722static int dbgDiggerLinuxFindTokenIndex(PUVM pUVM, PDBGDIGGERLINUX pThis)
1723{
1724 /*
1725 * The kallsyms_token_table is very much like a string table. Due to the
1726 * nature of the compression algorithm it is reasonably short (one example
1727 * here is 853 bytes), so we'll not be reading it in chunks but in full.
1728 * To be on the safe side, we read 8KB, ASSUMING we won't run into unmapped
1729 * memory or any other nasty stuff...
1730 */
1731 union
1732 {
1733 uint8_t ab[0x2000];
1734 uint16_t au16[0x2000 / sizeof(uint16_t)];
1735 } uBuf;
1736 DBGFADDRESS CurAddr = pThis->AddrKernelTokenTable;
1737 int rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &CurAddr, &uBuf, sizeof(uBuf));
1738 if (RT_FAILURE(rc))
1739 return rc;
1740
1741 /*
1742 * We've got two choices here, either walk the string table or look for
1743 * the next structure, kallsyms_token_index.
1744 *
1745 * The token index is a table of 256 uint16_t entries (index by bytes
1746 * from kallsyms_names) that gives offsets in kallsyms_token_table. It
1747 * starts with a zero entry and the following entries are sorted in
1748 * ascending order. The range of the entries are reasonably small since
1749 * kallsyms_token_table is small.
1750 *
1751 * The alignment seems to be sizeof(unsigned long), just like
1752 * kallsyms_token_table.
1753 *
1754 * So, we start by looking for a zero 16-bit entry.
1755 */
1756 uint32_t cIncr = (pThis->f64Bit ? sizeof(uint64_t) : sizeof(uint32_t)) / sizeof(uint16_t);
1757
1758 for (uint32_t i = 0; i < sizeof(uBuf) / sizeof(uint16_t) - 16; i += cIncr)
1759 if ( uBuf.au16[i] == 0
1760 && uBuf.au16[i + 1] > 0
1761 && uBuf.au16[i + 1] <= LNX_MAX_KALLSYMS_TOKEN_LEN
1762 && (uint16_t)(uBuf.au16[i + 2] - uBuf.au16[i + 1] - 1U) <= (uint16_t)LNX_MAX_KALLSYMS_TOKEN_LEN
1763 && (uint16_t)(uBuf.au16[i + 3] - uBuf.au16[i + 2] - 1U) <= (uint16_t)LNX_MAX_KALLSYMS_TOKEN_LEN
1764 && (uint16_t)(uBuf.au16[i + 4] - uBuf.au16[i + 3] - 1U) <= (uint16_t)LNX_MAX_KALLSYMS_TOKEN_LEN
1765 && (uint16_t)(uBuf.au16[i + 5] - uBuf.au16[i + 4] - 1U) <= (uint16_t)LNX_MAX_KALLSYMS_TOKEN_LEN
1766 && (uint16_t)(uBuf.au16[i + 6] - uBuf.au16[i + 5] - 1U) <= (uint16_t)LNX_MAX_KALLSYMS_TOKEN_LEN
1767 )
1768 {
1769 pThis->AddrKernelTokenIndex = CurAddr;
1770 DBGFR3AddrAdd(&pThis->AddrKernelTokenIndex, i * sizeof(uint16_t));
1771 pThis->cbKernelTokenTable = i * sizeof(uint16_t);
1772 return VINF_SUCCESS;
1773 }
1774
1775 Log(("dbgDiggerLinuxFindTokenIndex: Failed (%RGv..%RGv)\n", CurAddr.FlatPtr, CurAddr.FlatPtr + (RTGCUINTPTR)sizeof(uBuf)));
1776 return VERR_NOT_FOUND;
1777}
1778
1779
1780/**
1781 * Loads the kernel symbols from the given kallsyms offset table decoding the symbol names
1782 * (worker common for dbgDiggerLinuxLoadKernelSymbolsAbsolute() and dbgDiggerLinuxLoadKernelSymbolsRelative()).
1783 *
1784 * @returns VBox status code.
1785 * @param pUVM The user mode VM handle.
1786 * @param pThis The Linux digger data.
1787 * @param uKernelStart Flat kernel start address.
1788 * @param cbKernel Size of the kernel in bytes.
1789 * @param pauSymOff Pointer to the array of symbol offsets in the kallsyms table
1790 * relative to the start of the kernel.
1791 */
1792static int dbgDiggerLinuxLoadKernelSymbolsWorker(PUVM pUVM, PDBGDIGGERLINUX pThis, RTGCUINTPTR uKernelStart,
1793 RTGCUINTPTR cbKernel, RTGCUINTPTR *pauSymOff)
1794{
1795 uint8_t *pbNames = (uint8_t *)RTMemAllocZ(pThis->cbKernelNames);
1796 int rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &pThis->AddrKernelNames, pbNames, pThis->cbKernelNames);
1797 if (RT_SUCCESS(rc))
1798 {
1799 char *pszzTokens = (char *)RTMemAllocZ(pThis->cbKernelTokenTable);
1800 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &pThis->AddrKernelTokenTable, pszzTokens, pThis->cbKernelTokenTable);
1801 if (RT_SUCCESS(rc))
1802 {
1803 uint16_t *paoffTokens = (uint16_t *)RTMemAllocZ(256 * sizeof(uint16_t));
1804 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &pThis->AddrKernelTokenIndex, paoffTokens, 256 * sizeof(uint16_t));
1805 if (RT_SUCCESS(rc))
1806 {
1807 /*
1808 * Create a module for the kernel.
1809 */
1810 RTDBGMOD hMod;
1811 rc = RTDbgModCreate(&hMod, "vmlinux", cbKernel, 0 /*fFlags*/);
1812 if (RT_SUCCESS(rc))
1813 {
1814 rc = RTDbgModSetTag(hMod, DIG_LNX_MOD_TAG); AssertRC(rc);
1815 rc = VINF_SUCCESS;
1816
1817 /*
1818 * Enumerate the symbols.
1819 */
1820 uint32_t offName = 0;
1821 uint32_t cLeft = pThis->cKernelSymbols;
1822 while (cLeft-- > 0 && RT_SUCCESS(rc))
1823 {
1824 /* Decode the symbol name first. */
1825 if (RT_LIKELY(offName < pThis->cbKernelNames))
1826 {
1827 uint8_t cbName = pbNames[offName++];
1828 if (RT_LIKELY(offName + cbName <= pThis->cbKernelNames))
1829 {
1830 char szSymbol[4096];
1831 uint32_t offSymbol = 0;
1832 while (cbName-- > 0)
1833 {
1834 uint8_t bEnc = pbNames[offName++];
1835 uint16_t offToken = paoffTokens[bEnc];
1836 if (RT_LIKELY(offToken < pThis->cbKernelTokenTable))
1837 {
1838 const char *pszToken = &pszzTokens[offToken];
1839 char ch;
1840 while ((ch = *pszToken++) != '\0')
1841 if (offSymbol < sizeof(szSymbol) - 1)
1842 szSymbol[offSymbol++] = ch;
1843 }
1844 else
1845 {
1846 rc = VERR_INVALID_UTF8_ENCODING;
1847 break;
1848 }
1849 }
1850 szSymbol[offSymbol < sizeof(szSymbol) ? offSymbol : sizeof(szSymbol) - 1] = '\0';
1851
1852 /* The offset. */
1853 RTGCUINTPTR uSymOff = *pauSymOff;
1854 pauSymOff++;
1855
1856 /* Add it without the type char. */
1857 if (uSymOff <= cbKernel)
1858 {
1859 rc = RTDbgModSymbolAdd(hMod, &szSymbol[1], RTDBGSEGIDX_RVA, uSymOff,
1860 0 /*cb*/, 0 /*fFlags*/, NULL);
1861 if (RT_FAILURE(rc))
1862 {
1863 if ( rc == VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE
1864 || rc == VERR_DBG_INVALID_RVA
1865 || rc == VERR_DBG_ADDRESS_CONFLICT
1866 || rc == VERR_DBG_DUPLICATE_SYMBOL)
1867 {
1868 Log2(("dbgDiggerLinuxLoadKernelSymbols: RTDbgModSymbolAdd(,%s,) failed %Rrc (ignored)\n", szSymbol, rc));
1869 rc = VINF_SUCCESS;
1870 }
1871 else
1872 Log(("dbgDiggerLinuxLoadKernelSymbols: RTDbgModSymbolAdd(,%s,) failed %Rrc\n", szSymbol, rc));
1873 }
1874 }
1875 }
1876 else
1877 {
1878 rc = VERR_END_OF_STRING;
1879 Log(("dbgDiggerLinuxLoadKernelSymbols: offName=%#x cLeft=%#x cbName=%#x cbKernelNames=%#x\n",
1880 offName, cLeft, cbName, pThis->cbKernelNames));
1881 }
1882 }
1883 else
1884 {
1885 rc = VERR_END_OF_STRING;
1886 Log(("dbgDiggerLinuxLoadKernelSymbols: offName=%#x cLeft=%#x cbKernelNames=%#x\n",
1887 offName, cLeft, pThis->cbKernelNames));
1888 }
1889 }
1890
1891 /*
1892 * Link the module into the address space.
1893 */
1894 if (RT_SUCCESS(rc))
1895 {
1896 RTDBGAS hAs = DBGFR3AsResolveAndRetain(pUVM, DBGF_AS_KERNEL);
1897 if (hAs != NIL_RTDBGAS)
1898 rc = RTDbgAsModuleLink(hAs, hMod, uKernelStart, RTDBGASLINK_FLAGS_REPLACE);
1899 else
1900 rc = VERR_INTERNAL_ERROR;
1901 RTDbgAsRelease(hAs);
1902 }
1903 else
1904 Log(("dbgDiggerLinuxLoadKernelSymbols: Failed: %Rrc\n", rc));
1905 RTDbgModRelease(hMod);
1906 }
1907 else
1908 Log(("dbgDiggerLinuxLoadKernelSymbols: RTDbgModCreate failed: %Rrc\n", rc));
1909 }
1910 else
1911 Log(("dbgDiggerLinuxLoadKernelSymbols: Reading token index at %RGv failed: %Rrc\n",
1912 pThis->AddrKernelTokenIndex.FlatPtr, rc));
1913 RTMemFree(paoffTokens);
1914 }
1915 else
1916 Log(("dbgDiggerLinuxLoadKernelSymbols: Reading token table at %RGv failed: %Rrc\n",
1917 pThis->AddrKernelTokenTable.FlatPtr, rc));
1918 RTMemFree(pszzTokens);
1919 }
1920 else
1921 Log(("dbgDiggerLinuxLoadKernelSymbols: Reading encoded names at %RGv failed: %Rrc\n",
1922 pThis->AddrKernelNames.FlatPtr, rc));
1923 RTMemFree(pbNames);
1924
1925 return rc;
1926}
1927
1928/**
1929 * Loads the kernel symbols from the kallsyms table if it contains absolute addresses
1930 *
1931 * @returns VBox status code.
1932 * @param pUVM The user mode VM handle.
1933 * @param pThis The Linux digger data.
1934 */
1935static int dbgDiggerLinuxLoadKernelSymbolsAbsolute(PUVM pUVM, PDBGDIGGERLINUX pThis)
1936{
1937 /*
1938 * Allocate memory for temporary table copies, reading the tables as we go.
1939 */
1940 uint32_t const cbGuestAddr = pThis->f64Bit ? sizeof(uint64_t) : sizeof(uint32_t);
1941 void *pvAddresses = RTMemAllocZ(pThis->cKernelSymbols * cbGuestAddr);
1942 int rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &pThis->AddrKernelAddresses, pvAddresses, pThis->cKernelSymbols * cbGuestAddr);
1943 if (RT_SUCCESS(rc))
1944 {
1945 /*
1946 * Figure out the kernel start and end and convert the absolute addresses to relative offsets.
1947 */
1948 RTGCUINTPTR uKernelStart = pThis->AddrKernelAddresses.FlatPtr;
1949 RTGCUINTPTR uKernelEnd = pThis->AddrKernelTokenIndex.FlatPtr + 256 * sizeof(uint16_t);
1950 RTGCUINTPTR *pauSymOff = (RTGCUINTPTR *)RTMemTmpAllocZ(pThis->cKernelSymbols * sizeof(RTGCUINTPTR));
1951 uint32_t i;
1952 if (cbGuestAddr == sizeof(uint64_t))
1953 {
1954 uint64_t *pauAddrs = (uint64_t *)pvAddresses;
1955 for (i = 0; i < pThis->cKernelSymbols; i++)
1956 if ( pauAddrs[i] < uKernelStart
1957 && LNX64_VALID_ADDRESS(pauAddrs[i])
1958 && uKernelStart - pauAddrs[i] < LNX_MAX_KERNEL_SIZE)
1959 uKernelStart = pauAddrs[i];
1960
1961 for (i = pThis->cKernelSymbols - 1; i > 0; i--)
1962 if ( pauAddrs[i] > uKernelEnd
1963 && LNX64_VALID_ADDRESS(pauAddrs[i])
1964 && pauAddrs[i] - uKernelEnd < LNX_MAX_KERNEL_SIZE)
1965 uKernelEnd = pauAddrs[i];
1966
1967 for (i = 0; i < pThis->cKernelSymbols; i++)
1968 pauSymOff[i] = pauAddrs[i] - uKernelStart;
1969 }
1970 else
1971 {
1972 uint32_t *pauAddrs = (uint32_t *)pvAddresses;
1973 for (i = 0; i < pThis->cKernelSymbols; i++)
1974 if ( pauAddrs[i] < uKernelStart
1975 && LNX32_VALID_ADDRESS(pauAddrs[i])
1976 && uKernelStart - pauAddrs[i] < LNX_MAX_KERNEL_SIZE)
1977 uKernelStart = pauAddrs[i];
1978
1979 for (i = pThis->cKernelSymbols - 1; i > 0; i--)
1980 if ( pauAddrs[i] > uKernelEnd
1981 && LNX32_VALID_ADDRESS(pauAddrs[i])
1982 && pauAddrs[i] - uKernelEnd < LNX_MAX_KERNEL_SIZE)
1983 uKernelEnd = pauAddrs[i];
1984
1985 for (i = 0; i < pThis->cKernelSymbols; i++)
1986 pauSymOff[i] = pauAddrs[i] - uKernelStart;
1987 }
1988
1989 RTGCUINTPTR cbKernel = uKernelEnd - uKernelStart;
1990 pThis->cbKernel = (uint32_t)cbKernel;
1991 DBGFR3AddrFromFlat(pUVM, &pThis->AddrKernelBase, uKernelStart);
1992 Log(("dbgDiggerLinuxLoadKernelSymbolsAbsolute: uKernelStart=%RGv cbKernel=%#x\n", uKernelStart, cbKernel));
1993
1994 rc = dbgDiggerLinuxLoadKernelSymbolsWorker(pUVM, pThis, uKernelStart, cbKernel, pauSymOff);
1995 if (RT_FAILURE(rc))
1996 Log(("dbgDiggerLinuxLoadKernelSymbolsAbsolute: Loading symbols from given offset table failed: %Rrc\n", rc));
1997 RTMemTmpFree(pauSymOff);
1998 }
1999 else
2000 Log(("dbgDiggerLinuxLoadKernelSymbolsAbsolute: Reading symbol addresses at %RGv failed: %Rrc\n",
2001 pThis->AddrKernelAddresses.FlatPtr, rc));
2002 RTMemFree(pvAddresses);
2003
2004 return rc;
2005}
2006
2007
2008/**
2009 * Loads the kernel symbols from the kallsyms table if it contains absolute addresses
2010 *
2011 * @returns VBox status code.
2012 * @param pUVM The user mode VM handle.
2013 * @param pThis The Linux digger data.
2014 */
2015static int dbgDiggerLinuxLoadKernelSymbolsRelative(PUVM pUVM, PDBGDIGGERLINUX pThis)
2016{
2017 /*
2018 * Allocate memory for temporary table copies, reading the tables as we go.
2019 */
2020 int32_t *pai32Offsets = (int32_t *)RTMemAllocZ(pThis->cKernelSymbols * sizeof(int32_t));
2021 int rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &pThis->AddrKernelAddresses, pai32Offsets, pThis->cKernelSymbols * sizeof(int32_t));
2022 if (RT_SUCCESS(rc))
2023 {
2024 /*
2025 * Figure out the kernel start and end and convert the absolute addresses to relative offsets.
2026 */
2027 RTGCUINTPTR uKernelStart = pThis->AddrKernelAddresses.FlatPtr;
2028 RTGCUINTPTR uKernelEnd = pThis->AddrKernelTokenIndex.FlatPtr + 256 * sizeof(uint16_t);
2029 RTGCUINTPTR *pauSymOff = (RTGCUINTPTR *)RTMemTmpAllocZ(pThis->cKernelSymbols * sizeof(RTGCUINTPTR));
2030 uint32_t i;
2031
2032 for (i = 0; i < pThis->cKernelSymbols; i++)
2033 {
2034 RTGCUINTPTR uSymAddr = dbgDiggerLinuxConvOffsetToAddr(pThis, pai32Offsets[i]);
2035
2036 if ( uSymAddr < uKernelStart
2037 && (pThis->f64Bit ? LNX64_VALID_ADDRESS(uSymAddr) : LNX32_VALID_ADDRESS(uSymAddr))
2038 && uKernelStart - uSymAddr < LNX_MAX_KERNEL_SIZE)
2039 uKernelStart = uSymAddr;
2040 }
2041
2042 for (i = pThis->cKernelSymbols - 1; i > 0; i--)
2043 {
2044 RTGCUINTPTR uSymAddr = dbgDiggerLinuxConvOffsetToAddr(pThis, pai32Offsets[i]);
2045
2046 if ( uSymAddr > uKernelEnd
2047 && (pThis->f64Bit ? LNX64_VALID_ADDRESS(uSymAddr) : LNX32_VALID_ADDRESS(uSymAddr))
2048 && uSymAddr - uKernelEnd < LNX_MAX_KERNEL_SIZE)
2049 uKernelEnd = uSymAddr;
2050
2051 /* Store the offset from the derived kernel start address. */
2052 pauSymOff[i] = uSymAddr - uKernelStart;
2053 }
2054
2055 RTGCUINTPTR cbKernel = uKernelEnd - uKernelStart;
2056 pThis->cbKernel = (uint32_t)cbKernel;
2057 DBGFR3AddrFromFlat(pUVM, &pThis->AddrKernelBase, uKernelStart);
2058 Log(("dbgDiggerLinuxLoadKernelSymbolsRelative: uKernelStart=%RGv cbKernel=%#x\n", uKernelStart, cbKernel));
2059
2060 rc = dbgDiggerLinuxLoadKernelSymbolsWorker(pUVM, pThis, uKernelStart, cbKernel, pauSymOff);
2061 if (RT_FAILURE(rc))
2062 Log(("dbgDiggerLinuxLoadKernelSymbolsRelative: Loading symbols from given offset table failed: %Rrc\n", rc));
2063 RTMemTmpFree(pauSymOff);
2064 }
2065 else
2066 Log(("dbgDiggerLinuxLoadKernelSymbolsRelative: Reading symbol addresses at %RGv failed: %Rrc\n",
2067 pThis->AddrKernelAddresses.FlatPtr, rc));
2068 RTMemFree(pai32Offsets);
2069
2070 return rc;
2071}
2072
2073
2074/**
2075 * Loads the kernel symbols.
2076 *
2077 * @returns VBox status code.
2078 * @param pUVM The user mode VM handle.
2079 * @param pThis The Linux digger data.
2080 */
2081static int dbgDiggerLinuxLoadKernelSymbols(PUVM pUVM, PDBGDIGGERLINUX pThis)
2082{
2083 /*
2084 * First the kernel itself.
2085 */
2086 if (pThis->fRelKrnlAddr)
2087 return dbgDiggerLinuxLoadKernelSymbolsRelative(pUVM, pThis);
2088 return dbgDiggerLinuxLoadKernelSymbolsAbsolute(pUVM, pThis);
2089}
2090
2091
2092/*
2093 * The module structure changed it was easier to produce different code for
2094 * each version of the structure. The C preprocessor rules!
2095 */
2096#define LNX_TEMPLATE_HEADER "DBGPlugInLinuxModuleCodeTmpl.cpp.h"
2097
2098#define LNX_BIT_SUFFIX _amd64
2099#define LNX_PTR_T uint64_t
2100#define LNX_64BIT 1
2101#include "DBGPlugInLinuxModuleVerTmpl.cpp.h"
2102
2103#define LNX_BIT_SUFFIX _x86
2104#define LNX_PTR_T uint32_t
2105#define LNX_64BIT 0
2106#include "DBGPlugInLinuxModuleVerTmpl.cpp.h"
2107
2108#undef LNX_TEMPLATE_HEADER
2109
2110static const struct
2111{
2112 uint32_t uVersion;
2113 bool f64Bit;
2114 uint64_t (*pfnProcessModule)(PDBGDIGGERLINUX pThis, PUVM pUVM, PDBGFADDRESS pAddrModule);
2115} g_aModVersions[] =
2116{
2117#define LNX_TEMPLATE_HEADER "DBGPlugInLinuxModuleTableEntryTmpl.cpp.h"
2118
2119#define LNX_BIT_SUFFIX _amd64
2120#define LNX_64BIT 1
2121#include "DBGPlugInLinuxModuleVerTmpl.cpp.h"
2122
2123#define LNX_BIT_SUFFIX _x86
2124#define LNX_64BIT 0
2125#include "DBGPlugInLinuxModuleVerTmpl.cpp.h"
2126
2127#undef LNX_TEMPLATE_HEADER
2128};
2129
2130
2131/**
2132 * Tries to find and process the module list.
2133 *
2134 * @returns VBox status code.
2135 * @param pThis The Linux digger data.
2136 * @param pUVM The user mode VM handle.
2137 */
2138static int dbgDiggerLinuxLoadModules(PDBGDIGGERLINUX pThis, PUVM pUVM)
2139{
2140 /*
2141 * Locate the list head.
2142 */
2143 RTDBGAS hAs = DBGFR3AsResolveAndRetain(pUVM, DBGF_AS_KERNEL);
2144 RTDBGSYMBOL SymInfo;
2145 int rc = RTDbgAsSymbolByName(hAs, "vmlinux!modules", &SymInfo, NULL);
2146 RTDbgAsRelease(hAs);
2147 if (RT_FAILURE(rc))
2148 return VERR_NOT_FOUND;
2149
2150 if (RT_FAILURE(rc))
2151 {
2152 LogRel(("dbgDiggerLinuxLoadModules: Failed to locate the module list (%Rrc).\n", rc));
2153 return VERR_NOT_FOUND;
2154 }
2155
2156 /*
2157 * Read the list anchor.
2158 */
2159 union
2160 {
2161 uint32_t volatile u32Pair[2];
2162 uint64_t u64Pair[2];
2163 } uListAnchor;
2164 DBGFADDRESS Addr;
2165 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, DBGFR3AddrFromFlat(pUVM, &Addr, SymInfo.Value),
2166 &uListAnchor, pThis->f64Bit ? sizeof(uListAnchor.u64Pair) : sizeof(uListAnchor.u32Pair));
2167 if (RT_FAILURE(rc))
2168 {
2169 LogRel(("dbgDiggerLinuxLoadModules: Error reading list anchor at %RX64: %Rrc\n", SymInfo.Value, rc));
2170 return VERR_NOT_FOUND;
2171 }
2172 if (!pThis->f64Bit)
2173 {
2174 uListAnchor.u64Pair[1] = uListAnchor.u32Pair[1];
2175 ASMCompilerBarrier();
2176 uListAnchor.u64Pair[0] = uListAnchor.u32Pair[0];
2177 }
2178
2179 /*
2180 * Get a numerical version number.
2181 */
2182 char szVersion[256] = "Linux version 4.19.0";
2183 bool fValid = pThis->fValid;
2184 pThis->fValid = true;
2185 dbgDiggerLinuxQueryVersion(pUVM, pThis, szVersion, sizeof(szVersion));
2186 pThis->fValid = fValid;
2187
2188 const char *pszVersion = szVersion;
2189 while (*pszVersion && !RT_C_IS_DIGIT(*pszVersion))
2190 pszVersion++;
2191
2192 size_t offVersion = 0;
2193 uint32_t uMajor = 0;
2194 while (pszVersion[offVersion] && RT_C_IS_DIGIT(pszVersion[offVersion]))
2195 uMajor = uMajor * 10 + pszVersion[offVersion++] - '0';
2196
2197 if (pszVersion[offVersion] == '.')
2198 offVersion++;
2199
2200 uint32_t uMinor = 0;
2201 while (pszVersion[offVersion] && RT_C_IS_DIGIT(pszVersion[offVersion]))
2202 uMinor = uMinor * 10 + pszVersion[offVersion++] - '0';
2203
2204 if (pszVersion[offVersion] == '.')
2205 offVersion++;
2206
2207 uint32_t uBuild = 0;
2208 while (pszVersion[offVersion] && RT_C_IS_DIGIT(pszVersion[offVersion]))
2209 uBuild = uBuild * 10 + pszVersion[offVersion++] - '0';
2210
2211 uint32_t const uGuestVer = LNX_MK_VER(uMajor, uMinor, uBuild);
2212 if (uGuestVer == 0)
2213 {
2214 LogRel(("dbgDiggerLinuxLoadModules: Failed to parse version string: %s\n", pszVersion));
2215 return VERR_NOT_FOUND;
2216 }
2217
2218 /*
2219 * Find the g_aModVersion entry that fits the best.
2220 * ASSUMES strict descending order by bitcount and version.
2221 */
2222 Assert(g_aModVersions[0].f64Bit == true);
2223 unsigned i = 0;
2224 if (!pThis->f64Bit)
2225 while (i < RT_ELEMENTS(g_aModVersions) && g_aModVersions[i].f64Bit)
2226 i++;
2227 while ( i < RT_ELEMENTS(g_aModVersions)
2228 && g_aModVersions[i].f64Bit == pThis->f64Bit
2229 && uGuestVer < g_aModVersions[i].uVersion)
2230 i++;
2231 if (i >= RT_ELEMENTS(g_aModVersions))
2232 {
2233 LogRel(("dbgDiggerLinuxLoadModules: Failed to find anything matching version: %u.%u.%u (%s)\n",
2234 uMajor, uMinor, uBuild, pszVersion));
2235 return VERR_NOT_FOUND;
2236 }
2237
2238 /*
2239 * Walk the list.
2240 */
2241 uint64_t uModAddr = uListAnchor.u64Pair[0];
2242 for (size_t iModule = 0; iModule < 4096 && uModAddr != SymInfo.Value && uModAddr != 0; iModule++)
2243 uModAddr = g_aModVersions[i].pfnProcessModule(pThis, pUVM, DBGFR3AddrFromFlat(pUVM, &Addr, uModAddr));
2244
2245 return VINF_SUCCESS;
2246}
2247
2248
2249/**
2250 * Checks if there is a likely kallsyms_names fragment at pHitAddr.
2251 *
2252 * @returns true if it's a likely fragment, false if not.
2253 * @param pUVM The user mode VM handle.
2254 * @param pHitAddr The address where paNeedle was found.
2255 * @param pabNeedle The fragment we've been searching for.
2256 * @param cbNeedle The length of the fragment.
2257 */
2258static bool dbgDiggerLinuxIsLikelyNameFragment(PUVM pUVM, PCDBGFADDRESS pHitAddr, uint8_t const *pabNeedle, uint8_t cbNeedle)
2259{
2260 /*
2261 * Examples of lead and tail bytes of our choosen needle in a randomly
2262 * picked kernel:
2263 * k o b j
2264 * 22 6b 6f 62 6a aa
2265 * fc 6b 6f 62 6a aa
2266 * 82 6b 6f 62 6a 5f - ascii trail byte (_).
2267 * ee 6b 6f 62 6a aa
2268 * fc 6b 6f 62 6a 5f - ascii trail byte (_).
2269 * 0a 74 6b 6f 62 6a 5f ea - ascii lead (t) and trail (_) bytes.
2270 * 0b 54 6b 6f 62 6a aa - ascii lead byte (T).
2271 * ... omitting 29 samples similar to the last two ...
2272 * d8 6b 6f 62 6a aa
2273 * d8 6b 6f 62 6a aa
2274 * d8 6b 6f 62 6a aa
2275 * d8 6b 6f 62 6a aa
2276 * f9 5f 6b 6f 62 6a 5f 94 - ascii lead and trail bytes (_)
2277 * f9 5f 6b 6f 62 6a 0c - ascii lead byte (_).
2278 * fd 6b 6f 62 6a 0f
2279 * ... enough.
2280 */
2281 uint8_t abBuf[32];
2282 DBGFADDRESS ReadAddr = *pHitAddr;
2283 DBGFR3AddrSub(&ReadAddr, 2);
2284 int rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &ReadAddr, abBuf, 2 + cbNeedle + 2);
2285 if (RT_SUCCESS(rc))
2286 {
2287 if (memcmp(&abBuf[2], pabNeedle, cbNeedle) == 0) /* paranoia */
2288 {
2289 uint8_t const bLead = abBuf[1] == '_' || abBuf[1] == 'T' || abBuf[1] == 't' ? abBuf[0] : abBuf[1];
2290 uint8_t const offTail = 2 + cbNeedle;
2291 uint8_t const bTail = abBuf[offTail] == '_' ? abBuf[offTail] : abBuf[offTail + 1];
2292 if ( bLead >= 1 && (bLead < 0x20 || bLead >= 0x80)
2293 && bTail >= 1 && (bTail < 0x20 || bTail >= 0x80))
2294 return true;
2295 Log(("dbgDiggerLinuxIsLikelyNameFragment: failed at %RGv: bLead=%#x bTail=%#x (offTail=%#x)\n",
2296 pHitAddr->FlatPtr, bLead, bTail, offTail));
2297 }
2298 else
2299 Log(("dbgDiggerLinuxIsLikelyNameFragment: failed at %RGv: Needle changed!\n", pHitAddr->FlatPtr));
2300 }
2301 else
2302 Log(("dbgDiggerLinuxIsLikelyNameFragment: failed at %RGv: %Rrc\n", pHitAddr->FlatPtr, rc));
2303
2304 return false;
2305}
2306
2307/**
2308 * Tries to find and load the kernel symbol table with the given needle.
2309 *
2310 * @returns VBox status code.
2311 * @param pThis The Linux digger data.
2312 * @param pUVM The user mode VM handle.
2313 * @param pabNeedle The needle to use for searching.
2314 * @param cbNeedle Size of the needle in bytes.
2315 */
2316static int dbgDiggerLinuxFindSymbolTableFromNeedle(PDBGDIGGERLINUX pThis, PUVM pUVM, uint8_t const *pabNeedle, uint8_t cbNeedle)
2317{
2318 int rc = VINF_SUCCESS;
2319
2320 /*
2321 * Go looking for the kallsyms table. If it's there, it will be somewhere
2322 * after the linux_banner symbol, so use it for starting the search.
2323 */
2324 DBGFADDRESS CurAddr = pThis->AddrLinuxBanner;
2325 uint32_t cbLeft = LNX_MAX_KERNEL_SIZE;
2326 while (cbLeft > 4096)
2327 {
2328 DBGFADDRESS HitAddr;
2329 rc = DBGFR3MemScan(pUVM, 0 /*idCpu*/, &CurAddr, cbLeft, 1 /*uAlign*/,
2330 pabNeedle, cbNeedle, &HitAddr);
2331 if (RT_FAILURE(rc))
2332 break;
2333 if (dbgDiggerLinuxIsLikelyNameFragment(pUVM, &HitAddr, pabNeedle, cbNeedle))
2334 {
2335 /* There will be another hit near by. */
2336 DBGFR3AddrAdd(&HitAddr, 1);
2337 rc = DBGFR3MemScan(pUVM, 0 /*idCpu*/, &HitAddr, LNX_MAX_KALLSYMS_NAMES_SIZE, 1 /*uAlign*/,
2338 pabNeedle, cbNeedle, &HitAddr);
2339 if ( RT_SUCCESS(rc)
2340 && dbgDiggerLinuxIsLikelyNameFragment(pUVM, &HitAddr, pabNeedle, cbNeedle))
2341 {
2342 /*
2343 * We've got a very likely candidate for a location inside kallsyms_names.
2344 * Try find the start of it, that is to say, try find kallsyms_num_syms.
2345 * kallsyms_num_syms is aligned on sizeof(unsigned long) boundrary
2346 */
2347 rc = dbgDiggerLinuxFindStartOfNamesAndSymbolCount(pUVM, pThis, &HitAddr);
2348 if (RT_SUCCESS(rc))
2349 rc = dbgDiggerLinuxFindEndOfNamesAndMore(pUVM, pThis, &HitAddr);
2350 if (RT_SUCCESS(rc))
2351 rc = dbgDiggerLinuxFindTokenIndex(pUVM, pThis);
2352 if (RT_SUCCESS(rc))
2353 rc = dbgDiggerLinuxLoadKernelSymbols(pUVM, pThis);
2354 if (RT_SUCCESS(rc))
2355 {
2356 rc = dbgDiggerLinuxLoadModules(pThis, pUVM);
2357 break;
2358 }
2359 }
2360 }
2361
2362 /*
2363 * Advance.
2364 */
2365 RTGCUINTPTR cbDistance = HitAddr.FlatPtr - CurAddr.FlatPtr + cbNeedle;
2366 if (RT_UNLIKELY(cbDistance >= cbLeft))
2367 {
2368 Log(("dbgDiggerLinuxInit: Failed to find kallsyms\n"));
2369 break;
2370 }
2371 cbLeft -= cbDistance;
2372 DBGFR3AddrAdd(&CurAddr, cbDistance);
2373
2374 }
2375
2376 return rc;
2377}
2378
2379/**
2380 * Skips whitespace and comments in the given config returning the pointer
2381 * to the first non whitespace character.
2382 *
2383 * @returns Pointer to the first non whitespace character or NULL if the end
2384 * of the string was reached.
2385 * @param pszCfg The config string.
2386 */
2387static const char *dbgDiggerLinuxCfgSkipWhitespace(const char *pszCfg)
2388{
2389 do
2390 {
2391 while ( *pszCfg != '\0'
2392 && ( RT_C_IS_SPACE(*pszCfg)
2393 || *pszCfg == '\n'))
2394 pszCfg++;
2395
2396 /* Do we have a comment? Skip it. */
2397 if (*pszCfg == '#')
2398 {
2399 while ( *pszCfg != '\n'
2400 && *pszCfg != '\0')
2401 pszCfg++;
2402 }
2403 } while ( *pszCfg != '\0'
2404 && ( RT_C_IS_SPACE(*pszCfg)
2405 || *pszCfg == '\n'
2406 || *pszCfg == '#'));
2407
2408 return pszCfg;
2409}
2410
2411/**
2412 * Parses an identifier at the given position.
2413 *
2414 * @returns VBox status code.
2415 * @param pszCfg The config data.
2416 * @param ppszCfgNext Where to store the pointer to the data following the identifier.
2417 * @param ppszIde Where to store the pointer to the identifier on success.
2418 * Free with RTStrFree().
2419 */
2420static int dbgDiggerLinuxCfgParseIde(const char *pszCfg, const char **ppszCfgNext, char **ppszIde)
2421{
2422 int rc = VINF_SUCCESS;
2423 size_t cchIde = 0;
2424
2425 while ( *pszCfg != '\0'
2426 && ( RT_C_IS_ALNUM(*pszCfg)
2427 || *pszCfg == '_'))
2428 {
2429 cchIde++;
2430 pszCfg++;
2431 }
2432
2433 if (cchIde)
2434 {
2435 *ppszIde = RTStrDupN(pszCfg - cchIde, cchIde);
2436 if (!*ppszIde)
2437 rc = VERR_NO_STR_MEMORY;
2438 }
2439
2440 *ppszCfgNext = pszCfg;
2441 return rc;
2442}
2443
2444/**
2445 * Parses a value for a config item.
2446 *
2447 * @returns VBox status code.
2448 * @param pszCfg The config data.
2449 * @param ppszCfgNext Where to store the pointer to the data following the identifier.
2450 * @param ppCfgItem Where to store the created config item on success.
2451 */
2452static int dbgDiggerLinuxCfgParseVal(const char *pszCfg, const char **ppszCfgNext,
2453 PDBGDIGGERLINUXCFGITEM *ppCfgItem)
2454{
2455 int rc = VINF_SUCCESS;
2456 PDBGDIGGERLINUXCFGITEM pCfgItem = NULL;
2457
2458 if (RT_C_IS_DIGIT(*pszCfg) || *pszCfg == '-')
2459 {
2460 /* Parse the number. */
2461 int64_t i64Num;
2462 rc = RTStrToInt64Ex(pszCfg, (char **)ppszCfgNext, 0, &i64Num);
2463 if ( RT_SUCCESS(rc)
2464 || rc == VWRN_TRAILING_CHARS
2465 || rc == VWRN_TRAILING_SPACES)
2466 {
2467 pCfgItem = (PDBGDIGGERLINUXCFGITEM)RTMemAllocZ(sizeof(DBGDIGGERLINUXCFGITEM));
2468 if (pCfgItem)
2469 {
2470 pCfgItem->enmType = DBGDIGGERLINUXCFGITEMTYPE_NUMBER;
2471 pCfgItem->u.i64Num = i64Num;
2472 }
2473 else
2474 rc = VERR_NO_MEMORY;
2475 }
2476 }
2477 else if (*pszCfg == '\"')
2478 {
2479 /* Parse a string. */
2480 const char *pszCfgCur = pszCfg + 1;
2481 while ( *pszCfgCur != '\0'
2482 && *pszCfgCur != '\"')
2483 pszCfgCur++;
2484
2485 if (*pszCfgCur == '\"')
2486 {
2487 pCfgItem = (PDBGDIGGERLINUXCFGITEM)RTMemAllocZ(RT_UOFFSETOF_DYN(DBGDIGGERLINUXCFGITEM,
2488 u.aszString[pszCfgCur - pszCfg + 1]));
2489 if (pCfgItem)
2490 {
2491 pCfgItem->enmType = DBGDIGGERLINUXCFGITEMTYPE_STRING;
2492 RTStrCopyEx(&pCfgItem->u.aszString[0], pszCfgCur - pszCfg + 1, pszCfg, pszCfgCur - pszCfg);
2493 *ppszCfgNext = pszCfgCur + 1;
2494 }
2495 else
2496 rc = VERR_NO_MEMORY;
2497 }
2498 else
2499 rc = VERR_INVALID_STATE;
2500 }
2501 else if ( *pszCfg == 'y'
2502 || *pszCfg == 'm')
2503 {
2504 /* Included or module. */
2505 pCfgItem = (PDBGDIGGERLINUXCFGITEM)RTMemAllocZ(sizeof(DBGDIGGERLINUXCFGITEM));
2506 if (pCfgItem)
2507 {
2508 pCfgItem->enmType = DBGDIGGERLINUXCFGITEMTYPE_FLAG;
2509 pCfgItem->u.fModule = *pszCfg == 'm';
2510 }
2511 else
2512 rc = VERR_NO_MEMORY;
2513 pszCfg++;
2514 *ppszCfgNext = pszCfg;
2515 }
2516 else
2517 rc = VERR_INVALID_STATE;
2518
2519 if (RT_SUCCESS(rc))
2520 *ppCfgItem = pCfgItem;
2521 else if (pCfgItem)
2522 RTMemFree(pCfgItem);
2523
2524 return rc;
2525}
2526
2527/**
2528 * Parses the given kernel config and creates the config database.
2529 *
2530 * @returns VBox status code
2531 * @param pThis The Linux digger data.
2532 * @param pszCfg The config string.
2533 */
2534static int dbgDiggerLinuxCfgParse(PDBGDIGGERLINUX pThis, const char *pszCfg)
2535{
2536 int rc = VINF_SUCCESS;
2537
2538 /*
2539 * The config is a text file with the following elements:
2540 * # starts a comment which goes till the end of the line
2541 * <Ide>=<val> where <Ide> is an identifier consisting of
2542 * alphanumerical characters (including _)
2543 * <val> denotes the value for the identifier and can have the following
2544 * formats:
2545 * (-)[0-9]* for numbers
2546 * "..." for a string value
2547 * m when a feature is enabled as a module
2548 * y when a feature is enabled
2549 * Newlines are used as a separator between values and mark the end
2550 * of a comment
2551 */
2552 const char *pszCfgCur = pszCfg;
2553 while ( RT_SUCCESS(rc)
2554 && *pszCfgCur != '\0')
2555 {
2556 /* Start skipping the whitespace. */
2557 pszCfgCur = dbgDiggerLinuxCfgSkipWhitespace(pszCfgCur);
2558 if ( pszCfgCur
2559 && *pszCfgCur != '\0')
2560 {
2561 char *pszIde = NULL;
2562 /* Must be an identifier, parse it. */
2563 rc = dbgDiggerLinuxCfgParseIde(pszCfgCur, &pszCfgCur, &pszIde);
2564 if (RT_SUCCESS(rc))
2565 {
2566 /*
2567 * Skip whitespace again (shouldn't be required because = follows immediately
2568 * in the observed configs).
2569 */
2570 pszCfgCur = dbgDiggerLinuxCfgSkipWhitespace(pszCfgCur);
2571 if ( pszCfgCur
2572 && *pszCfgCur == '=')
2573 {
2574 pszCfgCur++;
2575 pszCfgCur = dbgDiggerLinuxCfgSkipWhitespace(pszCfgCur);
2576 if ( pszCfgCur
2577 && *pszCfgCur != '\0')
2578 {
2579 /* Get the value. */
2580 PDBGDIGGERLINUXCFGITEM pCfgItem = NULL;
2581 rc = dbgDiggerLinuxCfgParseVal(pszCfgCur, &pszCfgCur, &pCfgItem);
2582 if (RT_SUCCESS(rc))
2583 {
2584 pCfgItem->Core.pszString = pszIde;
2585 bool fRc = RTStrSpaceInsert(&pThis->hCfgDb, &pCfgItem->Core);
2586 if (!fRc)
2587 {
2588 RTStrFree(pszIde);
2589 RTMemFree(pCfgItem);
2590 rc = VERR_INVALID_STATE;
2591 }
2592 }
2593 }
2594 else
2595 rc = VERR_EOF;
2596 }
2597 else
2598 rc = VERR_INVALID_STATE;
2599 }
2600
2601 if (RT_FAILURE(rc))
2602 RTStrFree(pszIde);
2603 }
2604 else
2605 break; /* Reached the end of the config. */
2606 }
2607
2608 if (RT_FAILURE(rc))
2609 dbgDiggerLinuxCfgDbDestroy(pThis);
2610
2611 return rc;
2612}
2613
2614/**
2615 * Decompresses the given config and validates the UTF-8 encoding.
2616 *
2617 * @returns VBox status code.
2618 * @param pbCfgComp The compressed config.
2619 * @param cbCfgComp Size of the compressed config.
2620 * @param ppszCfg Where to store the pointer to the decompressed config
2621 * on success.
2622 */
2623static int dbgDiggerLinuxCfgDecompress(const uint8_t *pbCfgComp, size_t cbCfgComp, char **ppszCfg)
2624{
2625 int rc = VINF_SUCCESS;
2626 RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
2627
2628 rc = RTVfsIoStrmFromBuffer(RTFILE_O_READ, pbCfgComp, cbCfgComp, &hVfsIos);
2629 if (RT_SUCCESS(rc))
2630 {
2631 RTVFSIOSTREAM hVfsIosDecomp = NIL_RTVFSIOSTREAM;
2632 rc = RTZipGzipDecompressIoStream(hVfsIos, RTZIPGZIPDECOMP_F_ALLOW_ZLIB_HDR, &hVfsIosDecomp);
2633 if (RT_SUCCESS(rc))
2634 {
2635 char *pszCfg = NULL;
2636 size_t cchCfg = 0;
2637 size_t cbRead = 0;
2638
2639 do
2640 {
2641 uint8_t abBuf[_64K];
2642 rc = RTVfsIoStrmRead(hVfsIosDecomp, abBuf, sizeof(abBuf), true /*fBlocking*/, &cbRead);
2643 if (rc == VINF_EOF && cbRead == 0)
2644 rc = VINF_SUCCESS;
2645 if ( RT_SUCCESS(rc)
2646 && cbRead > 0)
2647 {
2648 /* Append data. */
2649 char *pszCfgNew = pszCfg;
2650 rc = RTStrRealloc(&pszCfgNew, cchCfg + cbRead + 1);
2651 if (RT_SUCCESS(rc))
2652 {
2653 pszCfg = pszCfgNew;
2654 memcpy(pszCfg + cchCfg, &abBuf[0], cbRead);
2655 cchCfg += cbRead;
2656 pszCfg[cchCfg] = '\0'; /* Enforce string termination. */
2657 }
2658 }
2659 } while (RT_SUCCESS(rc) && cbRead > 0);
2660
2661 if (RT_SUCCESS(rc))
2662 *ppszCfg = pszCfg;
2663 else if (RT_FAILURE(rc) && pszCfg)
2664 RTStrFree(pszCfg);
2665
2666 RTVfsIoStrmRelease(hVfsIosDecomp);
2667 }
2668 RTVfsIoStrmRelease(hVfsIos);
2669 }
2670
2671 return rc;
2672}
2673
2674/**
2675 * Reads and decodes the compressed kernel config.
2676 *
2677 * @returns VBox status code.
2678 * @param pThis The Linux digger data.
2679 * @param pUVM The user mode VM handle.
2680 * @param pAddrStart The start address of the compressed config.
2681 * @param cbCfgComp The size of the compressed config.
2682 */
2683static int dbgDiggerLinuxCfgDecode(PDBGDIGGERLINUX pThis, PUVM pUVM,
2684 PCDBGFADDRESS pAddrStart, size_t cbCfgComp)
2685{
2686 int rc = VINF_SUCCESS;
2687 uint8_t *pbCfgComp = (uint8_t *)RTMemTmpAlloc(cbCfgComp);
2688 if (!pbCfgComp)
2689 return VERR_NO_MEMORY;
2690
2691 rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, pAddrStart, pbCfgComp, cbCfgComp);
2692 if (RT_SUCCESS(rc))
2693 {
2694 char *pszCfg = NULL;
2695 rc = dbgDiggerLinuxCfgDecompress(pbCfgComp, cbCfgComp, &pszCfg);
2696 if (RT_SUCCESS(rc))
2697 {
2698 if (RTStrIsValidEncoding(pszCfg))
2699 rc = dbgDiggerLinuxCfgParse(pThis, pszCfg);
2700 else
2701 rc = VERR_INVALID_UTF8_ENCODING;
2702 RTStrFree(pszCfg);
2703 }
2704 }
2705
2706 RTMemFree(pbCfgComp);
2707 return rc;
2708}
2709
2710/**
2711 * Tries to find the compressed kernel config in the kernel address space
2712 * and sets up the config database.
2713 *
2714 * @returns VBox status code.
2715 * @param pThis The Linux digger data.
2716 * @param pUVM The user mode VM handle.
2717 */
2718static int dbgDiggerLinuxCfgFind(PDBGDIGGERLINUX pThis, PUVM pUVM)
2719{
2720 int rc = VINF_SUCCESS;
2721
2722 /*
2723 * Go looking for the IKCFG_ST string which indicates the start
2724 * of the compressed config file.
2725 */
2726 static const uint8_t s_abCfgNeedleStart[] = "IKCFG_ST";
2727 static const uint8_t s_abCfgNeedleEnd[] = "IKCFG_ED";
2728 DBGFADDRESS CurAddr = pThis->AddrLinuxBanner;
2729 uint32_t cbLeft = LNX_MAX_KERNEL_SIZE;
2730 while (cbLeft > 4096)
2731 {
2732 DBGFADDRESS HitAddrStart;
2733 rc = DBGFR3MemScan(pUVM, 0 /*idCpu*/, &CurAddr, cbLeft, 1 /*uAlign*/,
2734 s_abCfgNeedleStart, sizeof(s_abCfgNeedleStart) - 1, &HitAddrStart);
2735 if (RT_FAILURE(rc))
2736 break;
2737
2738 /* Check for the end marker which shouldn't be that far away. */
2739 DBGFR3AddrAdd(&HitAddrStart, sizeof(s_abCfgNeedleStart) - 1);
2740 DBGFADDRESS HitAddrEnd;
2741 rc = DBGFR3MemScan(pUVM, 0 /* idCpu */, &HitAddrStart, LNX_MAX_COMPRESSED_CFG_SIZE,
2742 1 /* uAlign */, s_abCfgNeedleEnd, sizeof(s_abCfgNeedleEnd) - 1, &HitAddrEnd);
2743 if (RT_SUCCESS(rc))
2744 {
2745 /* Allocate a buffer to hold the compressed data between the markers and fetch it. */
2746 RTGCUINTPTR cbCfg = HitAddrEnd.FlatPtr - HitAddrStart.FlatPtr;
2747 Assert(cbCfg == (size_t)cbCfg);
2748 rc = dbgDiggerLinuxCfgDecode(pThis, pUVM, &HitAddrStart, cbCfg);
2749 if (RT_SUCCESS(rc))
2750 break;
2751 }
2752
2753 /*
2754 * Advance.
2755 */
2756 RTGCUINTPTR cbDistance = HitAddrStart.FlatPtr - CurAddr.FlatPtr + sizeof(s_abCfgNeedleStart) - 1;
2757 if (RT_UNLIKELY(cbDistance >= cbLeft))
2758 {
2759 LogFunc(("Failed to find compressed kernel config\n"));
2760 break;
2761 }
2762 cbLeft -= cbDistance;
2763 DBGFR3AddrAdd(&CurAddr, cbDistance);
2764
2765 }
2766
2767 return rc;
2768}
2769
2770/**
2771 * Probes for a Linux kernel starting at the given address.
2772 *
2773 * @returns Flag whether something which looks like a valid Linux kernel was found.
2774 * @param pThis The Linux digger data.
2775 * @param pUVM The user mode VM handle.
2776 * @param uAddrStart The address to start scanning at.
2777 * @param cbScan How much to scan.
2778 */
2779static bool dbgDiggerLinuxProbeWithAddr(PDBGDIGGERLINUX pThis, PUVM pUVM, RTGCUINTPTR uAddrStart, size_t cbScan)
2780{
2781 /*
2782 * Look for "Linux version " at the start of the rodata segment.
2783 * Hope that this comes before any message buffer or other similar string.
2784 */
2785 DBGFADDRESS KernelAddr;
2786 DBGFR3AddrFromFlat(pUVM, &KernelAddr, uAddrStart);
2787 DBGFADDRESS HitAddr;
2788 int rc = DBGFR3MemScan(pUVM, 0, &KernelAddr, cbScan, 1,
2789 g_abLinuxVersion, sizeof(g_abLinuxVersion) - 1, &HitAddr);
2790 if (RT_SUCCESS(rc))
2791 {
2792 char szTmp[128];
2793 char const *pszX = &szTmp[sizeof(g_abLinuxVersion) - 1];
2794 rc = DBGFR3MemReadString(pUVM, 0, &HitAddr, szTmp, sizeof(szTmp));
2795 if ( RT_SUCCESS(rc)
2796 && ( ( pszX[0] == '2' /* 2.x.y with x in {0..6} */
2797 && pszX[1] == '.'
2798 && pszX[2] >= '0'
2799 && pszX[2] <= '6')
2800 || ( pszX[0] >= '3' /* 3.x, 4.x, ... 9.x */
2801 && pszX[0] <= '9'
2802 && pszX[1] == '.'
2803 && pszX[2] >= '0'
2804 && pszX[2] <= '9')
2805 )
2806 )
2807 {
2808 pThis->AddrKernelBase = KernelAddr;
2809 pThis->AddrLinuxBanner = HitAddr;
2810 return true;
2811 }
2812 }
2813
2814 return false;
2815}
2816
2817/**
2818 * Probes for a Linux kernel which has KASLR enabled.
2819 *
2820 * @returns Flag whether a possible candidate location was found.
2821 * @param pThis The Linux digger data.
2822 * @param pUVM The user mode VM handle.
2823 */
2824static bool dbgDiggerLinuxProbeKaslr(PDBGDIGGERLINUX pThis, PUVM pUVM)
2825{
2826 /**
2827 * With KASLR the kernel is loaded at a different address at each boot making detection
2828 * more difficult for us.
2829 *
2830 * The randomization is done in arch/x86/boot/compressed/kaslr.c:choose_random_location() (as of Nov 2017).
2831 * At the end of the method a random offset is chosen using find_random_virt_addr() which is added to the
2832 * kernel map start in the caller (the start of the kernel depends on the bit size, see LNX32_KERNEL_ADDRESS_START
2833 * and LNX64_KERNEL_ADDRESS_START for 32bit and 64bit kernels respectively).
2834 * The lowest offset possible is LOAD_PHYSICAL_ADDR which is defined in arch/x86/include/asm/boot.h
2835 * using CONFIG_PHYSICAL_START aligned to CONFIG_PHYSICAL_ALIGN.
2836 * The default CONFIG_PHYSICAL_START and CONFIG_PHYSICAL_ALIGN are both 0x1000000 no matter whether a 32bit
2837 * or a 64bit kernel is used. So the lowest offset to the kernel start address is 0x1000000.
2838 * The find_random_virt_addr() the number of possible slots where the kernel can be placed based on the image size
2839 * is calculated using the following formula:
2840 * cSlots = ((KERNEL_IMAGE_SIZE - 0x1000000 (minimum) - image_size) / 0x1000000 (CONFIG_PHYSICAL_ALIGN)) + 1
2841 *
2842 * KERNEL_IMAGE_SIZE is 1GB for 64bit kernels and 512MB for 32bit kernels, so the maximum number of slots (resulting
2843 * in the largest possible offset) can be achieved when image_size (which contains the real size of the kernel image
2844 * which is unknown for us) goes to 0 and a 1GB KERNEL_IMAGE_SIZE is assumed. With that the biggest cSlots which can be
2845 * achieved is 64. The chosen random offset is taken from a random long integer using kaslr_get_random_long() modulo the
2846 * number of slots which selects a slot between 0 and 63. The final offset is calculated using:
2847 * offAddr = random_addr * 0x1000000 (CONFIG_PHYSICAL_ALIGN) + 0x1000000 (minimum)
2848 *
2849 * So the highest offset the kernel can start is 0x40000000 which is 1GB (plus the maximum kernel size we defined).
2850 */
2851 if (dbgDiggerLinuxProbeWithAddr(pThis, pUVM, LNX64_KERNEL_ADDRESS_START, _1G + LNX_MAX_KERNEL_SIZE))
2852 return true;
2853
2854 /*
2855 * 32bit variant, makes sure we don't exceed the 4GB address space or DBGFR3MemScan() returns VERR_DBGF_MEM_NOT_FOUND immediately
2856 * without searching the remainder of the address space.
2857 *
2858 * The default split is 3GB userspace and 1GB kernel, so we just search the entire upper 1GB kernel space.
2859 */
2860 if (dbgDiggerLinuxProbeWithAddr(pThis, pUVM, LNX32_KERNEL_ADDRESS_START, _4G - LNX32_KERNEL_ADDRESS_START))
2861 return true;
2862
2863 return false;
2864}
2865
2866/**
2867 * @copydoc DBGFOSREG::pfnInit
2868 */
2869static DECLCALLBACK(int) dbgDiggerLinuxInit(PUVM pUVM, void *pvData)
2870{
2871 PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
2872 Assert(!pThis->fValid);
2873
2874 /*
2875 * Assume 64-bit kernels all live way beyond 32-bit address space.
2876 */
2877 pThis->f64Bit = pThis->AddrLinuxBanner.FlatPtr > UINT32_MAX;
2878 pThis->fRelKrnlAddr = false;
2879
2880 pThis->hCfgDb = NULL;
2881
2882 /*
2883 * Try to find the compressed kernel config and parse it before we try
2884 * to get the symbol table, the config database is required to select
2885 * the method to use.
2886 */
2887 int rc = dbgDiggerLinuxCfgFind(pThis, pUVM);
2888 if (RT_FAILURE(rc))
2889 LogFlowFunc(("Failed to find kernel config (%Rrc), no config database available\n", rc));
2890
2891 static const uint8_t s_abNeedle[] = "kobj";
2892 rc = dbgDiggerLinuxFindSymbolTableFromNeedle(pThis, pUVM, s_abNeedle, sizeof(s_abNeedle) - 1);
2893 if (RT_FAILURE(rc))
2894 {
2895 /* Try alternate needle (seen on older x86 Linux kernels). */
2896 static const uint8_t s_abNeedleAlt[] = "kobjec";
2897 rc = dbgDiggerLinuxFindSymbolTableFromNeedle(pThis, pUVM, s_abNeedleAlt, sizeof(s_abNeedleAlt) - 1);
2898 if (RT_FAILURE(rc))
2899 {
2900 static const uint8_t s_abNeedleOSuseX86[] = "nmi"; /* OpenSuSe 10.2 x86 */
2901 rc = dbgDiggerLinuxFindSymbolTableFromNeedle(pThis, pUVM, s_abNeedleOSuseX86, sizeof(s_abNeedleOSuseX86) - 1);
2902 }
2903 }
2904
2905 pThis->fValid = true;
2906 return VINF_SUCCESS;
2907}
2908
2909
2910/**
2911 * @copydoc DBGFOSREG::pfnProbe
2912 */
2913static DECLCALLBACK(bool) dbgDiggerLinuxProbe(PUVM pUVM, void *pvData)
2914{
2915 PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
2916
2917 for (unsigned i = 0; i < RT_ELEMENTS(g_au64LnxKernelAddresses); i++)
2918 {
2919 if (dbgDiggerLinuxProbeWithAddr(pThis, pUVM, g_au64LnxKernelAddresses[i], LNX_MAX_KERNEL_SIZE))
2920 return true;
2921 }
2922
2923 /* Maybe the kernel uses KASLR. */
2924 if (dbgDiggerLinuxProbeKaslr(pThis, pUVM))
2925 return true;
2926
2927 return false;
2928}
2929
2930
2931/**
2932 * @copydoc DBGFOSREG::pfnDestruct
2933 */
2934static DECLCALLBACK(void) dbgDiggerLinuxDestruct(PUVM pUVM, void *pvData)
2935{
2936 RT_NOREF2(pUVM, pvData);
2937}
2938
2939
2940/**
2941 * @copydoc DBGFOSREG::pfnConstruct
2942 */
2943static DECLCALLBACK(int) dbgDiggerLinuxConstruct(PUVM pUVM, void *pvData)
2944{
2945 RT_NOREF1(pUVM);
2946 PDBGDIGGERLINUX pThis = (PDBGDIGGERLINUX)pvData;
2947 pThis->IDmesg.u32Magic = DBGFOSIDMESG_MAGIC;
2948 pThis->IDmesg.pfnQueryKernelLog = dbgDiggerLinuxIDmsg_QueryKernelLog;
2949 pThis->IDmesg.u32EndMagic = DBGFOSIDMESG_MAGIC;
2950
2951 return VINF_SUCCESS;
2952}
2953
2954
2955const DBGFOSREG g_DBGDiggerLinux =
2956{
2957 /* .u32Magic = */ DBGFOSREG_MAGIC,
2958 /* .fFlags = */ 0,
2959 /* .cbData = */ sizeof(DBGDIGGERLINUX),
2960 /* .szName = */ "Linux",
2961 /* .pfnConstruct = */ dbgDiggerLinuxConstruct,
2962 /* .pfnDestruct = */ dbgDiggerLinuxDestruct,
2963 /* .pfnProbe = */ dbgDiggerLinuxProbe,
2964 /* .pfnInit = */ dbgDiggerLinuxInit,
2965 /* .pfnRefresh = */ dbgDiggerLinuxRefresh,
2966 /* .pfnTerm = */ dbgDiggerLinuxTerm,
2967 /* .pfnQueryVersion = */ dbgDiggerLinuxQueryVersion,
2968 /* .pfnQueryInterface = */ dbgDiggerLinuxQueryInterface,
2969 /* .pfnStackUnwindAssist = */ dbgDiggerLinuxStackUnwindAssist,
2970 /* .u32EndMagic = */ DBGFOSREG_MAGIC
2971};
2972
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