| 1 | // |
| 2 | // DBGManager.cpp: Debugger information manager |
| 3 | // |
| 4 | // by Jean-Paul Mari |
| 5 | // |
| 6 | // JPM = Jean-Paul Mari <djipi.mari@gmail.com> |
| 7 | // |
| 8 | // WHO WHEN WHAT |
| 9 | // --- ---------- ------------------------------------------------------------ |
| 10 | // JPM 12/21/2016 Created this file |
| 11 | // JPM Various efforts to set the ELF format support |
| 12 | // JPM Various efforts to set the DWARF format support |
| 13 | |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <stdint.h> |
| 17 | #include "libelf/libelf.h" |
| 18 | #include "libelf/gelf.h" |
| 19 | #include "log.h" |
| 20 | #include "ELFManager.h" |
| 21 | #include "DwarfManager.h" |
| 22 | #include "DBGManager.h" |
| 23 | #include "HWLABELManager.h" |
| 24 | #include "settings.h" |
| 25 | #include "memory.h" |
| 26 | |
| 27 | |
| 28 | // |
| 29 | char *DBGManager_GetVariableValueFromAdr(uint32_t Adr, uint32_t TypeEncoding, uint32_t TypeByteSize); |
| 30 | |
| 31 | |
| 32 | // |
| 33 | struct Value |
| 34 | { |
| 35 | union |
| 36 | { |
| 37 | char C[10]; |
| 38 | double D; |
| 39 | float F; |
| 40 | int32_t SI; |
| 41 | int64_t SL; |
| 42 | uint32_t UI; |
| 43 | uint64_t UL; |
| 44 | }; |
| 45 | }S_Value; |
| 46 | |
| 47 | |
| 48 | // Common debugger variables |
| 49 | int DBGType; |
| 50 | char value[1000]; |
| 51 | |
| 52 | |
| 53 | // Common debugger initialisation |
| 54 | void DBGManager_Init(void) |
| 55 | { |
| 56 | DBGType = DBG_NO_TYPE; |
| 57 | ELFManager_Init(); |
| 58 | DWARFManager_Init(); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | // Common debugger reset |
| 63 | void DBGManager_Reset(void) |
| 64 | { |
| 65 | if ((DBGType & DBG_DWARF)) |
| 66 | { |
| 67 | DWARFManager_Reset(); |
| 68 | } |
| 69 | |
| 70 | if ((DBGType & DBG_ELF)) |
| 71 | { |
| 72 | ELFManager_Reset(); |
| 73 | } |
| 74 | |
| 75 | DBGType = vjs.displayHWlabels ? DBG_HWLABEL : DBG_NO_TYPE; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | // Common debugger set |
| 80 | void DBGManager_SetType(int DBGTypeSet) |
| 81 | { |
| 82 | DBGType |= DBGTypeSet; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | // Common debugger close |
| 87 | void DBGManager_Close(void) |
| 88 | { |
| 89 | if ((DBGType & DBG_DWARF)) |
| 90 | { |
| 91 | DWARFManager_Close(); |
| 92 | } |
| 93 | |
| 94 | if ((DBGType & DBG_ELF)) |
| 95 | { |
| 96 | ELFManager_Close(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
| 101 | // Get source filename based on the memeory address |
| 102 | // return NULL if no source filename |
| 103 | char *DBGManager_GetFullSourceFilenameFromAdr(size_t Adr, bool *Error) |
| 104 | { |
| 105 | if ((DBGType & DBG_ELFDWARF)) |
| 106 | { |
| 107 | return DWARFManager_GetFullSourceFilenameFromAdr(Adr, Error); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | return NULL; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | // Get number of external variables |
| 117 | // Return 0 if none has been found |
| 118 | size_t DBGManager_GetNbExternalVariables(void) |
| 119 | { |
| 120 | if ((DBGType & DBG_ELFDWARF)) |
| 121 | { |
| 122 | return DWARFManager_GetNbExternalVariables(); |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | return 0; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | // |
| 132 | size_t DBGManager_GetAdrFromSymbolName(char *SymbolName) |
| 133 | { |
| 134 | if ((DBGType & DBG_ELF)) |
| 135 | { |
| 136 | return ELFManager_GetAdrFromSymbolName(SymbolName); |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | return 0; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
| 145 | // Get external variable's Address based on his Name |
| 146 | // Return found Address |
| 147 | // Return NULL if no Address has been found |
| 148 | size_t DBGManager_GetExternalVariableAdrFromName(char *VariableName) |
| 149 | { |
| 150 | if ((DBGType & DBG_ELFDWARF)) |
| 151 | { |
| 152 | return DWARFManager_GetExternalVariableAdrFromName(VariableName); |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | return 0; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | // |
| 162 | size_t DBGManager_GetExternalVariableTypeTag(size_t Index) |
| 163 | { |
| 164 | if ((DBGType & DBG_ELFDWARF)) |
| 165 | { |
| 166 | return DWARFManager_GetExternalVariableTypeTag(Index); |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | return 0; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | // Get external variable's type name based on his Index |
| 176 | // Return type name's text pointer found |
| 177 | // Return NULL if no type name has been found |
| 178 | char *DBGManager_GetExternalVariableTypeName(size_t Index) |
| 179 | { |
| 180 | if ((DBGType & DBG_ELFDWARF)) |
| 181 | { |
| 182 | return DWARFManager_GetExternalVariableTypeName(Index); |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | return NULL; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
| 191 | // Get external variable's Address based on his Index |
| 192 | // Return the Address found |
| 193 | // Return 0 if no Address has been found |
| 194 | size_t DBGManager_GetExternalVariableAdr(size_t Index) |
| 195 | { |
| 196 | if ((DBGType & DBG_ELFDWARF)) |
| 197 | { |
| 198 | return DWARFManager_GetExternalVariableAdr(Index); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | return 0; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | // Get external variable's type byte size based on his Index |
| 208 | // Return the type's byte size found |
| 209 | // Return 0 if no type's byte size has been found |
| 210 | size_t DBGManager_GetExternalVariableTypeByteSize(size_t Index) |
| 211 | { |
| 212 | if ((DBGType & DBG_ELFDWARF)) |
| 213 | { |
| 214 | return DWARFManager_GetExternalVariableTypeByteSize(Index); |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | return 0; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | // Get external variable's type encoding based on his Index |
| 224 | // Return the type encoding found |
| 225 | // Return 0 if no type encoding has been found |
| 226 | size_t DBGManager_GetExternalVariableTypeEncoding(size_t Index) |
| 227 | { |
| 228 | if ((DBGType & DBG_ELFDWARF)) |
| 229 | { |
| 230 | return DWARFManager_GetExternalVariableTypeEncoding(Index); |
| 231 | } |
| 232 | else |
| 233 | { |
| 234 | return 0; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | |
| 239 | // Get external variable value based on his Index |
| 240 | // Return value as a text pointer |
| 241 | // Note: Pointer may point on a 0 lenght text |
| 242 | char *DBGManager_GetExternalVariableValue(size_t Index) |
| 243 | { |
| 244 | uint32_t Adr = 0; |
| 245 | uint32_t TypeEncoding = DBG_NO_TYPEENCODING; |
| 246 | uint32_t TypeByteSize = 0; |
| 247 | |
| 248 | if ((DBGType & DBG_ELFDWARF)) |
| 249 | { |
| 250 | Adr = DWARFManager_GetExternalVariableAdr(Index); |
| 251 | TypeEncoding = DWARFManager_GetExternalVariableTypeEncoding(Index); |
| 252 | TypeByteSize = DWARFManager_GetExternalVariableTypeByteSize(Index); |
| 253 | } |
| 254 | |
| 255 | return DBGManager_GetVariableValueFromAdr(Adr, TypeEncoding, TypeByteSize); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | // Get variable value based on his Adresse, Encoding Type and Size |
| 260 | // Return value as a text pointer |
| 261 | // Note: Pointer may point on a 0 lenght text if Adress is NULL |
| 262 | char *DBGManager_GetVariableValueFromAdr(uint32_t Adr, uint32_t TypeEncoding, uint32_t TypeByteSize) |
| 263 | { |
| 264 | Value V; |
| 265 | char *Ptrvalue = value; |
| 266 | |
| 267 | value[0] = 0; |
| 268 | |
| 269 | if (Adr) |
| 270 | { |
| 271 | memset(&V, 0, sizeof(Value)); |
| 272 | #if 0 |
| 273 | for (uint32_t i = 0; i < TypeByteSize; i++) |
| 274 | jaguarMainRAM[Adr + i] = 0; |
| 275 | //jaguarMainRAM[Adr + i] = rand(); |
| 276 | jaguarMainRAM[Adr + TypeByteSize - 1] = 0x10; |
| 277 | #endif |
| 278 | #if 1 |
| 279 | for (uint32_t i = 0, j = TypeByteSize; i < TypeByteSize; i++, j--) |
| 280 | { |
| 281 | V.C[i] = jaguarMainRAM[Adr + j - 1]; |
| 282 | } |
| 283 | #endif |
| 284 | |
| 285 | switch (TypeEncoding) |
| 286 | { |
| 287 | case DBG_ATE_address: |
| 288 | break; |
| 289 | |
| 290 | case DBG_ATE_boolean: |
| 291 | break; |
| 292 | |
| 293 | case DBG_ATE_complex_float: |
| 294 | break; |
| 295 | |
| 296 | case DBG_ATE_float: |
| 297 | switch (TypeByteSize) |
| 298 | { |
| 299 | case 4: |
| 300 | sprintf(value, "%F", V.F); |
| 301 | break; |
| 302 | |
| 303 | case 8: |
| 304 | //V.D = (double)jaguarMainRAM[Adr]; |
| 305 | //sprintf(value, "%10.10F", V.D); |
| 306 | sprintf(value, "%F", V.D); |
| 307 | break; |
| 308 | |
| 309 | default: |
| 310 | break; |
| 311 | } |
| 312 | break; |
| 313 | |
| 314 | case DBG_ATE_signed: |
| 315 | switch (TypeByteSize) |
| 316 | { |
| 317 | case 4: |
| 318 | sprintf(value, "%i", V.SI); |
| 319 | break; |
| 320 | |
| 321 | case 8: |
| 322 | sprintf(value, "%i", V.SL); |
| 323 | break; |
| 324 | |
| 325 | default: |
| 326 | break; |
| 327 | } |
| 328 | break; |
| 329 | |
| 330 | case DBG_ATE_signed_char: |
| 331 | break; |
| 332 | |
| 333 | case DBG_ATE_unsigned: |
| 334 | switch (TypeByteSize) |
| 335 | { |
| 336 | case 4: |
| 337 | sprintf(value, "%u", V.UI); |
| 338 | break; |
| 339 | |
| 340 | case 8: |
| 341 | sprintf(value, "%u", V.UL); |
| 342 | break; |
| 343 | |
| 344 | default: |
| 345 | break; |
| 346 | } |
| 347 | break; |
| 348 | |
| 349 | case DBG_ATE_unsigned_char: |
| 350 | break; |
| 351 | |
| 352 | case DBG_ATE_ptr: |
| 353 | switch (TypeByteSize) |
| 354 | { |
| 355 | case 4: |
| 356 | sprintf(value, "0x%06x", V.UI); |
| 357 | break; |
| 358 | |
| 359 | default: |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | default: |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return Ptrvalue; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | // Get external variable name based on his Index |
| 373 | // Return variable name's text pointer found |
| 374 | // Return NULL if no variable name has been found |
| 375 | char *DBGManager_GetExternalVariableName(size_t Index) |
| 376 | { |
| 377 | if ((DBGType & DBG_ELFDWARF)) |
| 378 | { |
| 379 | return DWARFManager_GetExternalVariableName(Index); |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | return NULL; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | |
| 388 | // Get line number from address and his tag |
| 389 | // Return line number on the symbol name found |
| 390 | // Return 0 if no symbol name has been found |
| 391 | size_t DBGManager_GetNumLineFromAdr(size_t Adr, size_t Tag) |
| 392 | { |
| 393 | if ((DBGType & DBG_ELFDWARF)) |
| 394 | { |
| 395 | return DWARFManager_GetNumLineFromAdr(Adr, Tag); |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | return 0; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | |
| 404 | // Get symbol name from address |
| 405 | // Return text pointer on the symbol name found |
| 406 | // Return NULL if no symbol name has been found |
| 407 | char *DBGManager_GetSymbolnameFromAdr(size_t Adr) |
| 408 | { |
| 409 | char *Symbolname = NULL; |
| 410 | |
| 411 | if ((DBGType & DBG_HWLABEL) || vjs.displayHWlabels) |
| 412 | { |
| 413 | Symbolname = HWLABELManager_GetSymbolnameFromAdr(Adr); |
| 414 | } |
| 415 | |
| 416 | if (Symbolname == NULL) |
| 417 | { |
| 418 | if ((DBGType & DBG_ELFDWARF)) |
| 419 | { |
| 420 | Symbolname = DWARFManager_GetSymbolnameFromAdr(Adr); |
| 421 | } |
| 422 | |
| 423 | if ((DBGType & DBG_ELF) && (Symbolname == NULL)) |
| 424 | { |
| 425 | Symbolname = ELFManager_GetSymbolnameFromAdr(Adr); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | return Symbolname; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | // Get source line based on the Address and his Tag |
| 434 | // Return text pointer on the source line found |
| 435 | // Return NULL if no source line has been found |
| 436 | char *DBGManager_GetLineSrcFromAdr(size_t Adr, size_t Tag) |
| 437 | { |
| 438 | char *Symbolname = NULL; |
| 439 | |
| 440 | if ((DBGType & DBG_ELFDWARF)) |
| 441 | { |
| 442 | Symbolname = DWARFManager_GetLineSrcFromAdr(Adr, Tag); |
| 443 | } |
| 444 | |
| 445 | return Symbolname; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | // Get text line from source based on address and num line (starting by 1) |
| 450 | // Return NULL if no text line has been found |
| 451 | char *DBGManager_GetLineSrcFromAdrNumLine(size_t Adr, size_t NumLine) |
| 452 | { |
| 453 | char *Symbolname = NULL; |
| 454 | |
| 455 | if ((DBGType & DBG_ELFDWARF)) |
| 456 | { |
| 457 | Symbolname = DWARFManager_GetLineSrcFromAdrNumLine(Adr, NumLine); |
| 458 | } |
| 459 | |
| 460 | return Symbolname; |
| 461 | } |
| 462 | |
| 463 | |
| 464 | // Get text line from source based on address and num line (starting by 1) |
| 465 | // Return NULL if no text line has been found |
| 466 | char *DBGManager_GetLineSrcFromNumLineBaseAdr(size_t Adr, size_t NumLine) |
| 467 | { |
| 468 | char *Symbolname = NULL; |
| 469 | |
| 470 | if ((DBGType & DBG_ELFDWARF)) |
| 471 | { |
| 472 | Symbolname = DWARFManager_GetLineSrcFromNumLineBaseAdr(Adr, NumLine); |
| 473 | } |
| 474 | |
| 475 | return Symbolname; |
| 476 | } |
| 477 | |