Added LEB128 decoding functions
[clinton/Virtual-Jaguar-Rx.git] / src / debugger / DBGManager.cpp
CommitLineData
2d0121d4
JPM
1//\r
2// DBGManager.cpp: Debugger information manager\r
3//\r
4// by Jean-Paul Mari\r
5//\r
6// JPM = Jean-Paul Mari <djipi.mari@gmail.com>\r
7//\r
8// WHO WHEN WHAT\r
9// --- ---------- ------------------------------------------------------------\r
10// JPM 12/21/2016 Created this file\r
11// JPM Various efforts to set the ELF format support\r
12// JPM Various efforts to set the DWARF format support\r
13\r
14#include <stdlib.h>\r
15#include <string.h>\r
16#include <stdint.h>\r
17#include "libelf/libelf.h"\r
18#include "libelf/gelf.h"\r
19#include "log.h"\r
20#include "ELFManager.h"\r
21#include "DwarfManager.h"\r
22#include "DBGManager.h"\r
23#include "HWLABELManager.h"\r
24#include "settings.h"\r
25#include "memory.h"\r
26\r
27\r
2d0121d4
JPM
28//\r
29struct Value\r
30{\r
31 union\r
32 {\r
33 char C[10];\r
34 double D;\r
35 float F;\r
36 int32_t SI;\r
37 int64_t SL;\r
38 uint32_t UI;\r
39 uint64_t UL;\r
40 };\r
41}S_Value;\r
42\r
43\r
44// Common debugger variables\r
45size_t DBGType;\r
46char value[1000];\r
47\r
48\r
49// Common debugger initialisation\r
50void DBGManager_Init(void)\r
51{\r
52 DBGType = DBG_NO_TYPE;\r
53 ELFManager_Init();\r
54 DWARFManager_Init();\r
55}\r
56\r
57\r
58// Common debugger reset\r
59void DBGManager_Reset(void)\r
60{\r
61 if ((DBGType & DBG_DWARF))\r
62 {\r
63 DWARFManager_Reset();\r
64 }\r
65\r
66 if ((DBGType & DBG_ELF))\r
67 {\r
68 ELFManager_Reset();\r
69 }\r
70\r
71 //DBGType = vjs.displayHWlabels ? DBG_HWLABEL : DBG_NO_TYPE;\r
72 DBGType = DBG_NO_TYPE;\r
73}\r
74\r
75\r
a4e46c05
JPM
76// Get debugger type\r
77size_t DBGManager_GetType(void)\r
78{\r
79 return DBGType;\r
80}\r
81\r
82\r
2d0121d4
JPM
83// Common debugger set\r
84void DBGManager_SetType(size_t DBGTypeSet)\r
85{\r
86 DBGType |= DBGTypeSet;\r
87}\r
88\r
89\r
90// Common debugger close\r
91void DBGManager_Close(void)\r
92{\r
93 if ((DBGType & DBG_DWARF))\r
94 {\r
95 DWARFManager_Close();\r
96 }\r
97\r
98 if ((DBGType & DBG_ELF))\r
99 {\r
100 ELFManager_Close();\r
101 }\r
102}\r
103\r
104\r
105// Get source filename based on the memeory address\r
106// return NULL if no source filename\r
107char *DBGManager_GetFullSourceFilenameFromAdr(size_t Adr, bool *Error)\r
108{\r
109 if ((DBGType & DBG_ELFDWARF))\r
110 {\r
111 return DWARFManager_GetFullSourceFilenameFromAdr(Adr, Error);\r
112 }\r
113 else\r
114 {\r
115 return NULL;\r
116 }\r
117}\r
118\r
119\r
0203b5fd
JPM
120// Get number of local variables\r
121// Return 0 if none has been found\r
122size_t DBGManager_GetNbLocalVariables(size_t Adr)\r
123{\r
124 if ((DBGType & DBG_ELFDWARF))\r
125 {\r
126 return DWARFManager_GetNbLocalVariables(Adr);\r
127 }\r
128 else\r
129 {\r
130 return 0;\r
131 }\r
132}\r
133\r
134\r
135// Get number of global variables\r
2d0121d4 136// Return 0 if none has been found\r
0203b5fd 137size_t DBGManager_GetNbGlobalVariables(void)\r
2d0121d4
JPM
138{\r
139 if ((DBGType & DBG_ELFDWARF))\r
140 {\r
0203b5fd 141 return DWARFManager_GetNbGlobalVariables();\r
2d0121d4
JPM
142 }\r
143 else\r
144 {\r
145 return 0;\r
146 }\r
147}\r
148\r
149\r
a4e46c05
JPM
150// Get address from symbol name\r
151// Return found address\r
152// Return NULL if no symbol has been found\r
2d0121d4
JPM
153size_t DBGManager_GetAdrFromSymbolName(char *SymbolName)\r
154{\r
a4e46c05 155 if (SymbolName)\r
2d0121d4 156 {\r
a4e46c05
JPM
157 if ((DBGType & DBG_ELF))\r
158 {\r
159 return ELFManager_GetAdrFromSymbolName(SymbolName);\r
160 }\r
2d0121d4 161 }\r
a4e46c05
JPM
162\r
163 return 0;\r
2d0121d4
JPM
164}\r
165\r
166\r
0203b5fd 167// Get global variable's Address based on his Name\r
2d0121d4
JPM
168// Return found Address\r
169// Return NULL if no Address has been found\r
0203b5fd 170size_t DBGManager_GetGlobalVariableAdrFromName(char *VariableName)\r
2d0121d4
JPM
171{\r
172 if ((DBGType & DBG_ELFDWARF))\r
173 {\r
0203b5fd 174 return DWARFManager_GetGlobalVariableAdrFromName(VariableName);\r
2d0121d4
JPM
175 }\r
176 else\r
177 {\r
178 return 0;\r
179 }\r
180}\r
181\r
182\r
0203b5fd
JPM
183// Get local variable's type encoding based on his address and Index\r
184// Return the type encoding found\r
185// Return 0 if no type encoding has been found\r
186size_t DBGManager_GetLocalVariableTypeEncoding(size_t Adr, size_t Index)\r
187{\r
188 if ((DBGType & DBG_ELFDWARF))\r
189 {\r
190 return DWARFManager_GetLocalVariableTypeEncoding(Adr, Index);\r
191 }\r
192 else\r
193 {\r
194 return 0;\r
195 }\r
196}\r
197\r
198\r
199//\r
200int DBGManager_GetLocalVariableOffset(size_t Adr, size_t Index)\r
201{\r
202 if ((DBGType & DBG_ELFDWARF))\r
203 {\r
204 return DWARFManager_GetLocalVariableOffset(Adr, Index);\r
205 }\r
206 else\r
207 {\r
208 return 0;\r
209 }\r
210}\r
211\r
212\r
213// Get local variable's type byte size based on his address and Index\r
214// Return the type's byte size found\r
215// Return 0 if no type's byte size has been found\r
216size_t DBGManager_GetLocalVariableTypeByteSize(size_t Adr, size_t Index)\r
217{\r
218 if ((DBGType & DBG_ELFDWARF))\r
219 {\r
220 return DWARFManager_GetLocalVariableTypeByteSize(Adr, Index);\r
221 }\r
222 else\r
223 {\r
224 return 0;\r
225 }\r
226}\r
227\r
228\r
229//\r
230size_t DBGManager_GetLocalVariableTypeTag(size_t Adr, size_t Index)\r
231{\r
232 if ((DBGType & DBG_ELFDWARF))\r
233 {\r
234 return DWARFManager_GetLocalVariableTypeTag(Adr, Index);\r
235 }\r
236 else\r
237 {\r
238 return 0;\r
239 }\r
240}\r
241\r
242\r
2d0121d4 243//\r
0203b5fd 244size_t DBGManager_GetGlobalVariableTypeTag(size_t Index)\r
2d0121d4
JPM
245{\r
246 if ((DBGType & DBG_ELFDWARF))\r
247 {\r
0203b5fd 248 return DWARFManager_GetGlobalVariableTypeTag(Index);\r
2d0121d4
JPM
249 }\r
250 else\r
251 {\r
252 return 0;\r
253 }\r
254}\r
255\r
256\r
0203b5fd 257// Get global variable's type name based on his Index\r
2d0121d4
JPM
258// Return type name's text pointer found\r
259// Return NULL if no type name has been found\r
0203b5fd 260char *DBGManager_GetGlobalVariableTypeName(size_t Index)\r
2d0121d4
JPM
261{\r
262 if ((DBGType & DBG_ELFDWARF))\r
263 {\r
0203b5fd 264 return DWARFManager_GetGlobalVariableTypeName(Index);\r
2d0121d4
JPM
265 }\r
266 else\r
267 {\r
268 return NULL;\r
269 }\r
270}\r
271\r
272\r
0203b5fd 273// Get global variable's Address based on his Index\r
2d0121d4
JPM
274// Return the Address found\r
275// Return 0 if no Address has been found\r
0203b5fd 276size_t DBGManager_GetGlobalVariableAdr(size_t Index)\r
2d0121d4
JPM
277{\r
278 if ((DBGType & DBG_ELFDWARF))\r
279 {\r
0203b5fd 280 return DWARFManager_GetGlobalVariableAdr(Index);\r
2d0121d4
JPM
281 }\r
282 else\r
283 {\r
284 return 0;\r
285 }\r
286}\r
287\r
288\r
0203b5fd 289// Get global variable's type byte size based on his Index\r
2d0121d4
JPM
290// Return the type's byte size found\r
291// Return 0 if no type's byte size has been found\r
0203b5fd 292size_t DBGManager_GetGlobalVariableTypeByteSize(size_t Index)\r
2d0121d4
JPM
293{\r
294 if ((DBGType & DBG_ELFDWARF))\r
295 {\r
0203b5fd 296 return DWARFManager_GetGlobalVariableTypeByteSize(Index);\r
2d0121d4
JPM
297 }\r
298 else\r
299 {\r
300 return 0;\r
301 }\r
302}\r
303\r
304\r
0203b5fd 305// Get global variable's type encoding based on his Index\r
2d0121d4
JPM
306// Return the type encoding found\r
307// Return 0 if no type encoding has been found\r
0203b5fd 308size_t DBGManager_GetGlobalVariableTypeEncoding(size_t Index)\r
2d0121d4
JPM
309{\r
310 if ((DBGType & DBG_ELFDWARF))\r
311 {\r
0203b5fd 312 return DWARFManager_GetGlobalVariableTypeEncoding(Index);\r
2d0121d4
JPM
313 }\r
314 else\r
315 {\r
316 return 0;\r
317 }\r
318}\r
319\r
320\r
0203b5fd 321// Get global variable value based on his Index\r
2d0121d4
JPM
322// Return value as a text pointer\r
323// Note: Pointer may point on a 0 lenght text\r
0203b5fd 324char *DBGManager_GetGlobalVariableValue(size_t Index)\r
2d0121d4 325{\r
0203b5fd
JPM
326 size_t Adr = 0;\r
327 size_t TypeEncoding = DBG_NO_TYPEENCODING;\r
328 size_t TypeByteSize = 0;\r
2d0121d4
JPM
329\r
330 if ((DBGType & DBG_ELFDWARF))\r
331 {\r
0203b5fd
JPM
332 Adr = DWARFManager_GetGlobalVariableAdr(Index);\r
333 TypeEncoding = DWARFManager_GetGlobalVariableTypeEncoding(Index);\r
334 TypeByteSize = DWARFManager_GetGlobalVariableTypeByteSize(Index);\r
2d0121d4
JPM
335 }\r
336\r
337 return DBGManager_GetVariableValueFromAdr(Adr, TypeEncoding, TypeByteSize);\r
338}\r
339\r
340\r
341// Get variable value based on his Adresse, Encoding Type and Size\r
342// Return value as a text pointer\r
343// Note: Pointer may point on a 0 lenght text if Adress is NULL\r
0203b5fd 344char *DBGManager_GetVariableValueFromAdr(size_t Adr, size_t TypeEncoding, size_t TypeByteSize)\r
2d0121d4
JPM
345{\r
346 Value V;\r
347 char *Ptrvalue = value;\r
348\r
349 value[0] = 0;\r
350\r
351 if (Adr)\r
352 {\r
353 memset(&V, 0, sizeof(Value));\r
354#if 0\r
355 for (uint32_t i = 0; i < TypeByteSize; i++)\r
356 jaguarMainRAM[Adr + i] = 0;\r
357 //jaguarMainRAM[Adr + i] = rand();\r
358 jaguarMainRAM[Adr + TypeByteSize - 1] = 0x10;\r
359#endif\r
360#if 1\r
0203b5fd 361 for (size_t i = 0, j = TypeByteSize; i < TypeByteSize; i++, j--)\r
2d0121d4
JPM
362 {\r
363 V.C[i] = jaguarMainRAM[Adr + j - 1];\r
364 }\r
365#endif\r
366\r
367 switch (TypeEncoding)\r
368 {\r
369 case DBG_ATE_address:\r
370 break;\r
371\r
372 case DBG_ATE_boolean:\r
373 break;\r
374\r
375 case DBG_ATE_complex_float:\r
376 break;\r
377\r
378 case DBG_ATE_float:\r
379 switch (TypeByteSize)\r
380 {\r
381 case 4:\r
382 sprintf(value, "%F", V.F);\r
383 break;\r
384\r
385 case 8:\r
386 //V.D = (double)jaguarMainRAM[Adr];\r
387 //sprintf(value, "%10.10F", V.D);\r
388 sprintf(value, "%F", V.D);\r
389 break;\r
390\r
391 default:\r
392 break;\r
393 }\r
394 break;\r
395\r
396 case DBG_ATE_signed:\r
397 switch (TypeByteSize)\r
398 {\r
399 case 4:\r
400 sprintf(value, "%i", V.SI);\r
401 break;\r
402\r
403 case 8:\r
404 sprintf(value, "%i", V.SL);\r
405 break;\r
406\r
407 default:\r
408 break;\r
409 }\r
410 break;\r
411\r
412 case DBG_ATE_signed_char:\r
413 break;\r
414\r
415 case DBG_ATE_unsigned:\r
416 switch (TypeByteSize)\r
417 {\r
418 case 4:\r
419 sprintf(value, "%u", V.UI);\r
420 break;\r
421\r
422 case 8:\r
423 sprintf(value, "%u", V.UL);\r
424 break;\r
425\r
426 default:\r
427 break;\r
428 }\r
429 break;\r
430\r
431 case DBG_ATE_unsigned_char:\r
432 break;\r
433\r
434 case DBG_ATE_ptr:\r
435 switch (TypeByteSize)\r
436 {\r
437 case 4:\r
438 sprintf(value, "0x%06x", V.UI);\r
439 break;\r
440\r
441 default:\r
442 break;\r
443 }\r
444\r
445 default:\r
446 break;\r
447 }\r
448 }\r
449\r
450 return Ptrvalue;\r
451}\r
452\r
453\r
0203b5fd
JPM
454// Get local variable's type name based on his Index\r
455// Return type name's text pointer found\r
456// Return NULL if no type name has been found\r
457char *DBGManager_GetLocalVariableTypeName(size_t Adr, size_t Index)\r
458{\r
459 if ((DBGType & DBG_ELFDWARF))\r
460 {\r
461 return DWARFManager_GetLocalVariableTypeName(Adr, Index);\r
462 }\r
463 else\r
464 {\r
465 return NULL;\r
466 }\r
467}\r
468\r
469\r
470// Get local variable Op based on his Index\r
471// Return variable Op's found\r
472// Return 0 if no variable Op has been found\r
473size_t DBGManager_GetLocalVariableOp(size_t Adr, size_t Index)\r
474{\r
475 if ((DBGType & DBG_ELFDWARF))\r
476 {\r
477 return DWARFManager_GetLocalVariableOp(Adr, Index);\r
478 }\r
479 else\r
480 {\r
481 return 0;\r
482 }\r
483}\r
484\r
485\r
486// Get local variable name based on his Index\r
487// Return variable name's text pointer found\r
488// Return NULL if no variable name has been found\r
489char *DBGManager_GetLocalVariableName(size_t Adr, size_t Index)\r
490{\r
491 if ((DBGType & DBG_ELFDWARF))\r
492 {\r
493 return DWARFManager_GetLocalVariableName(Adr, Index);\r
494 }\r
495 else\r
496 {\r
497 return NULL;\r
498 }\r
499}\r
500\r
501\r
502// Get global variable name based on his Index\r
2d0121d4
JPM
503// Return variable name's text pointer found\r
504// Return NULL if no variable name has been found\r
0203b5fd
JPM
505char *DBGManager_GetGlobalVariableName(size_t Index)\r
506{\r
507 if ((DBGType & DBG_ELFDWARF))\r
508 {\r
509 return DWARFManager_GetGlobalVariableName(Index);\r
510 }\r
511 else\r
512 {\r
513 return NULL;\r
514 }\r
515}\r
516\r
517\r
518// Get function name from address\r
519// Return function name found\r
520// Return NULL if no function name has been found\r
521char *DBGManager_GetFunctionName(size_t Adr)\r
2d0121d4
JPM
522{\r
523 if ((DBGType & DBG_ELFDWARF))\r
524 {\r
0203b5fd 525 return DWARFManager_GetFunctionName(Adr);\r
2d0121d4
JPM
526 }\r
527 else\r
528 {\r
529 return NULL;\r
530 }\r
531}\r
532\r
533\r
534// Get line number from address and his tag\r
535// Return line number on the symbol name found\r
536// Return 0 if no symbol name has been found\r
537size_t DBGManager_GetNumLineFromAdr(size_t Adr, size_t Tag)\r
538{\r
539 if ((DBGType & DBG_ELFDWARF))\r
540 {\r
541 return DWARFManager_GetNumLineFromAdr(Adr, Tag);\r
542 }\r
543 else\r
544 {\r
545 return 0;\r
546 }\r
547}\r
548\r
549\r
550// Get symbol name from address\r
551// Return text pointer on the symbol name found\r
552// Return NULL if no symbol name has been found\r
0203b5fd 553char *DBGManager_GetSymbolNameFromAdr(size_t Adr)\r
2d0121d4
JPM
554{\r
555 char *Symbolname;\r
556\r
557 //if ((DBGType & DBG_HWLABEL) || vjs.displayHWlabels)\r
558 if (vjs.displayHWlabels)\r
559 {\r
560 Symbolname = HWLABELManager_GetSymbolnameFromAdr(Adr);\r
561 }\r
562 else\r
563 {\r
564 Symbolname = NULL;\r
565 }\r
41d6f5f7
JPM
566#ifdef _MSC_VER\r
567#pragma message("Warning: !!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!")\r
568#else\r
569 #warning "!!! Need to set the DBG_HWLABEL in DBGType instead to use the setting value !!!"\r
2d0121d4
JPM
570#endif // _MSC_VER\r
571\r
572 if (Symbolname == NULL)\r
573 {\r
574 if ((DBGType & DBG_ELFDWARF))\r
575 {\r
576 Symbolname = DWARFManager_GetSymbolnameFromAdr(Adr);\r
577 }\r
578\r
579 if ((DBGType & DBG_ELF) && (Symbolname == NULL))\r
580 {\r
581 Symbolname = ELFManager_GetSymbolnameFromAdr(Adr);\r
582 }\r
583 }\r
584\r
585 return Symbolname;\r
586}\r
587\r
588\r
589// Get source line based on the Address and his Tag\r
590// Return text pointer on the source line found\r
591// Return NULL if no source line has been found\r
592char *DBGManager_GetLineSrcFromAdr(size_t Adr, size_t Tag)\r
593{\r
594 char *Symbolname = NULL;\r
595\r
596 if ((DBGType & DBG_ELFDWARF))\r
597 {\r
598 Symbolname = DWARFManager_GetLineSrcFromAdr(Adr, Tag);\r
599 }\r
600\r
601 return Symbolname;\r
602}\r
603\r
604\r
605// Get text line from source based on address and num line (starting by 1)\r
606// Return NULL if no text line has been found\r
607char *DBGManager_GetLineSrcFromAdrNumLine(size_t Adr, size_t NumLine)\r
608{\r
609 char *Symbolname = NULL;\r
610\r
611 if ((DBGType & DBG_ELFDWARF))\r
612 {\r
613 Symbolname = DWARFManager_GetLineSrcFromAdrNumLine(Adr, NumLine);\r
614 }\r
615\r
616 return Symbolname;\r
617}\r
618\r
619\r
620// Get text line from source based on address and num line (starting by 1)\r
621// Return NULL if no text line has been found\r
622char *DBGManager_GetLineSrcFromNumLineBaseAdr(size_t Adr, size_t NumLine)\r
623{\r
624 char *Symbolname = NULL;\r
625\r
626 if ((DBGType & DBG_ELFDWARF))\r
627 {\r
628 Symbolname = DWARFManager_GetLineSrcFromNumLineBaseAdr(Adr, NumLine);\r
629 }\r
630\r
631 return Symbolname;\r
632}\r
633\r