forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathgdbremote.py
executable file
·1835 lines (1620 loc) · 58.6 KB
/
gdbremote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# ----------------------------------------------------------------------
# This module will enable GDB remote packet logging when the
# 'start_gdb_log' command is called with a filename to log to. When the
# 'stop_gdb_log' command is called, it will disable the logging and
# print out statistics about how long commands took to execute and also
# will primnt ou
# Be sure to add the python path that points to the LLDB shared library.
#
# To use this in the embedded python interpreter using "lldb" just
# import it with the full path using the "command script import"
# command. This can be done from the LLDB command line:
# (lldb) command script import /path/to/gdbremote.py
# Or it can be added to your ~/.lldbinit file so this module is always
# available.
# ----------------------------------------------------------------------
importbinascii
importsubprocess
importjson
importmath
importoptparse
importos
importre
importshlex
importstring
importsys
importtempfile
importxml.etree.ElementTreeasET
# ----------------------------------------------------------------------
# Global variables
# ----------------------------------------------------------------------
g_log_file=""
g_byte_order="little"
g_number_regex=re.compile("^(0x[0-9a-fA-F]+|[0-9]+)")
g_thread_id_regex=re.compile("^(-1|[0-9a-fA-F]+|0)")
classTerminalColors:
"""Simple terminal colors class"""
def__init__(self, enabled=True):
# TODO: discover terminal type from "file" and disable if
# it can't handle the color codes
self.enabled=enabled
defreset(self):
"""Reset all terminal colors and formatting."""
ifself.enabled:
return"\x1b[0m"
return""
defbold(self, on=True):
"""Enable or disable bold depending on the "on" parameter."""
ifself.enabled:
ifon:
return"\x1b[1m"
else:
return"\x1b[22m"
return""
defitalics(self, on=True):
"""Enable or disable italics depending on the "on" parameter."""
ifself.enabled:
ifon:
return"\x1b[3m"
else:
return"\x1b[23m"
return""
defunderline(self, on=True):
"""Enable or disable underline depending on the "on" parameter."""
ifself.enabled:
ifon:
return"\x1b[4m"
else:
return"\x1b[24m"
return""
definverse(self, on=True):
"""Enable or disable inverse depending on the "on" parameter."""
ifself.enabled:
ifon:
return"\x1b[7m"
else:
return"\x1b[27m"
return""
defstrike(self, on=True):
"""Enable or disable strike through depending on the "on" parameter."""
ifself.enabled:
ifon:
return"\x1b[9m"
else:
return"\x1b[29m"
return""
defblack(self, fg=True):
"""Set the foreground or background color to black.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.
"""
ifself.enabled:
iffg:
return"\x1b[30m"
else:
return"\x1b[40m"
return""
defred(self, fg=True):
"""Set the foreground or background color to red.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.
"""
ifself.enabled:
iffg:
return"\x1b[31m"
else:
return"\x1b[41m"
return""
defgreen(self, fg=True):
"""Set the foreground or background color to green.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.
"""
ifself.enabled:
iffg:
return"\x1b[32m"
else:
return"\x1b[42m"
return""
defyellow(self, fg=True):
"""Set the foreground or background color to yellow.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.
"""
ifself.enabled:
iffg:
return"\x1b[33m"
else:
return"\x1b[43m"
return""
defblue(self, fg=True):
"""Set the foreground or background color to blue.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.
"""
ifself.enabled:
iffg:
return"\x1b[34m"
else:
return"\x1b[44m"
return""
defmagenta(self, fg=True):
"""Set the foreground or background color to magenta.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.
"""
ifself.enabled:
iffg:
return"\x1b[35m"
else:
return"\x1b[45m"
return""
defcyan(self, fg=True):
"""Set the foreground or background color to cyan.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.
"""
ifself.enabled:
iffg:
return"\x1b[36m"
else:
return"\x1b[46m"
return""
defwhite(self, fg=True):
"""Set the foreground or background color to white.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.
"""
ifself.enabled:
iffg:
return"\x1b[37m"
else:
return"\x1b[47m"
return""
defdefault(self, fg=True):
"""Set the foreground or background color to the default.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.
"""
ifself.enabled:
iffg:
return"\x1b[39m"
else:
return"\x1b[49m"
return""
defstart_gdb_log(debugger, command, result, dict):
"""Start logging GDB remote packets by enabling logging with timestamps and
thread safe logging. Follow a call to this function with a call to "stop_gdb_log"
in order to dump out the commands."""
globalg_log_file
command_args=shlex.split(command)
usage="usage: start_gdb_log [options] [<LOGFILEPATH>]"
description="""The command enables GDB remote packet logging with timestamps. The packets will be logged to <LOGFILEPATH> if supplied, or a temporary file will be used. Logging stops when stop_gdb_log is called and the packet times will
be aggregated and displayed."""
parser=optparse.OptionParser(
description=description, prog="start_gdb_log", usage=usage
)
parser.add_option(
"-v",
"--verbose",
action="store_true",
dest="verbose",
help="display verbose debug info",
default=False,
)
try:
(options, args) =parser.parse_args(command_args)
except:
return
ifg_log_file:
result.PutCString(
'error: logging is already in progress with file "%s"'%g_log_file
)
else:
args_len=len(args)
ifargs_len==0:
g_log_file=tempfile.mktemp()
eliflen(args) ==1:
g_log_file=args[0]
ifg_log_file:
debugger.HandleCommand(
'log enable --threadsafe --timestamp --file "%s" gdb-remote packets'
%g_log_file
)
result.PutCString(
"GDB packet logging enable with log file '%s'\nUse the 'stop_gdb_log' command to stop logging and show packet statistics."
%g_log_file
)
return
result.PutCString("error: invalid log file path")
result.PutCString(usage)
defstop_gdb_log(debugger, command, result, dict):
"""Stop logging GDB remote packets to the file that was specified in a call
to "start_gdb_log" and normalize the timestamps to be relative to the first
timestamp in the log file. Also print out statistics for how long each
command took to allow performance bottlenecks to be determined."""
globalg_log_file
# Any commands whose names might be followed by more valid C identifier
# characters must be listed here
command_args=shlex.split(command)
usage="usage: stop_gdb_log [options]"
description="""The command stops a previously enabled GDB remote packet logging command. Packet logging must have been previously enabled with a call to start_gdb_log."""
parser=optparse.OptionParser(
description=description, prog="stop_gdb_log", usage=usage
)
parser.add_option(
"-v",
"--verbose",
action="store_true",
dest="verbose",
help="display verbose debug info",
default=False,
)
parser.add_option(
"--plot",
action="store_true",
dest="plot",
help="plot packet latencies by packet type",
default=False,
)
parser.add_option(
"-q",
"--quiet",
action="store_true",
dest="quiet",
help="display verbose debug info",
default=False,
)
parser.add_option(
"-C",
"--color",
action="store_true",
dest="color",
help="add terminal colors",
default=False,
)
parser.add_option(
"-c",
"--sort-by-count",
action="store_true",
dest="sort_count",
help="display verbose debug info",
default=False,
)
parser.add_option(
"-s",
"--symbolicate",
action="store_true",
dest="symbolicate",
help='symbolicate addresses in log using current "lldb.target"',
default=False,
)
try:
(options, args) =parser.parse_args(command_args)
except:
return
options.colors=TerminalColors(options.color)
options.symbolicator=None
ifoptions.symbolicate:
iflldb.target:
importlldb.utils.symbolication
options.symbolicator=lldb.utils.symbolication.Symbolicator()
options.symbolicator.target=lldb.target
else:
print("error: can't symbolicate without a target")
ifnotg_log_file:
result.PutCString(
'error: logging must have been previously enabled with a call to "stop_gdb_log"'
)
elifos.path.exists(g_log_file):
iflen(args) ==0:
debugger.HandleCommand("log disable gdb-remote packets")
result.PutCString(
"GDB packet logging disabled. Logged packets are in '%s'"%g_log_file
)
parse_gdb_log_file(g_log_file, options)
else:
result.PutCString(usage)
else:
print('error: the GDB packet log file "%s" does not exist'%g_log_file)
defis_hex_byte(str):
iflen(str) ==2:
returnstr[0] instring.hexdigitsandstr[1] instring.hexdigits
returnFalse
defget_hex_string_if_all_printable(str):
try:
s=binascii.unhexlify(str).decode()
ifall(cinstring.printableforcins):
returns
except (TypeError, binascii.Error, UnicodeDecodeError):
pass
returnNone
# global register info list
g_register_infos=list()
g_max_register_info_name_len=0
classRegisterInfo:
"""Class that represents register information"""
def__init__(self, kvp):
self.info=dict()
forkvinkvp:
key=kv[0]
value=kv[1]
self.info[key] =value
defname(self):
"""Get the name of the register."""
ifself.infoand"name"inself.info:
returnself.info["name"]
returnNone
defbit_size(self):
"""Get the size in bits of the register."""
ifself.infoand"bitsize"inself.info:
returnint(self.info["bitsize"])
return0
defbyte_size(self):
"""Get the size in bytes of the register."""
returnself.bit_size() /8
defget_value_from_hex_string(self, hex_str):
"""Dump the register value given a native byte order encoded hex ASCII byte string."""
encoding=self.info["encoding"]
bit_size=self.bit_size()
packet=Packet(hex_str)
ifencoding=="uint":
uval=packet.get_hex_uint(g_byte_order)
ifbit_size==8:
return"0x%2.2x"% (uval)
elifbit_size==16:
return"0x%4.4x"% (uval)
elifbit_size==32:
return"0x%8.8x"% (uval)
elifbit_size==64:
return"0x%16.16x"% (uval)
bytes=list()
uval=packet.get_hex_uint8()
whileuvalisnotNone:
bytes.append(uval)
uval=packet.get_hex_uint8()
value_str="0x"
ifg_byte_order=="little":
bytes.reverse()
forbyteinbytes:
value_str+="%2.2x"%byte
return"%s"% (value_str)
def__str__(self):
"""Dump the register info key/value pairs"""
s=""
forkeyinself.info.keys():
ifs:
s+=", "
s+="%s=%s "% (key, self.info[key])
returns
classPacket:
"""Class that represents a packet that contains string data"""
def__init__(self, packet_str):
self.str=packet_str
defpeek_char(self):
ch=0
ifself.str:
ch=self.str[0]
returnch
defget_char(self):
ch=0
ifself.str:
ch=self.str[0]
self.str=self.str[1:]
returnch
defskip_exact_string(self, s):
ifself.strandself.str.startswith(s):
self.str=self.str[len(s) :]
returnTrue
else:
returnFalse
defget_thread_id(self, fail_value=-1):
match=g_number_regex.match(self.str)
ifmatch:
number_str=match.group(1)
self.str=self.str[len(number_str) :]
returnint(number_str, 0)
else:
returnfail_value
defget_hex_uint8(self):
if (
self.str
andlen(self.str) >=2
andself.str[0] instring.hexdigits
andself.str[1] instring.hexdigits
):
uval=int(self.str[0:2], 16)
self.str=self.str[2:]
returnuval
returnNone
defget_hex_uint16(self, byte_order):
uval=0
ifbyte_order=="big":
uval|=self.get_hex_uint8() <<8
uval|=self.get_hex_uint8()
else:
uval|=self.get_hex_uint8()
uval|=self.get_hex_uint8() <<8
returnuval
defget_hex_uint32(self, byte_order):
uval=0
ifbyte_order=="big":
uval|=self.get_hex_uint8() <<24
uval|=self.get_hex_uint8() <<16
uval|=self.get_hex_uint8() <<8
uval|=self.get_hex_uint8()
else:
uval|=self.get_hex_uint8()
uval|=self.get_hex_uint8() <<8
uval|=self.get_hex_uint8() <<16
uval|=self.get_hex_uint8() <<24
returnuval
defget_hex_uint64(self, byte_order):
uval=0
ifbyte_order=="big":
uval|=self.get_hex_uint8() <<56
uval|=self.get_hex_uint8() <<48
uval|=self.get_hex_uint8() <<40
uval|=self.get_hex_uint8() <<32
uval|=self.get_hex_uint8() <<24
uval|=self.get_hex_uint8() <<16
uval|=self.get_hex_uint8() <<8
uval|=self.get_hex_uint8()
else:
uval|=self.get_hex_uint8()
uval|=self.get_hex_uint8() <<8
uval|=self.get_hex_uint8() <<16
uval|=self.get_hex_uint8() <<24
uval|=self.get_hex_uint8() <<32
uval|=self.get_hex_uint8() <<40
uval|=self.get_hex_uint8() <<48
uval|=self.get_hex_uint8() <<56
returnuval
defget_number(self, fail_value=-1):
"""Get a number from the packet. The number must be in big endian format and should be parsed
according to its prefix (starts with "0x" means hex, starts with "0" means octal, starts with
[1-9] means decimal, etc)"""
match=g_number_regex.match(self.str)
ifmatch:
number_str=match.group(1)
self.str=self.str[len(number_str) :]
returnint(number_str, 0)
else:
returnfail_value
defget_hex_ascii_str(self, n=0):
hex_chars=self.get_hex_chars(n)
ifhex_chars:
returnbinascii.unhexlify(hex_chars)
else:
returnNone
defget_hex_chars(self, n=0):
str_len=len(self.str)
ifn==0:
# n was zero, so we need to determine all hex chars and
# stop when we hit the end of the string of a non-hex character
whilen<str_lenandself.str[n] instring.hexdigits:
n=n+1
else:
ifn>str_len:
returnNone# Not enough chars
# Verify all chars are hex if a length was specified
foriinrange(n):
ifself.str[i] notinstring.hexdigits:
returnNone# Not all hex digits
ifn==0:
returnNone
hex_str=self.str[0:n]
self.str=self.str[n:]
returnhex_str
defget_hex_uint(self, byte_order, n=0):
ifbyte_order=="big":
hex_str=self.get_hex_chars(n)
ifhex_strisNone:
returnNone
returnint(hex_str, 16)
else:
uval=self.get_hex_uint8()
ifuvalisNone:
returnNone
uval_result=0
shift=0
whileuvalisnotNone:
uval_result|=uval<<shift
shift+=8
uval=self.get_hex_uint8()
returnuval_result
defget_key_value_pairs(self):
kvp=list()
if";"inself.str:
key_value_pairs=self.str.split(";")
forkey_value_pairinkey_value_pairs:
iflen(key_value_pair):
kvp.append(key_value_pair.split(":", 1))
returnkvp
defsplit(self, ch):
returnself.str.split(ch)
defsplit_hex(self, ch, byte_order):
hex_values=list()
strings=self.str.split(ch)
forstrinstrings:
hex_values.append(Packet(str).get_hex_uint(byte_order))
returnhex_values
def__str__(self):
returnself.str
def__len__(self):
returnlen(self.str)
g_thread_suffix_regex=re.compile(";thread:([0-9a-fA-F]+);")
defget_thread_from_thread_suffix(str):
ifstr:
match=g_thread_suffix_regex.match(str)
ifmatch:
returnint(match.group(1), 16)
returnNone
defcmd_qThreadStopInfo(options, cmd, args):
packet=Packet(args)
tid=packet.get_hex_uint("big")
print("get_thread_stop_info (tid = 0x%x)"% (tid))
defcmd_stop_reply(options, cmd, args):
print("get_last_stop_info()")
returnFalse
defrsp_stop_reply(options, cmd, cmd_args, rsp):
globalg_byte_order
packet=Packet(rsp)
stop_type=packet.get_char()
ifstop_type=="T"orstop_type=="S":
signo=packet.get_hex_uint8()
key_value_pairs=packet.get_key_value_pairs()
forkey_value_pairinkey_value_pairs:
key=key_value_pair[0]
ifis_hex_byte(key):
reg_num=Packet(key).get_hex_uint8()
ifreg_num<len(g_register_infos):
reg_info=g_register_infos[reg_num]
key_value_pair[0] =reg_info.name()
key_value_pair[1] =reg_info.get_value_from_hex_string(
key_value_pair[1]
)
elifkey=="jthreads"orkey=="jstopinfo":
key_value_pair[1] =binascii.unhexlify(key_value_pair[1])
key_value_pairs.insert(0, ["signal", signo])
print("stop_reply():")
dump_key_value_pairs(key_value_pairs)
elifstop_type=="W":
exit_status=packet.get_hex_uint8()
print("stop_reply(): exit (status=%i)"%exit_status)
elifstop_type=="O":
print('stop_reply(): stdout = "%s"'%packet.str)
defcmd_unknown_packet(options, cmd, args):
ifargs:
print("cmd: %s, args: %s", cmd, args)
else:
print("cmd: %s", cmd)
returnFalse
defcmd_qSymbol(options, cmd, args):
ifargs==":":
print("ready to serve symbols")
else:
packet=Packet(args)
symbol_addr=packet.get_hex_uint("big")
ifsymbol_addrisNone:
ifpacket.skip_exact_string(":"):
symbol_name=packet.get_hex_ascii_str()
print('lookup_symbol("%s") -> symbol not available yet'% (symbol_name))
else:
print("error: bad command format")
else:
ifpacket.skip_exact_string(":"):
symbol_name=packet.get_hex_ascii_str()
print('lookup_symbol("%s") -> 0x%x'% (symbol_name, symbol_addr))
else:
print("error: bad command format")
defcmd_QSetWithHexString(options, cmd, args):
print('%s("%s")'% (cmd[:-1], binascii.unhexlify(args)))
defcmd_QSetWithString(options, cmd, args):
print('%s("%s")'% (cmd[:-1], args))
defcmd_QSetWithUnsigned(options, cmd, args):
print("%s(%i)"% (cmd[:-1], int(args)))
defrsp_qSymbol(options, cmd, cmd_args, rsp):
iflen(rsp) ==0:
print("Unsupported")
else:
ifrsp=="OK":
print("No more symbols to lookup")
else:
packet=Packet(rsp)
ifpacket.skip_exact_string("qSymbol:"):
symbol_name=packet.get_hex_ascii_str()
print('lookup_symbol("%s")'% (symbol_name))
else:
print(
'error: response string should start with "qSymbol:": respnse is "%s"'
% (rsp)
)
defcmd_qXfer(options, cmd, args):
# $qXfer:features:read:target.xml:0,1ffff#14
print("read target special data %s"% (args))
returnTrue
defrsp_qXfer(options, cmd, cmd_args, rsp):
data=cmd_args.split(":")
ifdata[0] =="features":
ifdata[1] =="read":
filename, extension=os.path.splitext(data[2])
ifextension==".xml":
response=Packet(rsp)
xml_string=response.get_hex_ascii_str()
ifxml_string:
ch=xml_string[0]
ifch=="l":
xml_string=xml_string[1:]
xml_root=ET.fromstring(xml_string)
forreg_elementinxml_root.findall("./feature/reg"):
ifnot"value_regnums"inreg_element.attrib:
reg_info=RegisterInfo([])
if"name"inreg_element.attrib:
reg_info.info["name"] =reg_element.attrib["name"]
else:
reg_info.info["name"] ="unspecified"
if"encoding"inreg_element.attrib:
reg_info.info["encoding"] =reg_element.attrib[
"encoding"
]
else:
reg_info.info["encoding"] ="uint"
if"offset"inreg_element.attrib:
reg_info.info["offset"] =reg_element.attrib[
"offset"
]
if"bitsize"inreg_element.attrib:
reg_info.info["bitsize"] =reg_element.attrib[
"bitsize"
]
g_register_infos.append(reg_info)
print('XML for "%s":'% (data[2]))
ET.dump(xml_root)
defcmd_A(options, cmd, args):
print("launch process:")
packet=Packet(args)
whileTrue:
arg_len=packet.get_number()
ifarg_len==-1:
break
ifnotpacket.skip_exact_string(","):
break
arg_idx=packet.get_number()
ifarg_idx==-1:
break
ifnotpacket.skip_exact_string(","):
break
arg_value=packet.get_hex_ascii_str(arg_len)
print('argv[%u] = "%s"'% (arg_idx, arg_value))
defcmd_qC(options, cmd, args):
print("query_current_thread_id()")
defrsp_qC(options, cmd, cmd_args, rsp):
packet=Packet(rsp)
ifpacket.skip_exact_string("QC"):
tid=packet.get_thread_id()
print("current_thread_id = %#x"% (tid))
else:
print("current_thread_id = old thread ID")
defcmd_query_packet(options, cmd, args):
ifargs:
print("%s%s"% (cmd, args))
else:
print("%s"% (cmd))
returnFalse
defrsp_ok_error(rsp):
print("rsp: ", rsp)
defrsp_ok_means_supported(options, cmd, cmd_args, rsp):
ifrsp=="OK":
print("%s%s is supported"% (cmd, cmd_args))
elifrsp=="":
print("%s%s is not supported"% (cmd, cmd_args))
else:
print("%s%s -> %s"% (cmd, cmd_args, rsp))
defrsp_ok_means_success(options, cmd, cmd_args, rsp):
ifrsp=="OK":
print("success")
elifrsp=="":
print("%s%s is not supported"% (cmd, cmd_args))
else:
print("%s%s -> %s"% (cmd, cmd_args, rsp))
defdump_key_value_pairs(key_value_pairs):
max_key_len=0
forkey_value_pairinkey_value_pairs:
key_len=len(key_value_pair[0])
ifmax_key_len<key_len:
max_key_len=key_len
forkey_value_pairinkey_value_pairs:
key=key_value_pair[0]
value=key_value_pair[1]
unhex_value=get_hex_string_if_all_printable(value)
ifunhex_value:
print("%*s = %s (%s)"% (max_key_len, key, value, unhex_value))
else:
print("%*s = %s"% (max_key_len, key, value))
defrsp_dump_key_value_pairs(options, cmd, cmd_args, rsp):
ifrsp:
print("%s response:"% (cmd))
packet=Packet(rsp)
key_value_pairs=packet.get_key_value_pairs()
dump_key_value_pairs(key_value_pairs)
else:
print("not supported")
defcmd_c(options, cmd, args):
print("continue()")
returnFalse
defcmd_s(options, cmd, args):
print("step()")
returnFalse
defcmd_qSpeedTest(options, cmd, args):
print(("qSpeedTest: cmd='%s', args='%s'"% (cmd, args)))
defrsp_qSpeedTest(options, cmd, cmd_args, rsp):
print(("qSpeedTest: rsp='%s' cmd='%s', args='%s'"% (rsp, cmd, args)))
defcmd_vCont(options, cmd, args):
ifargs=="?":
print("%s: get supported extended continue modes"% (cmd))
else:
got_other_threads=0
s=""
forthread_actioninargs[1:].split(";"):
(short_action, thread) =thread_action.split(":", 1)
tid=int(thread, 16)
ifshort_action=="c":
action="continue"
elifshort_action=="s":
action="step"
elifshort_action[0] =="C":
action="continue with signal 0x%s"% (short_action[1:])
elifshort_action=="S":
action="step with signal 0x%s"% (short_action[1:])
else:
action=short_action
ifs:
s+=", "
iftid==-1:
got_other_threads=1
s+="other-threads:"
else:
s+="thread 0x%4.4x: %s"% (tid, action)
ifgot_other_threads:
print("extended_continue (%s)"% (s))
else:
print("extended_continue (%s, other-threads: suspend)"% (s))
returnFalse
defrsp_vCont(options, cmd, cmd_args, rsp):
ifcmd_args=="?":
# Skip the leading 'vCont;'
rsp=rsp[6:]
modes=rsp.split(";")
s="%s: supported extended continue modes include: "% (cmd)
fori, modeinenumerate(modes):
ifi:
s+=", "
ifmode=="c":
s+="continue"
elifmode=="C":
s+="continue with signal"
elifmode=="s":
s+="step"
elifmode=="S":
s+="step with signal"
elifmode=="t":
s+="stop"
# else:
# s += 'unrecognized vCont mode: ', str(mode)
print(s)
elifrsp:
ifrsp[0] =="T"orrsp[0] =="S"orrsp[0] =="W"orrsp[0] =="X":
rsp_stop_reply(options, cmd, cmd_args, rsp)
return
ifrsp[0] =="O":
print("stdout: %s"% (rsp))
return
else:
print(
"not supported (cmd = '%s', args = '%s', rsp = '%s')"% (cmd, cmd_args, rsp)
)
defcmd_vAttach(options, cmd, args):
(extra_command, args) =args.split(";")
ifextra_command:
print("%s%s(%s)"% (cmd, extra_command, args))
else:
print("attach(pid = %u)"%int(args, 16))
returnFalse
defcmd_qRegisterInfo(options, cmd, args):
print("query_register_info(reg_num=%i)"% (int(args, 16)))
returnFalse
defrsp_qRegisterInfo(options, cmd, cmd_args, rsp):
globalg_max_register_info_name_len
print("query_register_info(reg_num=%i):"% (int(cmd_args, 16)), end=" ")
iflen(rsp) ==3andrsp[0] =="E":
g_max_register_info_name_len=0
forreg_infoing_register_infos:
name_len=len(reg_info.name())
ifg_max_register_info_name_len<name_len:
g_max_register_info_name_len=name_len
print(" DONE")
else:
packet=Packet(rsp)
reg_info=RegisterInfo(packet.get_key_value_pairs())
g_register_infos.append(reg_info)
print(reg_info)
returnFalse
defcmd_qThreadInfo(options, cmd, args):
ifcmd=="qfThreadInfo":
query_type="first"
else:
query_type="subsequent"
print("get_current_thread_list(type=%s)"% (query_type))
returnFalse
defrsp_qThreadInfo(options, cmd, cmd_args, rsp):
packet=Packet(rsp)
response_type=packet.get_char()
ifresponse_type=="m":
tids=packet.split_hex(";", "big")
fori, tidinenumerate(tids):
ifi:
print(",", end=" ")
print("0x%x"% (tid), end=" ")
print()
elifresponse_type=="l":
print("END")
defrsp_hex_big_endian(options, cmd, cmd_args, rsp):
ifrsp=="":
print("%s%s is not supported"% (cmd, cmd_args))
else:
packet=Packet(rsp)
uval=packet.get_hex_uint("big")
print("%s: 0x%x"% (cmd, uval))
defcmd_read_mem_bin(options, cmd, args):
# x0x7fff5fc39200,0x200
packet=Packet(args)
addr=packet.get_hex_uint("big")
comma=packet.get_char()