Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LLDB: Extend the dump_page methods #4420

Merged
merged 3 commits into from
Apr 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 74 additions & 13 deletions misc/lldb_cruby.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,68 @@ def dump_bits(target, result, page, object_address, end = "\n"):
check_bits(page, "wb_unprotected_bits", bitmap_index, bitmap_bit, "U"),
), end=end, file=result)

def dump_page(debugger, command, result, internal_dict):
class HeapPageIter:
def __init__(self, page, target):
self.page = page
self.target = target
self.start = page.GetChildMemberWithName('start').GetValueAsUnsigned();
self.num_slots = page.GetChildMemberWithName('total_slots').unsigned
self.counter = 0
self.tRBasic = target.FindFirstType("struct RBasic")
self.tRValue = target.FindFirstType("struct RVALUE")

def __iter__(self):
return self

def __next__(self):
if self.counter < self.num_slots:
obj_addr_i = self.start + (self.counter * self.tRValue.GetByteSize())
obj_addr = lldb.SBAddress(obj_addr_i, self.target)
slot_info = (self.counter, obj_addr_i, self.target.CreateValueFromAddress("object", obj_addr, self.tRBasic))
self.counter += 1

return slot_info
else:
raise StopIteration


def dump_page_internal(page, target, process, thread, frame, result, debugger, highlight=None):
if not ('RUBY_Qfalse' in globals()):
lldb_init(debugger)

ruby_type_map = ruby_types(debugger)

freelist = []
fl_start = page.GetChildMemberWithName('freelist').GetValueAsUnsigned()
tRVALUE = target.FindFirstType("struct RVALUE")

while fl_start > 0:
freelist.append(fl_start)
obj_addr = lldb.SBAddress(fl_start, target)
obj = target.CreateValueFromAddress("object", obj_addr, tRVALUE)
fl_start = obj.GetChildMemberWithName("as").GetChildMemberWithName("free").GetChildMemberWithName("next").GetValueAsUnsigned()

for (page_index, obj_addr, obj) in HeapPageIter(page, target):
dump_bits(target, result, page, obj_addr, end= " ")
flags = obj.GetChildMemberWithName('flags').GetValueAsUnsigned()
flType = flags & RUBY_T_MASK

flidx = ' '
if flType == RUBY_T_NONE:
try:
flidx = "%3d" % freelist.index(obj_addr)
except ValueError:
flidx = ' '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like objects that are not T_NONE will get an empty { } in the output for flidx. I think this is some confusing output for objects that are not T_NONE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that was the intended behaviour. It allows you to easily visualise which slots are part of the freelist and which ones aren't. Like this:

bits: [     ] T_NONE     [401]: {  2} Addr: 0x1007ebed0 (flags: 0x0)
bits: [     ] T_NONE     [402]: {  1} Addr: 0x1007ebef8 (flags: 0x0)
bits: [     ] T_NONE     [403]: {  0} Addr: 0x1007ebf20 (flags: 0x0)
bits: [LM   ] T_CLASS    [404]: {   } Addr: 0x1007ebf48 (flags: 0x100001062)
bits: [LM   ] T_PAYLOAD  [405]: {   } Addr: 0x1007ebf70 (flags: 0x3077)

It's easy to see where slots have been assigned values and where the freelist pointer (and list) is going. Although I suspect we could make it more obvious in the output that { } was the freelist index? How about:

bits: [     ] T_NONE     idx: [401] freelist_idx: {  2} Addr: 0x1007ebed0 (flags: 0x0)
bits: [     ] T_NONE     idx: [402] freelist_idx: {  1} Addr: 0x1007ebef8 (flags: 0x0)
bits: [     ] T_NONE     idx: [403] freelist_idx: {  0} Addr: 0x1007ebf20 (flags: 0x0)
bits: [LM   ] T_CLASS    idx: [404] freelist_idx: {   } Addr: 0x1007ebf48 (flags: 0x100001062)
bits: [LM   ] T_PAYLOAD  idx: [405] freelist_idx: {   } Addr: 0x1007ebf70 (flags: 0x3077)

the empty { } mirrors the behaviour of rp - it shows an empty [ ] section when no bits are set

bits: [     ]
T_NONE: (RBasic) *$35 = (flags = 0x0000000000000000, klass = 0x00000001007ebea8)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your second one 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'll patch that and push it up.


result_str = "%s idx: [%3d] freelist_idx: {%s} Addr: %0#x (flags: %0#x)" % (rb_type(flags, ruby_type_map), page_index, flidx, obj_addr, flags)

if highlight == obj_addr:
result_str = ' '.join([result_str, "<<<<<"])

print(result_str, file=result)


def dump_page(debugger, command, result, internal_dict):
target = debugger.GetSelectedTarget()
process = target.GetProcess()
thread = process.GetSelectedThread()
Expand All @@ -540,21 +598,23 @@ def dump_page(debugger, command, result, internal_dict):
page = frame.EvaluateExpression(command)
page = page.Cast(tHeapPageP)

tRBasic = target.FindFirstType("struct RBasic")
tRValue = target.FindFirstType("struct RVALUE")
dump_page_internal(page, target, process, thread, frame, result, debugger)

obj_address = page.GetChildMemberWithName('start').GetValueAsUnsigned();
num_slots = page.GetChildMemberWithName('total_slots').unsigned

ruby_type_map = ruby_types(debugger)
def dump_page_rvalue(debugger, command, result, internal_dict):
target = debugger.GetSelectedTarget()
process = target.GetProcess()
thread = process.GetSelectedThread()
frame = thread.GetSelectedFrame()

val = frame.EvaluateExpression(command)
page = get_page(lldb, target, val)
page_type = target.FindFirstType("struct heap_page").GetPointerType()
page.Cast(page_type)

dump_page_internal(page, target, process, thread, frame, result, debugger, highlight=val.GetValueAsUnsigned())


for j in range(0, num_slots):
offset = obj_address + (j * tRValue.GetByteSize())
obj_addr = lldb.SBAddress(offset, target)
p = target.CreateValueFromAddress("object", obj_addr, tRBasic)
dump_bits(target, result, page, offset, end = " ")
flags = p.GetChildMemberWithName('flags').GetValueAsUnsigned()
print("%s [%3d]: Addr: %0#x (flags: %0#x)" % (rb_type(flags, ruby_type_map), j, offset, flags), file=result)

def rb_type(flags, ruby_types):
flType = flags & RUBY_T_MASK
Expand Down Expand Up @@ -586,6 +646,7 @@ def __lldb_init_module(debugger, internal_dict):
debugger.HandleCommand("command script add -f lldb_cruby.heap_page_body heap_page_body")
debugger.HandleCommand("command script add -f lldb_cruby.rb_backtrace rbbt")
debugger.HandleCommand("command script add -f lldb_cruby.dump_page dump_page")
debugger.HandleCommand("command script add -f lldb_cruby.dump_page_rvalue dump_page_rvalue")

lldb_init(debugger)
print("lldb scripts for ruby has been installed.")