-
Notifications
You must be signed in to change notification settings - Fork 49
/
bde_printer.py
1521 lines (1181 loc) · 45.9 KB
/
bde_printer.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
"""
GDB pretty printer support for BDE components
This module provides a set of pretty printers to load into gdb for debugging
code using BDE (http://github.com/bloomberg/bde) components.
This is a work in progress, more printers will be added as needed.
Authors: David Rodriguez Ibeas <dribeas@bloomberg.net>
Evgeny Yakimov <eyakimov@bloomberg.net>
Hyman Rosen <hrosen4@bloomberg.net>
Configuration options
---------------------
These settings configure some of the behavior of the pretty printer to
improve access to different elements in the underlying data.
(gdb) set print bslma-allocator on
Setting Meaning
--------------- -----------------------------------------------------
bslma-allocator Controls whether the bslma::Allocator* is printed
bsl-eclipse Controls output format, set to 'on' inside Eclipse
and leave as 'off' for plain gdb.
string-address Controls whether string buffer address is printed
Usage
-----
To use the pretty printers load the script into gdb, either manually
through:
(gdb) python execfile('/path/to/this/script.py')
or automatically at start up. See the gdb documentation on how to setup
automatic loading of pretty printers.
You can list, enable or disable pretty printers by using the gdb commands:
(gdb) info pretty-printer
(gdb) disable pretty-printer global BDE;vector
(gdb) enable pretty-printer global BDE
Additionally, you can ignore the pretty printer for a single 'print'
command by running it in "raw" mode:
(gdb) print /r container
"""
# General design considerations
# -----------------------------
# Pretty printers should focus on the useful information for the developer
# that is debugging the application. What is useful for one developer might
# be too verbose for another and not enough for a third one. Unless
# otherwise noted the provided pretty printers will reformat the available
# information but avoid hiding data from the developer. The implication is
# that the output might be slightly verbose (the 'bslma::Allocator' pointer
# in containers is printed always, the size and capacity for 'bsl::string' is
# printed...). Other existing pretty printers (for example for the standard
# library provided with gcc) will omit details and focus on the data.
#
# The format used for output has been considered for a high information
# density given that it will print more things than needed by most users.
# The description of the format for each one of the pretty printers is
# documented below and does not reflect the layout of the C++ types that are
# being printed.
import re
import string
import sys
import gdb
import gdb.printing
###############################################################################
# Private Types and Helpers
###############################################################################
global docs
docs = {}
global pp
pp = None
## Helpers controlling the printout based on printer options
def _createAllocatorList(cbase):
"""Create a list with the allocator information if 'print bslma-allocator'
is set or empty otherwise.
"""
printAllocator = gdb.parameter("print bslma-allocator")
return [] if not printAllocator else [("alloc", cbase)]
def _optionalAllocator(allocator, prefix=",", suffix=""):
printalloc = gdb.parameter("print bslma-allocator")
return "%salloc:%s%s" % (prefix, allocator, suffix) if printalloc else ""
def keyValueIterator(arg):
eclipseMode = gdb.parameter("print bsl-eclipse")
if eclipseMode:
return RawKeyValueIterator(arg)
else:
return KeyValueIterator(arg)
def valueIterator(arg):
eclipseMode = gdb.parameter("print bsl-eclipse")
if eclipseMode:
return RawValueIterator(arg)
else:
return ValueIterator(arg)
def stringAddress(arg):
char_ptr_type = gdb.lookup_type("unsigned char").pointer()
c_str = arg.cast(char_ptr_type)
return "0x%x " % c_str if gdb.parameter("print string-address") else ""
def stringRep(arg, length):
print_len = gdb.parameter("print elements")
if not print_len or print_len + 4 > length:
print_len = length
print_str = ""
char_ptr_type = gdb.lookup_type("unsigned char").pointer()
c_str = arg.cast(char_ptr_type)
for i in range(print_len):
ci = (c_str + i).dereference()
cc = chr(ci)
if cc in string.printable:
print_str += cc
else:
print_str += "\{0:03o}".format(int(ci))
if print_len < length:
print_str += "..."
return print_str
## Debug catch all pretty printer
class CatchAll:
"""Not a pretty printer
This type complies with the pretty printer interface, but will open an
interactive python shell with the information available to the printer for
debugging and testing purposes.
"""
def __init__(self, val):
"""Store the gdb value in this object and open an interactive shell"""
self.val = val
import code
code.interact(local=locals())
def to_string(self):
import code
code.interact(local=locals())
return "<------>"
class BslStringImp:
"""Pretty printer for 'bsl::String_Imp<char>'
The implementation of 'bsl::string' ('bsl::basic_string<>' template) uses a
base template 'bsl::String_Imp<>' to handle the actual contents. This
implementation uses a small string optimization with an internal buffer
that depends on the architecture. The pretty printer for this type will
print a compact representation of the available information, encoding in
the capitalization of the message whether the current object is using the
small buffer ('size') or the dynamically allocated large buffer ('Size').
The 'print string-address' parameter controls whether the address of the
string buffer is printed.
# With print string-address on
data = 0x0x8051074 [size:5,capacity:19] "short"
# With print string-address off
data = [Size:24,capacity:34] "This is a long string!!!"
The size of the internal buffer is detected at runtime.
Note that the pretty printer only handles narrow character strings,
'bsl::string', and not wide character strings 'bsl::wstring' or any other
specialization of the 'bsl::basic_string<>' template.
The current implementation in BDE will set the length value to
'bsl::string::npos' on destruction. The pretty printer detects this as a
special value and reports that the string has been destroyed. If the
capacity of the string indicates that it was using the small string
optimization it then attempts to print the contents of the buffer, if the
small string optimization is not in place, the pretty printer will attempt
to print the contents of 'd_start_p' (pointer to the string that *has
already been deallocated*). Note that this is a *best effort* with no
guarantees, the object has been destroyed, the value may have already been
reused.
If 'print elements' is set, the value will be used to limit the number of
characters printed, and the string will terminate with a "..." indicating
more characters are present. Non-printable characters are written out as a
backslash and three octal digits.
Note: This is not intended for direct use.
Note: The implementation is not able to print strings with a length
greater or equal to 2^31.
"""
def __init__(self, val):
"""Precalculate the data needed to later print the string"""
self.val = val
length = val["d_length"]
if str(length) == "4294967295":
self.destroyed = True
self.length = int(-1)
else:
self.destroyed = False
self.length = int(val["d_length"])
self.capacity = int(val["d_capacity"])
short = val["d_short"]
self.isShort = self.capacity < short.type.sizeof
self.buffer = (
short["d_data"]["d_buffer"] if self.isShort else val["d_start_p"]
)
def to_string(self):
"""Format the string"""
str = None
if not self.destroyed:
str = '%s[%s:%d,capacity:%d] "%s"' % (
stringAddress(self.buffer),
"size" if self.isShort else "Size",
self.length,
self.capacity,
stringRep(self.buffer, self.length),
)
else:
if self.isShort:
str = "[DESTROYED, small buffer value]: %s" % self.buffer
else:
str = "[DESTROYED] %s" % self.buffer
return str
class BslVectorImp:
"""Printer for 'bsl::vectorBase<T>' specializations.
This pretty printer handles printing instances of the
'bsl::vectorBase<>' template used to hold the contents of
'bsl::vector<>'. The printer will dump the size and capacity of the
object followed by the sequence of values in the sequence.
[size:10,capacity:16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Note: This is not intended for direct use
"""
def __init__(self, val):
self.val = val
self.begin = val["d_dataBegin_p"]
self.end = val["d_dataEnd_p"]
self.size = int(self.end - self.begin)
self.capacity = int(val["d_capacity"])
def to_string(self):
return "[size:%d,capacity:%d]" % (self.size, self.capacity)
def display_hint(self):
return "map"
def children(self):
class VectorContentsIterator:
"""Iterator over the contents of the vector"""
def __init__(s, begin, end):
self.begin = begin
self.end = end
self.current = begin
def __iter__(s):
return s
def __next__(s):
if self.current == self.end:
raise StopIteration
name = int(self.current - self.begin)
value = self.current.dereference()
self.current += 1
return (name, value)
next = __next__
return keyValueIterator(VectorContentsIterator(self.begin, self.end))
class BslRbTreeIterator:
"""Helper class to produce iterations over a RB-tree
This is **not** a pretty printer, but a helper class to aid in the
implementation of pretty printers for sorted associative containers using
RB-Trees as underlying data structure.
"""
def __init__(self, type, sentinel):
self.sentinel = sentinel
self.current = sentinel["d_right_p"]
self.nodeType = gdb.lookup_type(
"BloombergLP::bslstl::TreeNode<%s>" % type
)
def __iter__(self):
return self
def __next__(self):
if self.current == self.sentinel.address:
raise StopIteration
treeNode = self.current.dereference().cast(self.nodeType)
self.current = self.nextNode(self.current)
return treeNode["d_value"]
next = __next__
def followPointer(self, pointer, name):
"""Follow the pointer specified by 'name' in the specified 'object'.
This function implements the equivalent in C++ of:
return pointer->name & ~1
"""
np = pointer.dereference()[name]
npi = np.cast(gdb.lookup_type("long long"))
if npi & 1:
np = gdb.Value(npi & ~1).reinterpret_cast(np.type)
return np
def nextNode(self, pointer):
if pointer["d_right_p"] != 0:
pointer = self.followPointer(pointer, "d_right_p")
l = self.followPointer(pointer, "d_left_p")
while not l == 0:
pointer = l
l = self.followPointer(pointer, "d_left_p")
else:
p = self.followPointer(pointer, "d_parentWithColor_p")
while (
p != self.sentinel.address
and self.followPointer(p, "d_right_p") == pointer
):
pointer = p
p = self.followPointer(p, "d_parentWithColor_p")
pointer = p
return pointer
class HashTableIterator:
"""Helper class to produce iterations over a hash table"""
def __init__(self, type, sentinel):
self.nodeType = gdb.lookup_type(
"BloombergLP::bslalg::BidirectionalNode<%s>" % type
)
self.current = sentinel.cast(self.nodeType.pointer())
def __iter__(self):
return self
def __next__(self):
if self.current == 0:
raise StopIteration
value = self.current.dereference()["d_value"]
self.current = self.current["d_next_p"].cast(self.nodeType.pointer())
return value
next = __next__
class PairTupleIterator:
"""Helper class to convert bsl::pair to a tuple as an iterator"""
def __init__(self, iter):
self.iter = iter
def __iter__(self):
return self
def __next__(self):
nextPair = self.iter.next()
return (nextPair["first"], nextPair["second"])
next = __next__
class KeyValueIterator:
"""This iterator converts an iterator of pairs into 2 alternating tuples."""
def __init__(self, iter):
self.iter = iter
self.item = None
self.count = 0
def __iter__(self):
return self
def __next__(self):
val = None
if self.count % 2 == 0:
self.item = self.iter.next()
val = self.item[0]
else:
val = self.item[1]
ret = ("[%d]" % self.count, val)
self.count = self.count + 1
return ret
next = __next__
class ValueIterator:
"""This iterator returns a ('[i]',value) tuple from an iterator."""
def __init__(self, iter):
self.iter = iter
self.count = 0
def __iter__(self):
return self
def __next__(self):
value = self.iter.next()
ret = ("[%d]" % self.count, value)
self.count = self.count + 1
return ret
next = __next__
class RawKeyValueIterator:
"""This iterator returns a (str(key),value) tuple from an iterator."""
def __init__(self, iter):
self.iter = iter
def __iter__(self):
return self
def __next__(self):
next = self.iter.next()
return (str(next[0]), next[1])
next = __next__
class RawValueIterator:
"""This iterator returns a (str(value),value) tuple from an iterator."""
def __init__(self, iter):
self.iter = iter
def __iter__(self):
return self
def __next__(self):
value = self.iter.next()
return (str(value), value)
next = __next__
###############################################################################
# Public Type Printers
###############################################################################
class IPv4Address:
"""Pretty printer for 'bteso_IPv4Address'
Prints the address in dotted decimal notation with the port separated by a
colon.
192.10.1.5:8194
"""
def __init__(self, val):
self.val = val
self.port = val["d_portNumber"]
ip = int(val["d_address"])
self.a = ip & 0xFF
self.b = (ip >> 8) & 0xFF
self.c = (ip >> 16) & 0xFF
self.d = (ip >> 24) & 0xFF
def to_string(self):
return "%d.%d.%d.%d:%d" % (self.a, self.b, self.c, self.d, self.port)
class Nullable:
"""Pretty printer for 'bdlb::NullableValue<T>'
This pretty printer handles both the allocator aware and not allocator
aware types internally.
The output of the pretty printer is split into a 'null' output and
optionally a 'value' if not null.
"""
def __init__(self, val):
self.val = val
self.type = val.type.template_argument(0)
self.members = []
if val["d_imp"].type.has_key("d_allocator_p"):
alloc = val["d_imp"]["d_allocator_p"]
self.members = _createAllocatorList(alloc)
self.members.append(("null", val["d_imp"]["d_isNull"]))
if not val["d_imp"]["d_isNull"]:
buf = val["d_imp"]["d_buffer"]["d_buffer"]
self.members.append(
("value", buf.cast(self.type.pointer()).dereference())
)
def to_string(self):
return str(self.val.type)
def children(self):
return iter(self.members)
class Time:
"""Pretty printer for 'bdlt::Time'
The value is shown in 'hh:mm:ss.xxx' format.
"""
def __init__(self, val):
self.val = val
@classmethod
def toHMmS(cls, value):
milliseconds = value % 1000
value //= 1000
seconds = value % 60
value //= 60
minutes = value % 60
value //= 60
hours = value
return "%02d:%02d:%02d.%03d" % (hours, minutes, seconds, milliseconds)
@classmethod
def toHMuS(cls, value):
microseconds = value % 1000000
value //= 1000000
seconds = value % 60
value //= 60
minutes = value % 60
value //= 60
hours = value
return "%02d:%02d:%02d.%06d" % (hours, minutes, seconds, microseconds)
def to_string(self):
us = int(self.val["d_value"])
mask = 0x4000000000
if us < mask:
return "invalid time value %d" % us
return Time.toHMuS(us & ~mask)
class Tz:
"""Utility to format a time zone offset."""
@classmethod
def toHM(cls, offset):
offset = int(offset)
if offset < 0:
offset = -offset
sign = "-"
else:
sign = "+"
return "%s%02d:%02d" % (sign, offset // 60, offset % 60)
class TimeTz:
"""Pretty printer for 'bdlt::TimeTz'
The value is shown in 'hh:mm:ss.xxx+hh:mm' format.
"""
def __init__(self, val):
self.val = val
def to_string(self):
time = Time(self.val["d_localTime"]).to_string()
return "%s%s" % (time, Tz.toHM(self.val["d_offset"]))
class Date:
"""Pretty printer for 'bdlt::Date'
The value is shown in 'YYYYY-MM-DD' format.
"""
SEPTEMBER = 9
YEAR_1752 = 1752
YEAR_1601 = 1601
JAN_01_1753 = 639908
JAN_01_1601 = 584401
YEAR_1752_FIRST_MISSING_DAY = 3
YEAR_1752_NUM_MISSING_DAYS = 11
DAYS_IN_NON_LEAP_YEAR = 365
DAYS_IN_LEAP_YEAR = DAYS_IN_NON_LEAP_YEAR + 1
DAYS_IN_4_YEARS = 3 * DAYS_IN_NON_LEAP_YEAR + DAYS_IN_LEAP_YEAR
DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1
DAYS_IN_400_YEARS = 4 * DAYS_IN_100_YEARS + 1
y1752DaysThroughMonth = [
0,
31,
60,
91,
121,
152,
182,
213,
244,
263,
294,
324,
355,
]
normDaysThroughMonth = [
0,
31,
59,
90,
120,
151,
181,
212,
243,
273,
304,
334,
365,
]
leapDaysThroughMonth = [
0,
31,
60,
91,
121,
152,
182,
213,
244,
274,
305,
335,
366,
]
def __init__(self, val):
self.val = val
@classmethod
def serialToYearDate(cls, serialDay):
"""Extract the year and day of the year from the value in 'serialDay'."""
serialDay = int(serialDay)
if serialDay > Date.JAN_01_1753:
y = Date.YEAR_1601 # base year
n = serialDay - Date.JAN_01_1601 # num actual days since 1601/1/1
m = n + Date.YEAR_1752_NUM_MISSING_DAYS - 1
# Compensate for the 11 missing days in September of 1752, and
# the additional leap day in 1700.
z400 = m // Date.DAYS_IN_400_YEARS # num 400-year blocks
y += z400 * 400
m -= z400 * Date.DAYS_IN_400_YEARS # num days since y/1/1 (400)
z100 = m // Date.DAYS_IN_100_YEARS # num 100-year blocks
y += z100 * 100
m -= z100 * Date.DAYS_IN_100_YEARS # num days since y/1/1 (100)
z4 = m // Date.DAYS_IN_4_YEARS # num 4-year blocks
y += z4 * 4
m -= z4 * Date.DAYS_IN_4_YEARS # num days since y/1/1 (4)
z = m // Date.DAYS_IN_NON_LEAP_YEAR # num whole years
y += z
m -= z * Date.DAYS_IN_NON_LEAP_YEAR # num days since y/1/1 (1)
if 0 == m and (4 == z or 4 == z100): # last day in a leap yeear or
# a leap year every 400 years
year = y - 1
dayOfYear = Date.DAYS_IN_LEAP_YEAR
else:
year = y
dayOfYear = m + 1
return (year, dayOfYear)
else:
# Date pre-1753
y = 1 # base year
n = serialDay - 1 # num actual days since 1/1/1
z4 = n // Date.DAYS_IN_4_YEARS # num 4-year blocks
y += z4 * 4
n -= z4 * Date.DAYS_IN_4_YEARS # num days since y/1/1 (4)
z = n // Date.DAYS_IN_NON_LEAP_YEAR # num whole years
y += z
n -= z * Date.DAYS_IN_NON_LEAP_YEAR # num days since y/1/1 (1)
if 4 == z and 0 == n: # last day in a leap year
year = y - 1
dayOfYear = Date.DAYS_IN_LEAP_YEAR
else:
year = y
dayOfYear = n + 1
return (year, dayOfYear)
@classmethod
def isLeapYear(cls, year):
return 0 == year % 4 and (
0 != year % 100 or 0 == year % 400 or year <= 1752
)
@classmethod
def dayOfYearToDayMonth(cls, year, dayOfYear):
if year == Date.YEAR_1752:
daysThroughMonth = Date.y1752DaysThroughMonth
elif Date.isLeapYear(year):
daysThroughMonth = Date.leapDaysThroughMonth
else:
daysThroughMonth = Date.normDaysThroughMonth
m = 0
while daysThroughMonth[m] < dayOfYear:
m = m + 1
d = dayOfYear - daysThroughMonth[m - 1]
if (
year == Date.YEAR_1752
and m == Date.SEPTEMBER
and d >= Date.YEAR_1752_FIRST_MISSING_DAY
):
d += Date.YEAR_1752_NUM_MISSING_DAYS
return (m, d)
@classmethod
def toYMD(cls, serialDay):
(year, dayOfYear) = Date.serialToYearDate(serialDay)
(month, day) = Date.dayOfYearToDayMonth(year, dayOfYear)
return "%04d-%02d-%02d" % (year, month, day)
def to_string(self):
return Date.toYMD(self.val["d_serialDate"])
class DateTz:
"""Pretty printer for 'bdlt::DateTz'
The value is shown in 'YYYYY-MM-DDT00+hh:mm' format.
"""
def __init__(self, val):
self.val = val
def to_string(self):
date = Date(self.val["d_localDate"]).to_string()
return "%sT00%s" % (date, Tz.toHM(self.val["d_offset"]))
class Datetime:
"""Pretty printer for 'bdlt::Datetime'
The value is shown in 'YYYYY-MM-DDTHH:MM:SS.SSSSSS' format.
"""
REP_MASK = 0x08000000000000000
DATE_MASK = 0x0FFFFFFE000000000
TIME_MASK = 0x00000001FFFFFFFFF
MASK_32 = 0x000000000FFFFFFFF
SHIFT_32 = 32
TIME_BITS = 37
def __init__(self, val):
self.val = val
def to_string(self):
value = int(self.val["d_value"])
if value < 0:
value += 2 ** 64
invalid = (value & Datetime.REP_MASK) == 0
if invalid:
if sys.byteorder == "little":
days = (value & Datetime.MASK_32) - 1
milliseconds = value >> Datetime.SHIFT_32
else:
days = (value >> Datetime.SHIFT_32) - 1
milliseconds = value & Datetime.MASK_32
value = (days << Datetime.TIME_BITS) | (1000 * milliseconds)
else:
value ^= Datetime.REP_MASK
date = Date.toYMD((value >> Datetime.TIME_BITS) + 1)
time = Time.toHMuS(value & Datetime.TIME_MASK)
return "%s%sT%s" % (("[invalid]" if invalid else ""), date, time)
class DatetimeTz:
"""Pretty printer for 'bdlt::DatetimeTz'
The value is shown in 'YYYYY-MM-DDThh:mm:ss.ssssss+hh:mm' format.
"""
def __init__(self, val):
self.val = val
def to_string(self):
datetime = Datetime(self.val["d_localDatetime"]).to_string()
return "%s%s" % (datetime, Tz.toHM(self.val["d_offset"]))
class BslString:
"""Printer for 'bsl::string'.
The pretty printer for 'bsl::string' ('bsl::basic_string<char>') uses the
pretty printer for 'StringImp'. See the documentation below to interpret
the printout.
string = {
alloc = 0x804fdf0 <BloombergLP::g_newDeleteAllocatorSingleton>,
data = [size:11,capacity:19] "Hello there"
}
Note that while common pretty printers for 'std::string' will only dump
the contents ("Hello there"), printing out the allocator
('bslma::Allocator*') helps detect bugs by which a member of a type might
not be using the same allocator as the container. The size and, to lesser
extent, capacity and use of small string optimization can help detect other
issues and do not add too much verbosity to the output.
See also: 'BslStringImp'
"""
def __init__(self, val):
self.val = val
self.alloc = val["d_allocator"]["d_mechanism"]
self.simp = val.cast(val.type.items()[0][1].type)
self.members = _createAllocatorList(self.alloc)
self.members.append(("data", self.simp))
def to_string(self):
return "bsl::string"
def children(self):
return iter(self.members)
class StringRefData:
"""Printer for bslstl::StringRef implementation data
The format of the output is [length:6] "abcdef"
"""
def __init__(self, val):
self.val = val
def to_string(self):
length = self.val["d_end_p"] - self.val["d_begin_p"]
buffer = self.val["d_begin_p"]
return '%s[length:%d] "%s"' % (
stringAddress(buffer),
length,
stringRep(buffer, length),
)
class StringRef:
"""Printer for bslstl::StringRef
The format of the output is bslstl::StringRef = {data = [length:2] "ab"}
"""
def __init__(self, val):
self.val = val
self.imp = val.cast(val.type.items()[0][1].type)
self.members = [("data", self.imp)]
def to_string(self):
return "bslstl::StringRef"
def children(self):
return iter(self.members)
class BslVector:
"""Printer for 'bsl::vector<T,bsl::allocator<T>>'
The pretty printer for specializations of 'bsl::vector<>' is implemented
in terms of the 'VectorImp' pretty printer.
vector = {
alloc = 0x804fdf0 <BloombergLP::g_newDeleteAllocatorSingleton>,
data = [size:10,capacity:16] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5}
}
See also 'BslVectorImp'
"""
def __init__(self, val):
self.val = val
self.alloc = val["d_allocator"]["d_mechanism"]
self.vimp = val.cast(val.type.items()[0][1].type)
self.members = _createAllocatorList(self.alloc)
self.members.append(("data", self.vimp))
def to_string(self):
return str(self.val.type)
def children(self):
return iter(self.members)
class BslMap:
"""Printer for a bsl::map<K,V>
The implementation of 'bsl::map' uses type erasure to minimize the amount
of code. The type holds two members, the first one wraps the polymorphic
allocator and comparator, while the second member is a generic RB-tree.
Since the internal tree does not hold the information of what data is
stored in the node, the pretty printer is forced to dump the contents of
the map directly in this pretty printer, forcing the 'to_string' to dump
all state other than the actual contents.
"""
def __init__(self, val):
self.val = val
self.keyArg = val.type.template_argument(0)
self.valueArg = val.type.template_argument(1)
self.valueType = gdb.lookup_type(
"bsl::pair<%s, %s >" % (self.keyArg.const(), self.valueArg)
)
self.size = val["d_tree"]["d_numNodes"]
self.alloc = val["d_compAndAlloc"]["d_pool"]["d_pool"]["d_mechanism"]
self.sentinel = val["d_tree"]["d_sentinel"]
def to_string(self):
# Locally handle the printing the allocator or not
return "map<%s,%s> [size:%d%s]" % (
self.keyArg,
self.valueArg,
self.size,
_optionalAllocator(self.alloc),
)
def display_hint(self):
return "map"
def children(self):
return keyValueIterator(
PairTupleIterator(BslRbTreeIterator(self.valueType, self.sentinel))
)
class BslSet:
"""Printer for a bsl::set
The implementation of 'bsl::set' uses type erasure to minimize the amount
of code. The type holds two members, the first one wraps the polymorphic
allocator and comparator, while the second member is a generic RB-tree.
Since the internal tree does not hold the information of what data is
stored in the node, the pretty printer is forced to dump the contents of
the set directly in this pretty printer, forcing the 'to_string' to dump
all state other than the actual contents.
"""
def __init__(self, val):
self.val = val
self.valueType = val.type.template_argument(0)
self.nodeType = gdb.lookup_type(
"BloombergLP::bslstl::TreeNode<%s>" % self.valueType
)
self.size = val["d_tree"]["d_numNodes"]
self.alloc = val["d_compAndAlloc"]["d_pool"]["d_pool"]["d_mechanism"]
self.sentinel = val["d_tree"]["d_sentinel"]
def to_string(self):
# Locally handle the printing the allocator or not
return "set<%s> [size:%d%s]" % (
self.valueType,
self.size,