Skip to content

Commit 2b8ff75

Browse files
committed
add MSVC str providers
1 parent 009994a commit 2b8ff75

File tree

2 files changed

+114
-35
lines changed

2 files changed

+114
-35
lines changed

src/etc/lldb_commands

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
# Forces test-compliant formatting to all other types
2+
type summary add -F _ -e -x -h "^.*$" --category Rust
13
# Std String
24
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)String$" --category Rust
35
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)String$" --category Rust
46
# Std str
57
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?str$" --category Rust
68
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?str$" --category Rust
9+
## MSVC
10+
type synthetic add -l lldb_lookup.MSVCStrSyntheticProvider -x "^ref(_mut)?\$<str\$>$" --category Rust
11+
type summary add -F lldb_lookup.StdStrSummaryProvider -e -h -x "^ref(_mut)?\$<str\$>$" --category Rust
712
# Array
813
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?\\[.+\\]$" --category Rust
914
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?\\[.+\\]$" --category Rust
1015
# Slice
1116
## MSVC
12-
type synthetic add -l lldb_lookup.MSVCStdSliceSyntheticProvider -x "^ref\$<slice2\$<.+> >" --category Rust
17+
type synthetic add -l lldb_lookup.MSVCStdSliceSyntheticProvider -x "^ref(_mut)?\$<slice2\$<.+> >" --category Rust
1318
type summary add -F lldb_lookup.StdSliceSummaryProvider -e -x -h "^ref(_mut)?\$<slice2\$<.+> >" --category Rust
1419
# OsString
1520
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::ffi::([a-z_]+::)+)OsString$" --category Rust
@@ -71,6 +76,4 @@ type synthetic add -l lldb_lookup.synthetic_lookup -x "^\(.*\)$" --category Rust
7176
## MSVC
7277
type synthetic add -l lldb_lookup.MSVCTupleSyntheticProvider -x "^tuple\$<.+>$" --category Rust
7378
type summary add -F lldb_lookup.TupleSummaryProvider -e -x -h "^tuple\$<.+>$" --category Rust
74-
# Forces test-compliant formatting to all other types
75-
type summary add -F _ -e -x -h "^.*$" --category Rust
7679
type category enable Rust

src/etc/lldb_providers.py

Lines changed: 108 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,47 @@ def has_children(self) -> bool:
266266
return True
267267

268268

269+
class MSVCStrSyntheticProvider:
270+
__slots__ = ["valobj", "data_ptr", "length"]
271+
272+
def __init__(self, valobj: SBValue, _dict: LLDBOpaque):
273+
self.valobj = valobj
274+
self.update()
275+
276+
def update(self):
277+
self.data_ptr = self.valobj.GetChildMemberWithName("data_ptr")
278+
self.length = self.valobj.GetChildMemberWithName("length").GetValueAsUnsigned()
279+
280+
def has_children(self) -> bool:
281+
return True
282+
283+
def num_children(self) -> int:
284+
return self.length
285+
286+
def get_child_index(self, name: str) -> int:
287+
index = name.lstrip("[").rstrip("]")
288+
if index.isdigit():
289+
return int(index)
290+
291+
return -1
292+
293+
def get_child_at_index(self, index: int) -> SBValue:
294+
if not 0 <= index < self.length:
295+
return None
296+
start = self.data_ptr.GetValueAsUnsigned()
297+
address = start + index
298+
element = self.data_ptr.CreateValueFromAddress(
299+
f"[{index}]", address, self.data_ptr.GetType().GetPointeeType()
300+
)
301+
return element
302+
303+
def get_type_name(self):
304+
if self.valobj.GetTypeName().startswith("ref_mut"):
305+
return "&mut str"
306+
else:
307+
return "&str"
308+
309+
269310
class ClangEncodedEnumProvider:
270311
"""Pretty-printer for 'clang-encoded' enums support implemented in LLDB"""
271312

@@ -327,6 +368,7 @@ def _getCurrentVariantIndex(self, all_variants: SBValue) -> int:
327368
default_index = i
328369
return default_index
329370

371+
330372
class MSVCEnumSyntheticProvider:
331373
"""
332374
Synthetic provider for sum-type enums on MSVC. For a detailed explanation of the internals,
@@ -336,6 +378,7 @@ class MSVCEnumSyntheticProvider:
336378
"""
337379

338380
__slots__ = ["valobj", "variant", "value"]
381+
339382
def __init__(self, valobj: SBValue, _dict: LLDBOpaque):
340383
self.valobj = valobj
341384
self.variant: SBValue
@@ -362,30 +405,44 @@ def update(self):
362405
).GetValueAsUnsigned()
363406
if tag == discr:
364407
self.variant = child
365-
self.value = child.GetChildMemberWithName("value").GetSyntheticValue()
408+
self.value = child.GetChildMemberWithName(
409+
"value"
410+
).GetSyntheticValue()
366411
return
367412
else: # if invalid, DISCR must be a range
368-
begin: int = variant_type.GetStaticFieldWithName(
369-
"DISCR_BEGIN"
370-
).GetConstantValue(self.valobj.target).GetValueAsUnsigned()
371-
end: int = variant_type.GetStaticFieldWithName(
372-
"DISCR_END"
373-
).GetConstantValue(self.valobj.target).GetValueAsUnsigned()
413+
begin: int = (
414+
variant_type.GetStaticFieldWithName("DISCR_BEGIN")
415+
.GetConstantValue(self.valobj.target)
416+
.GetValueAsUnsigned()
417+
)
418+
end: int = (
419+
variant_type.GetStaticFieldWithName("DISCR_END")
420+
.GetConstantValue(self.valobj.target)
421+
.GetValueAsUnsigned()
422+
)
374423

