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