1 /* $Id: kDbgSpace.cpp 29 2009-07-01 20:30:29Z bird $ */
2 /** @file
3  * kDbg - The Debug Info Reader, Address Space Manager.
4  */
5 
6 /*
7  * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
8  *
9  * Permission is hereby granted, free of charge, to any person
10  * obtaining a copy of this software and associated documentation
11  * files (the "Software"), to deal in the Software without
12  * restriction, including without limitation the rights to use,
13  * copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following
16  * conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  */
30 
31 /*******************************************************************************
32 *   Header Files                                                               *
33 *******************************************************************************/
34 #include "kDbgInternal.h"
35 #include <k/kHlpAlloc.h>
36 #include <k/kHlpString.h>
37 #include <k/kAvl.h>
38 
39 
40 /*******************************************************************************
41 *   Structures and Typedefs                                                    *
42 *******************************************************************************/
43 /** Pointer to a name space module. */
44 typedef struct KDBGSPACEMOD *PKDBGSPACEMOD;
45 
46 /**
47  * Tracks a module segment in the address space.
48  *
49  * These segments are organized in two trees, by address in the
50  * KDBGSPACE::pSegRoot tree and by selector value in the
51  * KDBGSPACE::pSegSelRoot tree.
52  *
53  * While the debug module reader could easily provide us with
54  * segment names and it could perhaps be interesting to lookup
55  * a segment by its name in some situations, this has been
56  * considered too much bother for now. :-)
57  */
58 typedef struct KDBGSPACESEG
59 {
60     /** The module segment index. */
61     KI32                    iSegment;
62     /** The address space module structure this segment belongs to. */
63     PKDBGSPACEMOD           pSpaceMod;
64 } KDBGSPACESEG;
65 typedef KDBGSPACESEG *PKDBGSPACESEG;
66 
67 
68 /**
69  * Track a module in the name space.
70  *
71  * Each module in the address space can be addressed efficiently
72  * by module name. The module name has to be unique.
73  */
74 typedef struct KDBGSPACEMOD
75 {
76     /** The module name hash. */
77     KU32                    u32Hash;
78     /** The length of the module name. */
79     KU32                    cchName;
80     /** The module name. */
81     char                   *pszName;
82     /** The next module in the same bucket. */
83     PKDBGSPACEMOD           pNext;
84     /** Pointer to the debug module reader. */
85     PKDBGMOD                pMod;
86     /** The number of segments. */
87     KU32                    cSegs;
88     /** The segment array. (variable length) */
89     KDBGSPACESEG            aSegs[1];
90 } KDBGSPACEMOD;
91 
92 
93 typedef struct KDBGCACHEDSYM *PKDBGCACHEDSYM;
94 /**
95  * A cached symbol.
96  */
97 typedef struct KDBGCACHEDSYM
98 {
99     /** The symbol name hash. */
100     KU32                    u32Hash;
101     /** The next symbol in the same bucket. */
102     PKDBGCACHEDSYM          pNext;
103     /** The next symbol belonging to the same module as this. */
104     PKDBGCACHEDSYM          pNextMod;
105     /** The cached symbol information. */
106     KDBGSYMBOL              Sym;
107 } KDBGCACHEDSYM;
108 
109 
110 /**
111  * A symbol cache.
112  */
113 typedef struct KDBGSYMCACHE
114 {
115     /** The symbol cache magic. (KDBGSYMCACHE_MAGIC) */
116     KU32                    u32Magic;
117     /** The maximum number of symbols.*/
118     KU32                    cMax;
119     /** The current number of symbols.*/
120     KU32                    cCur;
121     /** The number of hash buckets. */
122     KU32                    cBuckets;
123     /** The address lookup tree. */
124     PKDBGADDRAVL            pAddrTree;
125     /** Array of hash buckets.
126      * The size is selected according to the cache size. */
127     PKDBGCACHEDSYM         *paBuckets[1];
128 } KDBGSYMCACHE;
129 typedef KDBGSYMCACHE *PKDBGSYMCACHE;
130 
131 
132 /**
133  * A user symbol record.
134  *
135  * The user symbols are organized in the KDBGSPACE::pUserRoot tree
136  * and form an overlay that overrides the debug info retrieved from
137  * the KDBGSPACE::pSegRoot tree.
138  *
139  * In the current implementation the user symbols are unique and
140  * one would have to first delete a symbol in order to add another
141  * at the same address. This may be changed later, perhaps.
142  */
143 typedef struct KDBGSPACEUSERSYM
144 {
145 
146 } KDBGSPACEUSERSYM;
147 typedef KDBGSPACEUSERSYM *PKDBGSPACEUSERSYM;
148 
149 
150 
151 /**
152  * Address space.
153  */
154 typedef struct KDBGSPACE
155 {
156     /** The addresspace magic. (KDBGSPACE_MAGIC) */
157     KU32            u32Magic;
158     /** User defined address space identifier or data pointer. */
159     KUPTR           uUser;
160     /** The name of the address space. (Optional) */
161     const char     *pszName;
162 
163 
164 } KDBGSPACE;
165 /** Pointer to an address space. */
166 typedef struct KDBGSPACE *PKDBGSPACE;
167 /** Pointer to an address space pointer. */
168 typedef PKDBGSPACE *PPKDBGSPACE;
169 
170 
kDbgSpaceCreate(PPDBGSPACE ppSpace,KDBGADDR LowAddr,DBGADDR HighAddr,KUPTR uUser,const char * pszName)171 KDBG_DECL(int) kDbgSpaceCreate(PPDBGSPACE ppSpace, KDBGADDR LowAddr, DBGADDR HighAddr,
172                                KUPTR uUser, const char *pszName)
173 {
174     /*
175      * Validate input.
176      */
177     kDbgAssertPtrReturn(ppSpace);
178     *ppSpace = NULL;
179     kDbgAssertPtrNullReturn(pszName);
180     kDbgAssertReturn(LowAddr < HighAddr);
181 
182     /*
183      * Create and initialize the address space.
184      */
185     PKDBGSPACE pSpace = (PKDBGSPACE)kHlpAlloc(sizeof(*pSpace));
186     if (!pSpace)
187         return KERR_NO_MEMORY;
188     pSpace->u32Magic = KDBGSPACE_MAGIC;
189     pSpace->uUser = uUser;
190     pSpace->pszName = pszName;
191 
192 }
193