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