1 | /* $Id: kDbgSpace.cpp 3556 2007-08-26 10:43:57Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kDbg - The Debug Info Reader, Address Space Manager.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
8 | *
|
---|
9 | * This file is part of kStuff.
|
---|
10 | *
|
---|
11 | * kStuff is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU Lesser General Public
|
---|
13 | * License as published by the Free Software Foundation; either
|
---|
14 | * version 2.1 of the License, or (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kStuff is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * Lesser General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU Lesser General Public
|
---|
22 | * License along with kStuff; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include "kDbgInternal.h"
|
---|
31 | #include <k/kHlpAlloc.h>
|
---|
32 | #include <k/kHlpString.h>
|
---|
33 | #include <k/kAvl.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | /*******************************************************************************
|
---|
37 | * Structures and Typedefs *
|
---|
38 | *******************************************************************************/
|
---|
39 | /** Pointer to a name space module. */
|
---|
40 | typedef struct KDBGSPACEMOD *PKDBGSPACEMOD;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Tracks a module segment in the address space.
|
---|
44 | *
|
---|
45 | * These segments are organized in two trees, by address in the
|
---|
46 | * KDBGSPACE::pSegRoot tree and by selector value in the
|
---|
47 | * KDBGSPACE::pSegSelRoot tree.
|
---|
48 | *
|
---|
49 | * While the debug module reader could easily provide us with
|
---|
50 | * segment names and it could perhaps be interesting to lookup
|
---|
51 | * a segment by its name in some situations, this has been
|
---|
52 | * considered too much bother for now. :-)
|
---|
53 | */
|
---|
54 | typedef struct KDBGSPACESEG
|
---|
55 | {
|
---|
56 | /** The module segment index. */
|
---|
57 | KI32 iSegment;
|
---|
58 | /** The address space module structure this segment belongs to. */
|
---|
59 | PKDBGSPACEMOD pSpaceMod;
|
---|
60 | } KDBGSPACESEG;
|
---|
61 | typedef KDBGSPACESEG *PKDBGSPACESEG;
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Track a module in the name space.
|
---|
66 | *
|
---|
67 | * Each module in the address space can be addressed efficiently
|
---|
68 | * by module name. The module name has to be unique.
|
---|
69 | */
|
---|
70 | typedef struct KDBGSPACEMOD
|
---|
71 | {
|
---|
72 | /** The module name hash. */
|
---|
73 | KU32 u32Hash;
|
---|
74 | /** The length of the module name. */
|
---|
75 | KU32 cchName;
|
---|
76 | /** The module name. */
|
---|
77 | char *pszName;
|
---|
78 | /** The next module in the same bucket. */
|
---|
79 | PKDBGSPACEMOD pNext;
|
---|
80 | /** Pointer to the debug module reader. */
|
---|
81 | PKDBGMOD pMod;
|
---|
82 | /** The number of segments. */
|
---|
83 | KU32 cSegs;
|
---|
84 | /** The segment array. (variable length) */
|
---|
85 | KDBGSPACESEG aSegs[1];
|
---|
86 | } KDBGSPACEMOD;
|
---|
87 |
|
---|
88 |
|
---|
89 | typedef struct KDBGCACHEDSYM *PKDBGCACHEDSYM;
|
---|
90 | /**
|
---|
91 | * A cached symbol.
|
---|
92 | */
|
---|
93 | typedef struct KDBGCACHEDSYM
|
---|
94 | {
|
---|
95 | /** The symbol name hash. */
|
---|
96 | KU32 u32Hash;
|
---|
97 | /** The next symbol in the same bucket. */
|
---|
98 | PKDBGCACHEDSYM pNext;
|
---|
99 | /** The next symbol belonging to the same module as this. */
|
---|
100 | PKDBGCACHEDSYM pNextMod;
|
---|
101 | /** The cached symbol information. */
|
---|
102 | KDBGSYMBOL Sym;
|
---|
103 | } KDBGCACHEDSYM;
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * A symbol cache.
|
---|
108 | */
|
---|
109 | typedef struct KDBGSYMCACHE
|
---|
110 | {
|
---|
111 | /** The symbol cache magic. (KDBGSYMCACHE_MAGIC) */
|
---|
112 | KU32 u32Magic;
|
---|
113 | /** The maximum number of symbols.*/
|
---|
114 | KU32 cMax;
|
---|
115 | /** The current number of symbols.*/
|
---|
116 | KU32 cCur;
|
---|
117 | /** The number of hash buckets. */
|
---|
118 | KU32 cBuckets;
|
---|
119 | /** The address lookup tree. */
|
---|
120 | PKDBGADDRAVL pAddrTree;
|
---|
121 | /** Array of hash buckets.
|
---|
122 | * The size is selected according to the cache size. */
|
---|
123 | PKDBGCACHEDSYM *paBuckets[1];
|
---|
124 | } KDBGSYMCACHE;
|
---|
125 | typedef KDBGSYMCACHE *PKDBGSYMCACHE;
|
---|
126 |
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * A user symbol record.
|
---|
130 | *
|
---|
131 | * The user symbols are organized in the KDBGSPACE::pUserRoot tree
|
---|
132 | * and form an overlay that overrides the debug info retrieved from
|
---|
133 | * the KDBGSPACE::pSegRoot tree.
|
---|
134 | *
|
---|
135 | * In the current implementation the user symbols are unique and
|
---|
136 | * one would have to first delete a symbol in order to add another
|
---|
137 | * at the same address. This may be changed later, perhaps.
|
---|
138 | */
|
---|
139 | typedef struct KDBGSPACEUSERSYM
|
---|
140 | {
|
---|
141 |
|
---|
142 | } KDBGSPACEUSERSYM;
|
---|
143 | typedef KDBGSPACEUSERSYM *PKDBGSPACEUSERSYM;
|
---|
144 |
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Address space.
|
---|
149 | */
|
---|
150 | typedef struct KDBGSPACE
|
---|
151 | {
|
---|
152 | /** The addresspace magic. (KDBGSPACE_MAGIC) */
|
---|
153 | KU32 u32Magic;
|
---|
154 | /** User defined address space identifier or data pointer. */
|
---|
155 | KUPTR uUser;
|
---|
156 | /** The name of the address space. (Optional) */
|
---|
157 | const char *pszName;
|
---|
158 |
|
---|
159 |
|
---|
160 | } KDBGSPACE;
|
---|
161 | /** Pointer to an address space. */
|
---|
162 | typedef struct KDBGSPACE *PKDBGSPACE;
|
---|
163 | /** Pointer to an address space pointer. */
|
---|
164 | typedef PKDBGSPACE *PPKDBGSPACE;
|
---|
165 |
|
---|
166 |
|
---|
167 | KDBG_DECL(int) kDbgSpaceCreate(PPDBGSPACE ppSpace, KDBGADDR LowAddr, DBGADDR HighAddr,
|
---|
168 | KUPTR uUser, const char *pszName)
|
---|
169 | {
|
---|
170 | /*
|
---|
171 | * Validate input.
|
---|
172 | */
|
---|
173 | kDbgAssertPtrReturn(ppSpace);
|
---|
174 | *ppSpace = NULL;
|
---|
175 | kDbgAssertPtrNullReturn(pszName);
|
---|
176 | kDbgAssertReturn(LowAddr < HighAddr);
|
---|
177 |
|
---|
178 | /*
|
---|
179 | * Create and initialize the address space.
|
---|
180 | */
|
---|
181 | PKDBGSPACE pSpace = (PKDBGSPACE)kHlpAlloc(sizeof(*pSpace));
|
---|
182 | if (!pSpace)
|
---|
183 | return KERR_NO_MEMORY;
|
---|
184 | pSpace->u32Magic = KDBGSPACE_MAGIC;
|
---|
185 | pSpace->uUser = uUser;
|
---|
186 | pSpace->pszName = pszName;
|
---|
187 |
|
---|
188 | }
|
---|