375424
# begin isn't necessarily smaller than end, so we must test for both cases
376425
if begin < end:
377426
if begin <= tag <= end:
378427
self.variant = child
379-
self.value = child.GetChildMemberWithName("value").GetSyntheticValue()
428+
self.value = child.GetChildMemberWithName(
429+
"value"
430+
).GetSyntheticValue()
380431
return
381432
else:
382433
if tag >= begin or tag <= end:
383434
self.variant = child
384-
self.value = child.GetChildMemberWithName("value").GetSyntheticValue()
435+
self.value = child.GetChildMemberWithName(
436+
"value"
437+
).GetSyntheticValue()
385438
return
386439
else: # if invalid, tag is a 128 bit value
387-
tag_lo: int = self.valobj.GetChildMemberWithName("tag128_lo").GetValueAsUnsigned()
388-
tag_hi: int = self.valobj.GetChildMemberWithName("tag128_hi").GetValueAsUnsigned()
440+
tag_lo: int = self.valobj.GetChildMemberWithName(
441+
"tag128_lo"
442+
).GetValueAsUnsigned()
443+
tag_hi: int = self.valobj.GetChildMemberWithName(
444+
"tag128_hi"
445+
).GetValueAsUnsigned()
389446

390447
tag: int = (tag_hi << 64) | tag_lo
391448

@@ -399,30 +456,44 @@ def update(self):
399456
)
400457

401458
if exact_lo.IsValid():
402-
exact_lo: int = exact_lo.GetConstantValue(self.valobj.target).GetValueAsUnsigned()
403-
exact_hi: int = variant_type.GetStaticFieldWithName(
404-
"DISCR128_EXACT_HI"
405-
).GetConstantValue(self.valobj.target).GetValueAsUnsigned()
459+
exact_lo: int = exact_lo.GetConstantValue(
460+
self.valobj.target
461+
).GetValueAsUnsigned()
462+
exact_hi: int = (
463+
variant_type.GetStaticFieldWithName("DISCR128_EXACT_HI")
464+
.GetConstantValue(self.valobj.target)
465+
.GetValueAsUnsigned()
466+
)
406467

407468
discr: int = (exact_hi << 64) | exact_lo
408469
if tag == discr:
409470
self.variant = child
410-
self.value = child.GetChildMemberWithName("value").GetSyntheticValue()
471+
self.value = child.GetChildMemberWithName(
472+
"value"
473+
).GetSyntheticValue()
411474
return
412475
else: # if invalid, DISCR must be a range
413-
begin_lo: int = variant_type.GetStaticFieldWithName(
414-
"DISCR128_BEGIN_LO"
415-
).GetConstantValue(self.valobj.target).GetValueAsUnsigned()
416-
begin_hi: int = variant_type.GetStaticFieldWithName(
417-
"DISCR128_BEGIN_HI"
418-
).GetConstantValue(self.valobj.target).GetValueAsUnsigned()
419-
420-
end_lo: int = variant_type.GetStaticFieldWithName(
421-
"DISCR128_END_LO"
422-
).GetConstantValue(self.valobj.target).GetValueAsUnsigned()
423-
end_hi: int = variant_type.GetStaticFieldWithName(
424-
"DISCR128_END_HI"
425-
).GetConstantValue(self.valobj.target).GetValueAsUnsigned()
476+
begin_lo: int = (
477+
variant_type.GetStaticFieldWithName("DISCR128_BEGIN_LO")
478+
.GetConstantValue(self.valobj.target)
479+
.GetValueAsUnsigned()
480+
)
481+
begin_hi: int = (
482+
variant_type.GetStaticFieldWithName("DISCR128_BEGIN_HI")
483+
.GetConstantValue(self.valobj.target)
484+
.GetValueAsUnsigned()
485+
)
486+
487+
end_lo: int = (
488+
variant_type.GetStaticFieldWithName("DISCR128_END_LO")
489+
.GetConstantValue(self.valobj.target)
490+
.GetValueAsUnsigned()
491+
)
492+
end_hi: int = (
493+
variant_type.GetStaticFieldWithName("DISCR128_END_HI")
494+
.GetConstantValue(self.valobj.target)
495+
.GetValueAsUnsigned()
496+
)
426497

427498
begin = (begin_hi << 64) | begin_lo
428499
end = (end_hi << 64) | end_lo
@@ -431,12 +502,16 @@ def update(self):
431502
if begin < end:
432503
if begin <= tag <= end:
433504
self.variant = child
434-
self.value = child.GetChildMemberWithName("value").GetSyntheticValue()
505+
self.value = child.GetChildMemberWithName(
506+
"value"
507+
).GetSyntheticValue()
435508
return
436509
else:
437510
if tag >= begin or tag <= end:
438511
self.variant = child
439-
self.value = child.GetChildMemberWithName("value").GetSyntheticValue()
512+
self.value = child.GetChildMemberWithName(
513+
"value"
514+
).GetSyntheticValue()
440515
return
441516

442517
def num_children(self) -> int:
@@ -498,7 +573,7 @@ def MSVCEnumSummaryProvider(valobj: SBValue, _dict: LLDBOpaque) -> str:
498573
if vars[-1][-1] == ")":
499574
vars[-1] = vars[-1][:-1]
500575

501-
return f'{name}{{{", ".join(vars)}}}'
576+
return f"{name}{{{', '.join(vars)}}}"
502577

503578

504579
class TupleSyntheticProvider:
@@ -681,6 +756,7 @@ def get_type_name(self) -> str:
681756

682757
return "".join([ref, "[", name, "]"])
683758

759+
684760
def StdSliceSummaryProvider(valobj, dict):
685761
output = sequence_formatter("[", valobj, dict)
686762
output += "]"

0 commit comments

Comments
 (0)