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