2017-05-27 20:04:31 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#coding: utf-8
|
|
|
|
#
|
|
|
|
# Usage: run `command script import -r misc/lldb_cruby.py` on LLDB
|
|
|
|
#
|
2017-08-03 12:39:25 +03:00
|
|
|
# Test: misc/test_lldb_cruby.rb
|
|
|
|
#
|
2017-05-27 20:04:31 +03:00
|
|
|
|
|
|
|
import lldb
|
|
|
|
import commands
|
|
|
|
import os
|
|
|
|
import shlex
|
|
|
|
|
2017-05-28 17:17:25 +03:00
|
|
|
def lldb_init(debugger):
|
|
|
|
target = debugger.GetSelectedTarget()
|
|
|
|
global SIZEOF_VALUE
|
|
|
|
SIZEOF_VALUE = target.FindFirstType("VALUE").GetByteSize()
|
2017-08-02 08:27:25 +03:00
|
|
|
|
|
|
|
value_types = []
|
2017-05-28 17:17:25 +03:00
|
|
|
g = globals()
|
|
|
|
for enum in target.FindFirstGlobalVariable('ruby_dummy_gdb_enums'):
|
|
|
|
enum = enum.GetType()
|
|
|
|
members = enum.GetEnumMembers()
|
|
|
|
for i in xrange(0, members.GetSize()):
|
|
|
|
member = members.GetTypeEnumMemberAtIndex(i)
|
|
|
|
name = member.GetName()
|
|
|
|
value = member.GetValueAsUnsigned()
|
|
|
|
g[name] = value
|
2017-05-27 20:04:31 +03:00
|
|
|
|
2017-08-02 08:27:25 +03:00
|
|
|
if name.startswith('RUBY_T_'):
|
|
|
|
value_types.append(name)
|
|
|
|
g['value_types'] = value_types
|
|
|
|
|
|
|
|
def string2cstr(rstring):
|
|
|
|
"""Returns the pointer to the C-string in the given String object"""
|
|
|
|
flags = rstring.GetValueForExpressionPath(".basic->flags").unsigned
|
|
|
|
if flags & RUBY_T_MASK != RUBY_T_STRING:
|
|
|
|
raise TypeError("not a string")
|
|
|
|
if flags & RUBY_FL_USER1:
|
|
|
|
cptr = int(rstring.GetValueForExpressionPath(".as.heap.ptr").value, 0)
|
|
|
|
clen = int(rstring.GetValueForExpressionPath(".as.heap.len").value, 0)
|
|
|
|
else:
|
|
|
|
cptr = int(rstring.GetValueForExpressionPath(".as.ary").value, 0)
|
|
|
|
clen = (flags & RSTRING_EMBED_LEN_MASK) >> RSTRING_EMBED_LEN_SHIFT
|
|
|
|
return cptr, clen
|
|
|
|
|
|
|
|
def output_string(ctx, rstring):
|
|
|
|
cptr, clen = string2cstr(rstring)
|
|
|
|
expr = 'printf("%%.*s", (size_t)%d, (const char*)%d)' % (clen, cptr)
|
|
|
|
ctx.frame.EvaluateExpression(expr)
|
|
|
|
|
2017-05-28 17:17:25 +03:00
|
|
|
def fixnum_p(x):
|
|
|
|
return x & RUBY_FIXNUM_FLAG != 0
|
2017-05-27 20:04:31 +03:00
|
|
|
|
2017-05-28 17:17:25 +03:00
|
|
|
def flonum_p(x):
|
|
|
|
return (x&RUBY_FLONUM_MASK) == RUBY_FLONUM_FLAG
|
2017-05-27 20:04:31 +03:00
|
|
|
|
|
|
|
def lldb_rp(debugger, command, result, internal_dict):
|
|
|
|
target = debugger.GetSelectedTarget()
|
|
|
|
process = target.GetProcess()
|
|
|
|
thread = process.GetSelectedThread()
|
|
|
|
frame = thread.GetSelectedFrame()
|
2017-05-31 05:42:10 +03:00
|
|
|
if frame.IsValid():
|
|
|
|
val = frame.EvaluateExpression(command)
|
|
|
|
else:
|
|
|
|
val = target.EvaluateExpression(command)
|
|
|
|
error = val.GetError()
|
|
|
|
if error.Fail():
|
|
|
|
print >> result, error
|
|
|
|
return
|
2017-05-27 20:04:31 +03:00
|
|
|
num = val.GetValueAsSigned()
|
2017-05-28 17:17:25 +03:00
|
|
|
if num == RUBY_Qfalse:
|
|
|
|
print >> result, 'false'
|
|
|
|
elif num == RUBY_Qtrue:
|
|
|
|
print >> result, 'true'
|
|
|
|
elif num == RUBY_Qnil:
|
|
|
|
print >> result, 'nil'
|
|
|
|
elif num == RUBY_Qundef:
|
2017-05-31 05:42:10 +03:00
|
|
|
print >> result, 'undef'
|
2017-05-28 17:17:25 +03:00
|
|
|
elif fixnum_p(num):
|
|
|
|
print >> result, num >> 1
|
|
|
|
elif flonum_p(num):
|
|
|
|
debugger.HandleCommand("print rb_float_value(%0#x)" % val.GetValueAsUnsigned())
|
|
|
|
elif num & RUBY_IMMEDIATE_MASK:
|
|
|
|
print >> result, 'immediate(%x)' % num
|
2017-05-27 20:04:31 +03:00
|
|
|
else:
|
|
|
|
tRBasic = target.FindFirstType("struct RBasic").GetPointerType()
|
|
|
|
val = val.Cast(tRBasic)
|
|
|
|
flags = val.GetValueForExpressionPath("->flags").GetValueAsUnsigned()
|
2017-05-28 17:17:25 +03:00
|
|
|
if (flags & RUBY_FL_PROMOTED) == RUBY_FL_PROMOTED:
|
|
|
|
print >> result, "[PROMOTED] "
|
2017-05-27 20:04:31 +03:00
|
|
|
flType = flags & RUBY_T_MASK
|
2017-05-28 17:17:25 +03:00
|
|
|
if flType == RUBY_T_NONE:
|
|
|
|
print >> result, 'T_NONE: %s' % val.Dereference()
|
|
|
|
elif flType == RUBY_T_NIL:
|
|
|
|
print >> result, 'T_NIL: %s' % val.Dereference()
|
|
|
|
elif flType == RUBY_T_OBJECT:
|
|
|
|
tRObject = target.FindFirstType("struct RObject").GetPointerType()
|
|
|
|
val = val.Cast(tRObject)
|
|
|
|
print >> result, 'T_OBJECT: %s' % val.Dereference()
|
|
|
|
elif flType == RUBY_T_STRING:
|
2017-05-27 20:04:31 +03:00
|
|
|
tRString = target.FindFirstType("struct RString").GetPointerType()
|
|
|
|
val = val.Cast(tRString)
|
|
|
|
if flags & RSTRING_NOEMBED:
|
2017-05-28 17:17:25 +03:00
|
|
|
print >> result, val.GetValueForExpressionPath("->as.heap")
|
2017-05-27 20:04:31 +03:00
|
|
|
else:
|
2017-05-28 17:17:25 +03:00
|
|
|
print >> result, val.GetValueForExpressionPath("->as.ary")
|
2017-05-27 20:04:31 +03:00
|
|
|
elif flType == RUBY_T_ARRAY:
|
|
|
|
tRArray = target.FindFirstType("struct RArray").GetPointerType()
|
|
|
|
val = val.Cast(tRArray)
|
|
|
|
if flags & RUBY_FL_USER1:
|
|
|
|
len = ((flags & (RUBY_FL_USER3|RUBY_FL_USER4)) >> (RUBY_FL_USHIFT+3))
|
2017-05-28 17:17:25 +03:00
|
|
|
print >> result, "T_ARRAY: len=%d (embed)" % len
|
2017-05-27 20:04:31 +03:00
|
|
|
if len == 0:
|
2017-05-28 17:17:25 +03:00
|
|
|
print >> result, "{(empty)}"
|
2017-05-27 20:04:31 +03:00
|
|
|
else:
|
2017-05-28 17:17:25 +03:00
|
|
|
print >> result, val.GetValueForExpressionPath("->as.ary")
|
2017-05-27 20:04:31 +03:00
|
|
|
else:
|
|
|
|
len = val.GetValueForExpressionPath("->as.heap.len").GetValueAsSigned()
|
2017-05-28 17:17:25 +03:00
|
|
|
print >> result, "T_ARRAY: len=%d " % len
|
|
|
|
#print >> result, val.GetValueForExpressionPath("->as.heap")
|
2017-05-27 20:04:31 +03:00
|
|
|
if flags & RUBY_FL_USER2:
|
|
|
|
shared = val.GetValueForExpressionPath("->as.heap.aux.shared").GetValueAsUnsigned()
|
2017-05-28 17:17:25 +03:00
|
|
|
print >> result, "(shared) shared=%016x " % shared
|
2017-05-27 20:04:31 +03:00
|
|
|
else:
|
|
|
|
capa = val.GetValueForExpressionPath("->as.heap.aux.capa").GetValueAsSigned()
|
2017-05-28 17:17:25 +03:00
|
|
|
print >> result, "(ownership) capa=%d " % capa
|
2017-05-27 20:04:31 +03:00
|
|
|
if len == 0:
|
2017-05-28 17:17:25 +03:00
|
|
|
print >> result, "{(empty)}"
|
2017-05-27 20:04:31 +03:00
|
|
|
else:
|
2017-05-28 17:17:25 +03:00
|
|
|
debugger.HandleCommand("expression -Z %d -fx -- (const VALUE*)((struct RArray*)%d)->as.heap.ptr" % (len, val.GetValueAsUnsigned()))
|
2017-05-27 20:04:31 +03:00
|
|
|
debugger.HandleCommand("p (struct RArray *) %0#x" % val.GetValueAsUnsigned())
|
|
|
|
|
2017-08-02 08:27:25 +03:00
|
|
|
def count_objects(debugger, command, ctx, result, internal_dict):
|
|
|
|
objspace = ctx.frame.EvaluateExpression("ruby_current_vm->objspace")
|
|
|
|
num_pages = objspace.GetValueForExpressionPath(".heap_pages.allocated_pages").unsigned
|
|
|
|
|
|
|
|
counts = {}
|
|
|
|
total = 0
|
|
|
|
for t in range(0x00, RUBY_T_MASK+1):
|
|
|
|
counts[t] = 0
|
|
|
|
|
|
|
|
for i in range(0, num_pages):
|
|
|
|
print "\rcounting... %d/%d" % (i, num_pages),
|
|
|
|
page = objspace.GetValueForExpressionPath('.heap_pages.sorted[%d]' % i)
|
|
|
|
p = page.GetChildMemberWithName('start')
|
|
|
|
num_slots = page.GetChildMemberWithName('total_slots').unsigned
|
|
|
|
for j in range(0, num_slots):
|
|
|
|
obj = p.GetValueForExpressionPath('[%d]' % j)
|
|
|
|
flags = obj.GetValueForExpressionPath('.as.basic.flags').unsigned
|
|
|
|
obj_type = flags & RUBY_T_MASK
|
|
|
|
counts[obj_type] += 1
|
|
|
|
total += num_slots
|
|
|
|
|
|
|
|
print "\rTOTAL: %d, FREE: %d" % (total, counts[0x00])
|
|
|
|
for sym in value_types:
|
|
|
|
print "%s: %d" % (sym, counts[globals()[sym]])
|
|
|
|
|
|
|
|
def stack_dump_raw(debugger, command, ctx, result, internal_dict):
|
|
|
|
ctx.frame.EvaluateExpression("rb_vmdebug_stack_dump_raw_current()")
|
|
|
|
|
|
|
|
def dump_node(debugger, command, ctx, result, internal_dict):
|
|
|
|
args = shlex.split(command)
|
|
|
|
if not args:
|
|
|
|
return
|
|
|
|
node = args[0]
|
|
|
|
|
|
|
|
dump = ctx.frame.EvaluateExpression("(struct RString*)rb_parser_dump_tree((NODE*)(%s), 0)" % node)
|
|
|
|
output_string(ctx, dump)
|
2017-05-27 20:04:31 +03:00
|
|
|
|
|
|
|
def __lldb_init_module(debugger, internal_dict):
|
|
|
|
debugger.HandleCommand("command script add -f lldb_cruby.lldb_rp rp")
|
2017-08-02 08:27:25 +03:00
|
|
|
debugger.HandleCommand("command script add -f lldb_cruby.count_objects rb_count_objects")
|
|
|
|
debugger.HandleCommand("command script add -f lldb_cruby.stack_dump_raw SDR")
|
|
|
|
debugger.HandleCommand("command script add -f lldb_cruby.dump_node dump_node")
|
2017-05-28 17:17:25 +03:00
|
|
|
lldb_init(debugger)
|
2017-05-27 20:04:31 +03:00
|
|
|
print "lldb scripts for ruby has been installed."
|