1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
|
/* $Id: DBGFAddrSpace.cpp $ */
/** @file
* DBGF - Debugger Facility, Address Space Management.
*/
/*
* Copyright (C) 2008 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/** @page pg_dbgf_addr_space DBGFAddrSpace - Address Space Management
*
* What's an address space? It's mainly a convenient way of stuffing
* module segments and ad-hoc symbols together. It will also help out
* when the debugger gets extended to deal with user processes later.
*
* There are two standard address spaces that will always be present:
* - The physical address space.
* - The global virtual address space.
*
* Additional address spaces will be added and removed at runtime for
* guest processes. The global virtual address space will be used to
* track the kernel parts of the OS, or at least the bits of the kernel
* that is part of all address spaces (mac os x and 4G/4G patched linux).
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DBGF
#include <VBox/dbgf.h>
#include <VBox/mm.h>
#include "DBGFInternal.h"
#include <VBox/vm.h>
#include <VBox/err.h>
#include <VBox/log.h>
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/ctype.h>
#include <iprt/env.h>
#include <iprt/path.h>
#include <iprt/param.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Address space database node.
*/
typedef struct DBGFASDBNODE
{
/** The node core for DBGF::AsHandleTree, the key is the address space handle. */
AVLPVNODECORE HandleCore;
/** The node core for DBGF::AsPidTree, the key is the process id. */
AVLU32NODECORE PidCore;
/** The node core for DBGF::AsNameSpace, the string is the address space name. */
RTSTRSPACECORE NameCore;
} DBGFASDBNODE;
/** Pointer to an address space database node. */
typedef DBGFASDBNODE *PDBGFASDBNODE;
/**
* For dbgfR3AsLoadImageOpenData and dbgfR3AsLoadMapOpenData.
*/
typedef struct DBGFR3ASLOADOPENDATA
{
const char *pszModName;
RTGCUINTPTR uSubtrahend;
uint32_t fFlags;
RTDBGMOD hMod;
} DBGFR3ASLOADOPENDATA;
/**
* Callback for dbgfR3AsSearchPath and dbgfR3AsSearchEnvPath.
*
* @returns VBox status code. If success, then the search is completed.
* @param pszFilename The file name under evaluation.
* @param pvUser The user argument.
*/
typedef int FNDBGFR3ASSEARCHOPEN(const char *pszFilename, void *pvUser);
/** Pointer to a FNDBGFR3ASSEARCHOPEN. */
typedef FNDBGFR3ASSEARCHOPEN *PFNDBGFR3ASSEARCHOPEN;
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Locks the address space database for writing. */
#define DBGF_AS_DB_LOCK_WRITE(pVM) \
do { \
int rcSem = RTSemRWRequestWrite((pVM)->dbgf.s.hAsDbLock, RT_INDEFINITE_WAIT); \
AssertRC(rcSem); \
} while (0)
/** Unlocks the address space database after writing. */
#define DBGF_AS_DB_UNLOCK_WRITE(pVM) \
do { \
int rcSem = RTSemRWReleaseWrite((pVM)->dbgf.s.hAsDbLock); \
AssertRC(rcSem); \
} while (0)
/** Locks the address space database for reading. */
#define DBGF_AS_DB_LOCK_READ(pVM) \
do { \
int rcSem = RTSemRWRequestRead((pVM)->dbgf.s.hAsDbLock, RT_INDEFINITE_WAIT); \
AssertRC(rcSem); \
} while (0)
/** Unlocks the address space database after reading. */
#define DBGF_AS_DB_UNLOCK_READ(pVM) \
do { \
int rcSem = RTSemRWReleaseRead((pVM)->dbgf.s.hAsDbLock); \
AssertRC(rcSem); \
} while (0)
/**
* Initializes the address space parts of DBGF.
*
* @returns VBox status code.
* @param pVM The VM handle.
*/
int dbgfR3AsInit(PVM pVM)
{
/*
* Create the semaphore.
*/
int rc = RTSemRWCreate(&pVM->dbgf.s.hAsDbLock);
AssertRCReturn(rc, rc);
/*
* Create the standard address spaces.
*/
RTDBGAS hDbgAs;
rc = RTDbgAsCreate(&hDbgAs, 0, RTGCPTR_MAX, "Global");
AssertRCReturn(rc, rc);
rc = DBGFR3AsAdd(pVM, hDbgAs, NIL_RTPROCESS);
AssertRCReturn(rc, rc);
RTDbgAsRetain(hDbgAs);
pVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_GLOBAL)] = hDbgAs;
RTDbgAsRetain(hDbgAs);
pVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_KERNEL)] = hDbgAs;
rc = RTDbgAsCreate(&hDbgAs, 0, RTGCPHYS_MAX, "Physical");
AssertRCReturn(rc, rc);
rc = DBGFR3AsAdd(pVM, hDbgAs, NIL_RTPROCESS);
AssertRCReturn(rc, rc);
RTDbgAsRetain(hDbgAs);
pVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_PHYS)] = hDbgAs;
rc = RTDbgAsCreate(&hDbgAs, 0, RTRCPTR_MAX, "HyperRawMode");
AssertRCReturn(rc, rc);
rc = DBGFR3AsAdd(pVM, hDbgAs, NIL_RTPROCESS);
AssertRCReturn(rc, rc);
RTDbgAsRetain(hDbgAs);
pVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_RC)] = hDbgAs;
RTDbgAsRetain(hDbgAs);
pVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_RC_AND_GC_GLOBAL)] = hDbgAs;
rc = RTDbgAsCreate(&hDbgAs, 0, RTR0PTR_MAX, "HyperRing0");
AssertRCReturn(rc, rc);
rc = DBGFR3AsAdd(pVM, hDbgAs, NIL_RTPROCESS);
AssertRCReturn(rc, rc);
RTDbgAsRetain(hDbgAs);
pVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_R0)] = hDbgAs;
return VINF_SUCCESS;
}
/**
* Callback used by dbgfR3AsTerm / RTAvlPVDestroy to release an address space.
*
* @returns 0.
* @param pNode The address space database node.
* @param pvIgnore NULL.
*/
static DECLCALLBACK(int) dbgfR3AsTermDestroyNode(PAVLPVNODECORE pNode, void *pvIgnore)
{
PDBGFASDBNODE pDbNode = (PDBGFASDBNODE)pNode;
RTDbgAsRelease((RTDBGAS)pDbNode->HandleCore.Key);
pDbNode->HandleCore.Key = NIL_RTDBGAS;
/* Don't bother freeing it here as MM will free it soon and MM is much at
it when doing it wholesale instead of piecemeal. */
NOREF(pvIgnore);
return 0;
}
/**
* Terminates the address space parts of DBGF.
*
* @param pVM The VM handle.
*/
void dbgfR3AsTerm(PVM pVM)
{
/*
* Create the semaphore.
*/
int rc = RTSemRWDestroy(pVM->dbgf.s.hAsDbLock);
AssertRC(rc);
pVM->dbgf.s.hAsDbLock = NIL_RTSEMRW;
/*
* Release all the address spaces.
*/
RTAvlPVDestroy(&pVM->dbgf.s.AsHandleTree, dbgfR3AsTermDestroyNode, NULL);
for (size_t i = 0; i < RT_ELEMENTS(pVM->dbgf.s.ahAsAliases); i++)
{
RTDbgAsRelease(pVM->dbgf.s.ahAsAliases[i]);
pVM->dbgf.s.ahAsAliases[i] = NIL_RTDBGAS;
}
}
/**
* Relocates the RC address space.
*
* @param pVM The VM handle.
* @param offDelta The relocation delta.
*/
void dbgfR3AsRelocate(PVM pVM, RTGCUINTPTR offDelta)
{
/** @todo */
}
/**
* Adds the address space to the database.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param hDbgAs The address space handle. The reference of the
* caller will NOT be consumed.
* @param ProcId The process id or NIL_RTPROCESS.
*/
VMMR3DECL(int) DBGFR3AsAdd(PVM pVM, RTDBGAS hDbgAs, RTPROCESS ProcId)
{
/*
* Input validation.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
const char *pszName = RTDbgAsName(hDbgAs);
if (!pszName)
return VERR_INVALID_HANDLE;
uint32_t cRefs = RTDbgAsRetain(hDbgAs);
if (cRefs == UINT32_MAX)
return VERR_INVALID_HANDLE;
/*
* Allocate a tracking node.
*/
int rc = VERR_NO_MEMORY;
PDBGFASDBNODE pDbNode = (PDBGFASDBNODE)MMR3HeapAlloc(pVM, MM_TAG_DBGF_AS, sizeof(*pDbNode));
if (pDbNode)
{
pDbNode->HandleCore.Key = hDbgAs;
pDbNode->PidCore.Key = NIL_RTPROCESS;
pDbNode->NameCore.pszString = pszName;
pDbNode->NameCore.cchString = strlen(pszName);
DBGF_AS_DB_LOCK_WRITE(pVM);
if (RTStrSpaceInsert(&pVM->dbgf.s.AsNameSpace, &pDbNode->NameCore))
{
if (RTAvlPVInsert(&pVM->dbgf.s.AsHandleTree, &pDbNode->HandleCore))
{
DBGF_AS_DB_UNLOCK_WRITE(pVM);
return VINF_SUCCESS;
}
/* bail out */
RTStrSpaceRemove(&pVM->dbgf.s.AsNameSpace, pszName);
}
DBGF_AS_DB_UNLOCK_WRITE(pVM);
MMR3HeapFree(pDbNode);
}
RTDbgAsRelease(hDbgAs);
return rc;
}
/**
* Delete an address space from the database.
*
* The address space must not be engaged as any of the standard aliases.
*
* @returns VBox status code.
* @retval VERR_SHARING_VIOLATION if in use as an alias.
* @retval VERR_NOT_FOUND if not found in the address space database.
*
* @param pVM The VM handle.
* @param hDbgAs The address space handle. Aliases are not allowed.
*/
VMMR3DECL(int) DBGFR3AsDelete(PVM pVM, RTDBGAS hDbgAs)
{
/*
* Input validation. Retain the address space so it can be released outside
* the lock as well as validated.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
if (hDbgAs == NIL_RTDBGAS)
return VINF_SUCCESS;
uint32_t cRefs = RTDbgAsRetain(hDbgAs);
if (cRefs == UINT32_MAX)
return VERR_INVALID_HANDLE;
RTDbgAsRelease(hDbgAs);
DBGF_AS_DB_LOCK_WRITE(pVM);
/*
* You cannot delete any of the aliases.
*/
for (size_t i = 0; i < RT_ELEMENTS(pVM->dbgf.s.ahAsAliases); i++)
if (pVM->dbgf.s.ahAsAliases[i] == hDbgAs)
{
DBGF_AS_DB_UNLOCK_WRITE(pVM);
return VERR_SHARING_VIOLATION;
}
/*
* Ok, try remove it from the database.
*/
PDBGFASDBNODE pDbNode = (PDBGFASDBNODE)RTAvlPVRemove(&pVM->dbgf.s.AsHandleTree, hDbgAs);
if (!pDbNode)
{
DBGF_AS_DB_UNLOCK_WRITE(pVM);
return VERR_NOT_FOUND;
}
RTStrSpaceRemove(&pVM->dbgf.s.AsNameSpace, pDbNode->NameCore.pszString);
if (pDbNode->PidCore.Key != NIL_RTPROCESS)
RTAvlU32Remove(&pVM->dbgf.s.AsPidTree, pDbNode->PidCore.Key);
DBGF_AS_DB_UNLOCK_WRITE(pVM);
/*
* Free the resources.
*/
RTDbgAsRelease(hDbgAs);
MMR3HeapFree(pDbNode);
return VINF_SUCCESS;
}
/**
* Changes an alias to point to a new address space.
*
* Not all the aliases can be changed, currently it's only DBGF_AS_GLOBAL
* and DBGF_AS_KERNEL.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param hAlias The alias to change.
* @param hAliasFor The address space hAlias should be an alias for.
* This can be an alias. The caller's reference to
* this address space will NOT be consumed.
*/
VMMR3DECL(int) DBGFR3AsSetAlias(PVM pVM, RTDBGAS hAlias, RTDBGAS hAliasFor)
{
/*
* Input validation.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertMsgReturn(DBGF_AS_IS_ALIAS(hAlias), ("%p\n", hAlias), VERR_INVALID_PARAMETER);
AssertMsgReturn(!DBGF_AS_IS_FIXED_ALIAS(hAlias), ("%p\n", hAlias), VERR_INVALID_PARAMETER);
RTDBGAS hRealAliasFor = DBGFR3AsResolveAndRetain(pVM, hAlias);
if (hRealAliasFor == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Make sure the handle has is already in the database.
*/
int rc = VERR_NOT_FOUND;
DBGF_AS_DB_LOCK_WRITE(pVM);
if (RTAvlPVGet(&pVM->dbgf.s.AsHandleTree, hRealAliasFor))
{
/*
* Update the alias table and release the current address space.
*/
RTDBGAS hAsOld;
ASMAtomicXchgHandle(&pVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(hAlias)], hRealAliasFor, &hAsOld);
uint32_t cRefs = RTDbgAsRelease(hAsOld);
Assert(cRefs > 0);
Assert(cRefs != UINT32_MAX);
rc = VINF_SUCCESS;
}
DBGF_AS_DB_UNLOCK_WRITE(pVM);
return rc;
}
/**
* Resolves the address space handle into a real handle if it's an alias.
*
* @returns Real address space handle. NIL_RTDBGAS if invalid handle.
*
* @param pVM The VM handle.
* @param hAlias The possibly address space alias.
*
* @remarks Doesn't take any locks.
*/
VMMR3DECL(RTDBGAS) DBGFR3AsResolve(PVM pVM, RTDBGAS hAlias)
{
VM_ASSERT_VALID_EXT_RETURN(pVM, NULL);
AssertCompile(NIL_RTDBGAS == (RTDBGAS)0);
uintptr_t iAlias = DBGF_AS_ALIAS_2_INDEX(hAlias);
if (iAlias < DBGF_AS_COUNT)
ASMAtomicReadHandle(&pVM->dbgf.s.ahAsAliases[iAlias], &hAlias);
return hAlias;
}
/**
* Resolves the address space handle into a real handle if it's an alias,
* and retains whatever it is.
*
* @returns Real address space handle. NIL_RTDBGAS if invalid handle.
*
* @param pVM The VM handle.
* @param hAlias The possibly address space alias.
*/
VMMR3DECL(RTDBGAS) DBGFR3AsResolveAndRetain(PVM pVM, RTDBGAS hAlias)
{
VM_ASSERT_VALID_EXT_RETURN(pVM, NULL);
AssertCompile(NIL_RTDBGAS == (RTDBGAS)0);
uint32_t cRefs;
uintptr_t iAlias = DBGF_AS_ALIAS_2_INDEX(hAlias);
if (iAlias < DBGF_AS_COUNT)
{
if (DBGF_AS_IS_FIXED_ALIAS(hAlias))
{
/* Won't ever change, no need to grab the lock. */
hAlias = pVM->dbgf.s.ahAsAliases[iAlias];
cRefs = RTDbgAsRetain(hAlias);
}
else
{
/* May change, grab the lock so we can read it safely. */
DBGF_AS_DB_LOCK_READ(pVM);
hAlias = pVM->dbgf.s.ahAsAliases[iAlias];
cRefs = RTDbgAsRetain(hAlias);
DBGF_AS_DB_UNLOCK_READ(pVM);
}
}
else
/* Not an alias, just retain it. */
cRefs = RTDbgAsRetain(hAlias);
return cRefs != UINT32_MAX ? hAlias : NIL_RTDBGAS;
}
/**
* Query an address space by name.
*
* @returns Retained address space handle if found, NIL_RTDBGAS if not.
*
* @param pVM The VM handle.
* @param pszName The name.
*/
VMMR3DECL(RTDBGAS) DBGFR3AsQueryByName(PVM pVM, const char *pszName)
{
/*
* Validate the input.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, NIL_RTDBGAS);
AssertPtrReturn(pszName, NIL_RTDBGAS);
AssertReturn(*pszName, NIL_RTDBGAS);
/*
* Look it up in the string space and retain the result.
*/
RTDBGAS hDbgAs = NIL_RTDBGAS;
DBGF_AS_DB_LOCK_READ(pVM);
PRTSTRSPACECORE pNode = RTStrSpaceGet(&pVM->dbgf.s.AsNameSpace, pszName);
if (pNode)
{
PDBGFASDBNODE pDbNode = RT_FROM_MEMBER(pNode, DBGFASDBNODE, NameCore);
hDbgAs = (RTDBGAS)pDbNode->HandleCore.Key;
uint32_t cRefs = RTDbgAsRetain(hDbgAs);
if (RT_UNLIKELY(cRefs == UINT32_MAX))
hDbgAs = NIL_RTDBGAS;
}
DBGF_AS_DB_UNLOCK_READ(pVM);
return hDbgAs;
}
/**
* Query an address space by process ID.
*
* @returns Retained address space handle if found, NIL_RTDBGAS if not.
*
* @param pVM The VM handle.
* @param ProcId The process ID.
*/
VMMR3DECL(RTDBGAS) DBGFR3AsQueryByPid(PVM pVM, RTPROCESS ProcId)
{
/*
* Validate the input.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, NIL_RTDBGAS);
AssertReturn(ProcId != NIL_RTPROCESS, NIL_RTDBGAS);
/*
* Look it up in the PID tree and retain the result.
*/
RTDBGAS hDbgAs = NIL_RTDBGAS;
DBGF_AS_DB_LOCK_READ(pVM);
PAVLU32NODECORE pNode = RTAvlU32Get(&pVM->dbgf.s.AsPidTree, ProcId);
if (pNode)
{
PDBGFASDBNODE pDbNode = RT_FROM_MEMBER(pNode, DBGFASDBNODE, PidCore);
hDbgAs = (RTDBGAS)pDbNode->HandleCore.Key;
uint32_t cRefs = RTDbgAsRetain(hDbgAs);
if (RT_UNLIKELY(cRefs == UINT32_MAX))
hDbgAs = NIL_RTDBGAS;
}
DBGF_AS_DB_UNLOCK_READ(pVM);
return hDbgAs;
}
/**
* Searches for the file in the path.
*
* The file is first tested without any path modification, then we walk the path
* looking in each directory.
*
* @returns VBox status code.
* @param pszFilename The file to search for.
* @param pszPath The search path.
* @param pfnOpen The open callback function.
* @param pvUser User argument for the callback.
*/
static int dbgfR3AsSearchPath(const char *pszFilename, const char *pszPath, PFNDBGFR3ASSEARCHOPEN pfnOpen, void *pvUser)
{
char szFound[RTPATH_MAX];
/* Check the filename length. */
size_t const cchFilename = strlen(pszFilename);
if (cchFilename >= sizeof(szFound))
return VERR_FILENAME_TOO_LONG;
const char *pszName = RTPathFilename(pszFilename);
if (!pszName)
return VERR_IS_A_DIRECTORY;
size_t const cchName = strlen(pszName);
/*
* Try default location first.
*/
memcpy(szFound, pszFilename, cchFilename + 1);
int rc = pfnOpen(szFound, pvUser);
if (RT_SUCCESS(rc))
return rc;
/*
* Walk the search path.
*/
const char *psz = pszPath;
while (*psz)
{
/* Skip leading blanks - no directories with leading spaces, thank you. */
while (RT_C_IS_BLANK(*psz))
psz++;
/* Find the end of this element. */
const char *pszNext;
const char *pszEnd = strchr(psz, ';');
if (!pszEnd)
pszEnd = pszNext = strchr(psz, '\0');
else
pszNext = pszEnd + 1;
if (pszEnd != psz)
{
size_t const cch = pszEnd - psz;
if (cch + 1 + cchName < sizeof(szFound))
{
/** @todo RTPathCompose, RTPathComposeN(). This code isn't right
* for 'E:' on DOS systems. It may also create unwanted double slashes. */
memcpy(szFound, psz, cch);
szFound[cch] = '/';
memcpy(szFound + cch + 1, pszName, cchName + 1);
int rc2 = pfnOpen(szFound, pvUser);
if (RT_SUCCESS(rc2))
return rc2;
if ( rc2 != rc
&& ( rc == VERR_FILE_NOT_FOUND
|| rc == VERR_OPEN_FAILED))
rc = rc2;
}
}
/* advance */
psz = pszNext;
}
/*
* Walk the path once again, this time do a depth search.
*/
/** @todo do a depth search using the specified path. */
/* failed */
return rc;
}
/**
* Same as dbgfR3AsSearchEnv, except that the path is taken from the environment.
*
* It the environment variable doesn't exist, the current directory is searched instead.
*
* @returns VBox status code.
* @param pszFilename The filename.
* @param pszEnvVar The environment variable name.
* @param pfnOpen The open callback function.
* @param pvUser User argument for the callback.
*/
static int dbgfR3AsSearchEnvPath(const char *pszFilename, const char *pszEnvVar, PFNDBGFR3ASSEARCHOPEN pfnOpen, void *pvUser)
{
int rc;
char *pszPath = RTEnvDupEx(RTENV_DEFAULT, pszEnvVar);
if (pszPath)
{
rc = dbgfR3AsSearchPath(pszFilename, pszPath, pfnOpen, pvUser);
RTStrFree(pszPath);
}
else
rc = dbgfR3AsSearchPath(pszFilename, ".", pfnOpen, pvUser);
return rc;
}
/**
* Callback function used by DBGFR3AsLoadImage.
*
* @returns VBox status code.
* @param pszFilename The filename under evaluation.
* @param pvUser Use arguments (DBGFR3ASLOADOPENDATA).
*/
static DECLCALLBACK(int) dbgfR3AsLoadImageOpen(const char *pszFilename, void *pvUser)
{
DBGFR3ASLOADOPENDATA *pData = (DBGFR3ASLOADOPENDATA *)pvUser;
return RTDbgModCreateFromImage(&pData->hMod, pszFilename, pData->pszModName, pData->fFlags);
}
/**
* Load symbols from an executable module into the specified address space.
*
* If an module exist at the specified address it will be replaced by this
* call, otherwise a new module is created.
*
* @returns VBox status code.
*
* @param pVM The VM handle.
* @param hDbgAs The address space.
* @param pszFilename The filename of the executable module.
* @param pszModName The module name. If NULL, then then the file name
* base is used (no extension or nothing).
* @param pModAddress The load address of the module.
* @param iModSeg The segment to load, pass NIL_RTDBGSEGIDX to load
* the whole image.
* @param fFlags Flags reserved for future extensions, must be 0.
*/
VMMR3DECL(int) DBGFR3AsLoadImage(PVM pVM, RTDBGAS hDbgAs, const char *pszFilename, const char *pszModName, PCDBGFADDRESS pModAddress, RTDBGSEGIDX iModSeg, uint32_t fFlags)
{
/*
* Validate input
*/
AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
AssertReturn(DBGFR3AddrIsValid(pVM, pModAddress), VERR_INVALID_PARAMETER);
AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pVM, hDbgAs);
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Do the work.
*/
DBGFR3ASLOADOPENDATA Data;
Data.pszModName = pszModName;
Data.uSubtrahend = 0;
Data.fFlags = 0;
Data.hMod = NIL_RTDBGMOD;
int rc = dbgfR3AsSearchEnvPath(pszFilename, "VBOXDBG_IMAGE_PATH", dbgfR3AsLoadImageOpen, &Data);
if (RT_FAILURE(rc))
rc = dbgfR3AsSearchEnvPath(pszFilename, "VBOXDBG_PATH", dbgfR3AsLoadImageOpen, &Data);
if (RT_SUCCESS(rc))
{
rc = DBGFR3AsLinkModule(pVM, hRealAS, Data.hMod, pModAddress, iModSeg, 0);
if (RT_FAILURE(rc))
RTDbgModRelease(Data.hMod);
}
RTDbgAsRelease(hRealAS);
return rc;
}
/**
* Callback function used by DBGFR3AsLoadMap.
*
* @returns VBox status code.
* @param pszFilename The filename under evaluation.
* @param pvUser Use arguments (DBGFR3ASLOADOPENDATA).
*/
static DECLCALLBACK(int) dbgfR3AsLoadMapOpen(const char *pszFilename, void *pvUser)
{
DBGFR3ASLOADOPENDATA *pData = (DBGFR3ASLOADOPENDATA *)pvUser;
return RTDbgModCreateFromMap(&pData->hMod, pszFilename, pData->pszModName, pData->uSubtrahend, pData->fFlags);
}
/**
* Load symbols from a map file into a module at the specified address space.
*
* If an module exist at the specified address it will be replaced by this
* call, otherwise a new module is created.
*
* @returns VBox status code.
*
* @param pVM The VM handle.
* @param hDbgAs The address space.
* @param pszFilename The map file.
* @param pszModName The module name. If NULL, then then the file name
* base is used (no extension or nothing).
* @param pModAddress The load address of the module.
* @param iModSeg The segment to load, pass NIL_RTDBGSEGIDX to load
* the whole image.
* @param uSubtrahend Value to to subtract from the symbols in the map
* file. This is useful for the linux System.map and
* /proc/kallsyms.
* @param fFlags Flags reserved for future extensions, must be 0.
*/
VMMR3DECL(int) DBGFR3AsLoadMap(PVM pVM, RTDBGAS hDbgAs, const char *pszFilename, const char *pszModName,
PCDBGFADDRESS pModAddress, RTDBGSEGIDX iModSeg, RTGCUINTPTR uSubtrahend, uint32_t fFlags)
{
/*
* Validate input
*/
AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
AssertReturn(DBGFR3AddrIsValid(pVM, pModAddress), VERR_INVALID_PARAMETER);
AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pVM, hDbgAs);
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Do the work.
*/
DBGFR3ASLOADOPENDATA Data;
Data.pszModName = pszModName;
Data.uSubtrahend = uSubtrahend;
Data.fFlags = 0;
Data.hMod = NIL_RTDBGMOD;
int rc = dbgfR3AsSearchEnvPath(pszFilename, "VBOXDBG_MAP_PATH", dbgfR3AsLoadMapOpen, &Data);
if (RT_FAILURE(rc))
rc = dbgfR3AsSearchEnvPath(pszFilename, "VBOXDBG_PATH", dbgfR3AsLoadMapOpen, &Data);
if (RT_SUCCESS(rc))
{
rc = DBGFR3AsLinkModule(pVM, hRealAS, Data.hMod, pModAddress, iModSeg, 0);
if (RT_FAILURE(rc))
RTDbgModRelease(Data.hMod);
}
RTDbgAsRelease(hRealAS);
return rc;
}
/**
* Wrapper around RTDbgAsModuleLink, RTDbgAsModuleLinkSeg and DBGFR3AsResolve.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param hDbgAs The address space handle.
* @param hMod The module handle.
* @param pModAddress The link address.
* @param iModSeg The segment to link, NIL_RTDBGSEGIDX for the entire image.
* @param fFlags Flags to pass to the link functions, see RTDBGASLINK_FLAGS_*.
*/
VMMR3DECL(int) DBGFR3AsLinkModule(PVM pVM, RTDBGAS hDbgAs, RTDBGMOD hMod, PCDBGFADDRESS pModAddress, RTDBGSEGIDX iModSeg, uint32_t fFlags)
{
/*
* Input validation.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertReturn(DBGFR3AddrIsValid(pVM, pModAddress), VERR_INVALID_PARAMETER);
RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pVM, hDbgAs);
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Do the job.
*/
int rc;
if (iModSeg == NIL_RTDBGSEGIDX)
rc = RTDbgAsModuleLink(hRealAS, hMod, pModAddress->FlatPtr, fFlags);
else
rc = RTDbgAsModuleLinkSeg(hRealAS, hMod, iModSeg, pModAddress->FlatPtr, fFlags);
RTDbgAsRelease(hRealAS);
return rc;
}
/**
* Adds the module name to the symbol name.
*
* @param pSymbol The symbol info (in/out).
* @param hMod The module handle.
*/
static void dbgfR3AsSymbolJoinNames(PRTDBGSYMBOL pSymbol, RTDBGMOD hMod)
{
/* Figure the lengths, adjust them if the result is too long. */
const char *pszModName = RTDbgModName(hMod);
size_t cchModName = strlen(pszModName);
size_t cchSymbol = strlen(pSymbol->szName);
if (cchModName + 1 + cchSymbol >= sizeof(pSymbol->szName))
{
if (cchModName >= sizeof(pSymbol->szName) / 4)
cchModName = sizeof(pSymbol->szName) / 4;
if (cchModName + 1 + cchSymbol >= sizeof(pSymbol->szName))
cchSymbol = sizeof(pSymbol->szName) - cchModName - 2;
Assert(cchModName + 1 + cchSymbol < sizeof(pSymbol->szName));
}
/* Do the moving and copying. */
memmove(&pSymbol->szName[cchModName + 1], &pSymbol->szName[0], cchSymbol + 1);
memcpy(&pSymbol->szName[0], pszModName, cchModName);
pSymbol->szName[cchModName] = '!';
}
/** Temporary symbol conversion function. */
static void dbgfR3AsSymbolConvert(PRTDBGSYMBOL pSymbol, PCDBGFSYMBOL pDbgfSym)
{
pSymbol->offSeg = pSymbol->Value = pDbgfSym->Value;
pSymbol->cb = pDbgfSym->cb;
pSymbol->iSeg = 0;
pSymbol->fFlags = 0;
pSymbol->iOrdinal = UINT32_MAX;
strcpy(pSymbol->szName, pDbgfSym->szName);
}
/**
* Query a symbol by address.
*
* The returned symbol is the one we consider closes to the specified address.
*
* @returns VBox status code. See RTDbgAsSymbolByAddr.
*
* @param pVM The VM handle.
* @param hDbgAs The address space handle.
* @param pAddress The address to lookup.
* @param poffDisp Where to return the distance between the
* returned symbol and pAddress. Optional.
* @param pSymbol Where to return the symbol information.
* The returned symbol name will be prefixed by
* the module name as far as space allows.
* @param phMod Where to return the module handle. Optional.
*/
VMMR3DECL(int) DBGFR3AsSymbolByAddr(PVM pVM, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress,
PRTGCINTPTR poffDisp, PRTDBGSYMBOL pSymbol, PRTDBGMOD phMod)
{
/*
* Implement the special address space aliases the lazy way.
*/
if (hDbgAs == DBGF_AS_RC_AND_GC_GLOBAL)
{
int rc = DBGFR3AsSymbolByAddr(pVM, DBGF_AS_RC, pAddress, poffDisp, pSymbol, phMod);
if (RT_FAILURE(rc))
rc = DBGFR3AsSymbolByAddr(pVM, DBGF_AS_GLOBAL, pAddress, poffDisp, pSymbol, phMod);
return rc;
}
/*
* Input validation.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertReturn(DBGFR3AddrIsValid(pVM, pAddress), VERR_INVALID_PARAMETER);
AssertPtrNullReturn(poffDisp, VERR_INVALID_POINTER);
AssertPtrReturn(pSymbol, VERR_INVALID_POINTER);
AssertPtrNullReturn(phMod, VERR_INVALID_POINTER);
if (poffDisp)
*poffDisp = 0;
if (phMod)
*phMod = NIL_RTDBGMOD;
RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pVM, hDbgAs);
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Do the lookup.
*/
RTDBGMOD hMod;
int rc = RTDbgAsSymbolByAddr(hRealAS, pAddress->FlatPtr, poffDisp, pSymbol, &hMod);
if (RT_SUCCESS(rc))
{
dbgfR3AsSymbolJoinNames(pSymbol, hMod);
if (!phMod)
RTDbgModRelease(hMod);
}
/* Temporary conversion. */
else if (hDbgAs == DBGF_AS_GLOBAL)
{
DBGFSYMBOL DbgfSym;
rc = DBGFR3SymbolByAddr(pVM, pAddress->FlatPtr, poffDisp, &DbgfSym);
if (RT_SUCCESS(rc))
dbgfR3AsSymbolConvert(pSymbol, &DbgfSym);
}
return rc;
}
/**
* Convenience function that combines RTDbgSymbolDup and DBGFR3AsSymbolByAddr.
*
* @returns Pointer to the symbol on success. This must be free using
* RTDbgSymbolFree(). NULL is returned if not found or any error
* occurs.
*
* @param pVM The VM handle.
* @param hDbgAs See DBGFR3AsSymbolByAddr.
* @param pAddress See DBGFR3AsSymbolByAddr.
* @param poffDisp See DBGFR3AsSymbolByAddr.
* @param phMod See DBGFR3AsSymbolByAddr.
*/
VMMR3DECL(PRTDBGSYMBOL) DBGFR3AsSymbolByAddrA(PVM pVM, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress, PRTGCINTPTR poffDisp, PRTDBGMOD phMod)
{
RTDBGSYMBOL SymInfo;
int rc = DBGFR3AsSymbolByAddr(pVM, hDbgAs, pAddress, poffDisp, &SymInfo, phMod);
if (RT_SUCCESS(rc))
return RTDbgSymbolDup(&SymInfo);
return NULL;
}
/**
* Query a symbol by name.
*
* The symbol can be prefixed by a module name pattern to scope the search. The
* pattern is a simple string pattern with '*' and '?' as wild chars. See
* RTStrSimplePatternMatch().
*
* @returns VBox status code. See RTDbgAsSymbolByAddr.
*
* @param pVM The VM handle.
* @param hDbgAs The address space handle.
* @param pszSymbol The symbol to search for, maybe prefixed by a
* module pattern.
* @param pSymbol Where to return the symbol information.
* The returned symbol name will be prefixed by
* the module name as far as space allows.
* @param phMod Where to return the module handle. Optional.
*/
VMMR3DECL(int) DBGFR3AsSymbolByName(PVM pVM, RTDBGAS hDbgAs, const char *pszSymbol,
PRTDBGSYMBOL pSymbol, PRTDBGMOD phMod)
{
/*
* Implement the special address space aliases the lazy way.
*/
if (hDbgAs == DBGF_AS_RC_AND_GC_GLOBAL)
{
int rc = DBGFR3AsSymbolByName(pVM, DBGF_AS_RC, pszSymbol, pSymbol, phMod);
if (RT_FAILURE(rc))
rc = DBGFR3AsSymbolByName(pVM, DBGF_AS_GLOBAL, pszSymbol, pSymbol, phMod);
return rc;
}
/*
* Input validation.
*/
VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
AssertPtrReturn(pSymbol, VERR_INVALID_POINTER);
AssertPtrNullReturn(phMod, VERR_INVALID_POINTER);
if (phMod)
*phMod = NIL_RTDBGMOD;
RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pVM, hDbgAs);
if (hRealAS == NIL_RTDBGAS)
return VERR_INVALID_HANDLE;
/*
* Do the lookup.
*/
RTDBGMOD hMod;
int rc = RTDbgAsSymbolByName(hRealAS, pszSymbol, pSymbol, &hMod);
if (RT_SUCCESS(rc))
{
dbgfR3AsSymbolJoinNames(pSymbol, hMod);
if (!phMod)
RTDbgModRelease(hMod);
}
/* Temporary conversion. */
else if (hDbgAs == DBGF_AS_GLOBAL)
{
DBGFSYMBOL DbgfSym;
rc = DBGFR3SymbolByName(pVM, pszSymbol, &DbgfSym);
if (RT_SUCCESS(rc))
dbgfR3AsSymbolConvert(pSymbol, &DbgfSym);
}
return rc;
}
|