2017-05-27 13:04:31 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#coding: utf-8
|
|
|
|
#
|
|
|
|
# Usage: run `command script import -r misc/lldb_cruby.py` on LLDB
|
|
|
|
#
|
2017-08-03 05:39:25 -04:00
|
|
|
# Test: misc/test_lldb_cruby.rb
|
|
|
|
#
|
2017-05-27 13:04:31 -04:00
|
|
|
|
2019-09-24 08:05:29 -04:00
|
|
|
from __future__ import print_function
|
2017-05-27 13:04:31 -04:00
|
|
|
import lldb
|
|
|
|
import os
|
|
|
|
import shlex
|
|
|
|
|
2017-05-28 10:17:25 -04:00
|
|
|
def lldb_init(debugger):
|
|
|
|
target = debugger.GetSelectedTarget()
|
|
|
|
global SIZEOF_VALUE
|
|
|
|
SIZEOF_VALUE = target.FindFirstType("VALUE").GetByteSize()
|
2017-08-02 01:27:25 -04:00
|
|
|
|
|
|
|
value_types = []
|
2017-05-28 10:17:25 -04:00
|
|
|
g = globals()
|
|
|
|
for enum in target.FindFirstGlobalVariable('ruby_dummy_gdb_enums'):
|
|
|
|
enum = enum.GetType()
|
|
|
|
members = enum.GetEnumMembers()
|
2019-09-24 08:05:29 -04:00
|
|
|
for i in range(0, members.GetSize()):
|
2017-05-28 10:17:25 -04:00
|
|
|
member = members.GetTypeEnumMemberAtIndex(i)
|
|
|
|
name = member.GetName()
|
|
|
|
value = member.GetValueAsUnsigned()
|
|
|
|
g[name] = value
|
2017-05-27 13:04:31 -04:00
|
|
|
|
2017-08-02 01:27:25 -04: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"""
|
2019-10-08 20:01:11 -04:00
|
|
|
if rstring.TypeIsPointerType():
|
|
|
|
rstring = rstring.Dereference()
|
2017-08-02 01:27:25 -04:00
|
|
|
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:
|
2019-10-08 20:01:11 -04:00
|
|
|
cptr = int(rstring.GetValueForExpressionPath(".as.ary").location, 0)
|
2017-08-02 01:27:25 -04:00
|
|
|
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 10:17:25 -04:00
|
|
|
def fixnum_p(x):
|
|
|
|
return x & RUBY_FIXNUM_FLAG != 0
|
2017-05-27 13:04:31 -04:00
|
|
|
|
2017-05-28 10:17:25 -04:00
|
|
|
def flonum_p(x):
|
|
|
|
return (x&RUBY_FLONUM_MASK) == RUBY_FLONUM_FLAG
|
2017-05-27 13:04:31 -04:00
|
|
|
|
2018-10-04 10:00:39 -04:00
|
|
|
def static_sym_p(x):
|
|
|
|
return (x&~(~0<<RUBY_SPECIAL_SHIFT)) == RUBY_SYMBOL_FLAG
|
|
|
|
|
2018-10-03 00:07:16 -04:00
|
|
|
def append_command_output(debugger, command, result):
|
|
|
|
output1 = result.GetOutput()
|
|
|
|
debugger.GetCommandInterpreter().HandleCommand(command, result)
|
|
|
|
output2 = result.GetOutput()
|
|
|
|
result.Clear()
|
|
|
|
result.write(output1)
|
|
|
|
result.write(output2)
|
|
|
|
|
2017-05-27 13:04:31 -04:00
|
|
|
def lldb_rp(debugger, command, result, internal_dict):
|
2018-10-02 15:14:24 -04:00
|
|
|
if not ('RUBY_Qfalse' in globals()):
|
|
|
|
lldb_init(debugger)
|
|
|
|
|
2017-05-27 13:04:31 -04:00
|
|
|
target = debugger.GetSelectedTarget()
|
|
|
|
process = target.GetProcess()
|
|
|
|
thread = process.GetSelectedThread()
|
|
|
|
frame = thread.GetSelectedFrame()
|
2017-05-30 22:42:10 -04:00
|
|
|
if frame.IsValid():
|
|
|
|
val = frame.EvaluateExpression(command)
|
|
|
|
else:
|
|
|
|
val = target.EvaluateExpression(command)
|
|
|
|
error = val.GetError()
|
|
|
|
if error.Fail():
|
2019-09-24 08:05:29 -04:00
|
|
|
print(error, file=result)
|
2017-05-30 22:42:10 -04:00
|
|
|
return
|
2018-10-20 08:48:07 -04:00
|
|
|
lldb_inspect(debugger, target, result, val)
|
|
|
|
|
|
|
|
def lldb_inspect(debugger, target, result, val):
|
2017-05-27 13:04:31 -04:00
|
|
|
num = val.GetValueAsSigned()
|
2017-05-28 10:17:25 -04:00
|
|
|
if num == RUBY_Qfalse:
|
2019-09-24 08:05:29 -04:00
|
|
|
print('false', file=result)
|
2017-05-28 10:17:25 -04:00
|
|
|
elif num == RUBY_Qtrue:
|
2019-09-24 08:05:29 -04:00
|
|
|
print('true', file=result)
|
2017-05-28 10:17:25 -04:00
|
|
|
elif num == RUBY_Qnil:
|
2019-09-24 08:05:29 -04:00
|
|
|
print('nil', file=result)
|
2017-05-28 10:17:25 -04:00
|
|
|
elif num == RUBY_Qundef:
|
2019-09-24 08:05:29 -04:00
|
|
|
print('undef', file=result)
|
2017-05-28 10:17:25 -04:00
|
|
|
elif fixnum_p(num):
|
2019-09-24 08:05:29 -04:00
|
|
|
print(num >> 1, file=result)
|
2017-05-28 10:17:25 -04:00
|
|
|
elif flonum_p(num):
|
2018-10-03 05:54:21 -04:00
|
|
|
append_command_output(debugger, "print rb_float_value(%0#x)" % val.GetValueAsUnsigned(), result)
|
2018-10-04 10:00:39 -04:00
|
|
|
elif static_sym_p(num):
|
|
|
|
if num < 128:
|
2019-09-24 08:05:29 -04:00
|
|
|
print("T_SYMBOL: %c" % num, file=result)
|
2018-10-04 10:00:39 -04:00
|
|
|
else:
|
2019-09-24 08:05:29 -04:00
|
|
|
print("T_SYMBOL: (%x)" % num, file=result)
|
2017-05-28 10:17:25 -04:00
|
|
|
elif num & RUBY_IMMEDIATE_MASK:
|
2019-09-24 08:05:29 -04:00
|
|
|
print('immediate(%x)' % num, file=result)
|
2017-05-27 13:04:31 -04:00
|
|
|
else:
|
|
|
|
tRBasic = target.FindFirstType("struct RBasic").GetPointerType()
|
|
|
|
val = val.Cast(tRBasic)
|
|
|
|
flags = val.GetValueForExpressionPath("->flags").GetValueAsUnsigned()
|
2017-05-28 10:17:25 -04:00
|
|
|
if (flags & RUBY_FL_PROMOTED) == RUBY_FL_PROMOTED:
|
2019-09-24 08:05:29 -04:00
|
|
|
print("[PROMOTED] ", file=result)
|
2019-05-09 15:27:44 -04:00
|
|
|
if (flags & RUBY_FL_FREEZE) == RUBY_FL_FREEZE:
|
2019-09-24 08:05:29 -04:00
|
|
|
print("[FROZEN] ", file=result)
|
2017-05-27 13:04:31 -04:00
|
|
|
flType = flags & RUBY_T_MASK
|
2017-05-28 10:17:25 -04:00
|
|
|
if flType == RUBY_T_NONE:
|
2019-09-24 08:05:29 -04:00
|
|
|
print('T_NONE: %s' % val.Dereference(), file=result)
|
2017-05-28 10:17:25 -04:00
|
|
|
elif flType == RUBY_T_NIL:
|
2019-09-24 08:05:29 -04:00
|
|
|
print('T_NIL: %s' % val.Dereference(), file=result)
|
2017-05-28 10:17:25 -04:00
|
|
|
elif flType == RUBY_T_OBJECT:
|
|
|
|
tRObject = target.FindFirstType("struct RObject").GetPointerType()
|
|
|
|
val = val.Cast(tRObject)
|
2019-09-24 08:05:29 -04:00
|
|
|
print('T_OBJECT: %s' % val.Dereference(), file=result)
|
2018-10-03 05:54:25 -04:00
|
|
|
elif flType == RUBY_T_CLASS or flType == RUBY_T_MODULE or flType == RUBY_T_ICLASS:
|
|
|
|
tRClass = target.FindFirstType("struct RClass").GetPointerType()
|
|
|
|
val = val.Cast(tRClass)
|
2019-09-24 08:05:29 -04:00
|
|
|
print('T_%s: %s' % ('CLASS' if flType == RUBY_T_CLASS else 'MODULE' if flType == RUBY_T_MODULE else 'ICLASS', val.Dereference()), file=result)
|
2017-05-28 10:17:25 -04:00
|
|
|
elif flType == RUBY_T_STRING:
|
2017-05-27 13:04:31 -04:00
|
|
|
tRString = target.FindFirstType("struct RString").GetPointerType()
|
2019-10-08 20:04:37 -04:00
|
|
|
ptr, len = string2cstr(val.Cast(tRString))
|
|
|
|
append_command_output(debugger, "print *(const char (*)[%d])%0#x" % (len, ptr), result)
|
2018-10-04 10:00:39 -04:00
|
|
|
elif flType == RUBY_T_SYMBOL:
|
|
|
|
tRSymbol = target.FindFirstType("struct RSymbol").GetPointerType()
|
2019-09-24 08:05:29 -04:00
|
|
|
print(val.Cast(tRSymbol).Dereference(), file=result)
|
2017-05-27 13:04:31 -04: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))
|
2018-10-03 00:07:16 -04:00
|
|
|
ptr = val.GetValueForExpressionPath("->as.ary")
|
2017-05-27 13:04:31 -04:00
|
|
|
else:
|
|
|
|
len = val.GetValueForExpressionPath("->as.heap.len").GetValueAsSigned()
|
2018-10-03 00:07:16 -04:00
|
|
|
ptr = val.GetValueForExpressionPath("->as.heap.ptr")
|
2019-09-24 08:05:29 -04:00
|
|
|
#print(val.GetValueForExpressionPath("->as.heap"), file=result)
|
2018-10-03 00:07:16 -04:00
|
|
|
result.write("T_ARRAY: len=%d" % len)
|
|
|
|
if flags & RUBY_FL_USER1:
|
|
|
|
result.write(" (embed)")
|
|
|
|
elif flags & RUBY_FL_USER2:
|
|
|
|
shared = val.GetValueForExpressionPath("->as.heap.aux.shared").GetValueAsUnsigned()
|
2019-10-02 16:26:39 -04:00
|
|
|
result.write(" (shared) shared=%016x" % shared)
|
2018-10-03 00:07:16 -04:00
|
|
|
else:
|
|
|
|
capa = val.GetValueForExpressionPath("->as.heap.aux.capa").GetValueAsSigned()
|
|
|
|
result.write(" (ownership) capa=%d" % capa)
|
|
|
|
if len == 0:
|
|
|
|
result.write(" {(empty)}")
|
|
|
|
else:
|
|
|
|
result.write("\n")
|
|
|
|
append_command_output(debugger, "expression -Z %d -fx -- (const VALUE*)%0#x" % (len, ptr.GetValueAsUnsigned()), result)
|
2018-10-03 00:27:19 -04:00
|
|
|
elif flType == RUBY_T_HASH:
|
2018-10-03 05:54:21 -04:00
|
|
|
append_command_output(debugger, "p *(struct RHash *) %0#x" % val.GetValueAsUnsigned(), result)
|
2018-10-03 00:27:19 -04:00
|
|
|
elif flType == RUBY_T_BIGNUM:
|
|
|
|
tRBignum = target.FindFirstType("struct RBignum").GetPointerType()
|
|
|
|
val = val.Cast(tRBignum)
|
|
|
|
if flags & RUBY_FL_USER2:
|
|
|
|
len = ((flags & (RUBY_FL_USER3|RUBY_FL_USER4|RUBY_FL_USER5)) >> (RUBY_FL_USHIFT+3))
|
2019-09-24 08:05:29 -04:00
|
|
|
print("T_BIGNUM: len=%d (embed)" % len, file=result)
|
2018-10-03 00:27:19 -04:00
|
|
|
append_command_output(debugger, "print ((struct RBignum *) %0#x)->as.ary" % val.GetValueAsUnsigned(), result)
|
|
|
|
else:
|
|
|
|
len = val.GetValueForExpressionPath("->as.heap.len").GetValueAsSigned()
|
2019-09-24 08:05:29 -04:00
|
|
|
print("T_BIGNUM: len=%d" % len, file=result)
|
|
|
|
print(val.Dereference(), file=result)
|
2018-10-03 00:27:19 -04:00
|
|
|
append_command_output(debugger, "expression -Z %x -fx -- (const BDIGIT*)((struct RBignum*)%d)->as.heap.digits" % (len, val.GetValueAsUnsigned()), result)
|
|
|
|
# append_command_output(debugger, "x ((struct RBignum *) %0#x)->as.heap.digits / %d" % (val.GetValueAsUnsigned(), len), result)
|
|
|
|
elif flType == RUBY_T_FLOAT:
|
|
|
|
tRFloat = target.FindFirstType("struct RFloat").GetPointerType()
|
|
|
|
val = val.Cast(tRFloat)
|
2018-10-03 05:54:21 -04:00
|
|
|
append_command_output(debugger, "p *(double *)%0#x" % val.GetValueForExpressionPath("->float_value").GetAddress(), result)
|
2018-10-20 08:48:07 -04:00
|
|
|
elif flType == RUBY_T_RATIONAL:
|
|
|
|
tRRational = target.FindFirstType("struct RRational").GetPointerType()
|
|
|
|
val = val.Cast(tRRational)
|
|
|
|
lldb_inspect(debugger, target, result, val.GetValueForExpressionPath("->num"))
|
|
|
|
output = result.GetOutput()
|
|
|
|
result.Clear()
|
|
|
|
result.write("(Rational) " + output.rstrip() + " / ")
|
|
|
|
lldb_inspect(debugger, target, result, val.GetValueForExpressionPath("->den"))
|
2018-10-20 13:19:37 -04:00
|
|
|
elif flType == RUBY_T_COMPLEX:
|
|
|
|
tRComplex = target.FindFirstType("struct RComplex").GetPointerType()
|
|
|
|
val = val.Cast(tRComplex)
|
|
|
|
lldb_inspect(debugger, target, result, val.GetValueForExpressionPath("->real"))
|
|
|
|
real = result.GetOutput().rstrip()
|
|
|
|
result.Clear()
|
|
|
|
lldb_inspect(debugger, target, result, val.GetValueForExpressionPath("->imag"))
|
|
|
|
imag = result.GetOutput().rstrip()
|
|
|
|
result.Clear()
|
|
|
|
if not imag.startswith("-"):
|
|
|
|
imag = "+" + imag
|
2019-09-24 08:05:29 -04:00
|
|
|
print("(Complex) " + real + imag + "i", file=result)
|
2019-04-02 17:13:22 -04:00
|
|
|
elif flType == RUBY_T_REGEXP:
|
2019-04-01 18:53:34 -04:00
|
|
|
tRRegex = target.FindFirstType("struct RRegexp").GetPointerType()
|
|
|
|
val = val.Cast(tRRegex)
|
2019-09-25 03:58:24 -04:00
|
|
|
print("(Regex) ->src {", file=result)
|
2019-04-01 18:53:34 -04:00
|
|
|
lldb_inspect(debugger, target, result, val.GetValueForExpressionPath("->src"))
|
2019-09-24 08:05:29 -04:00
|
|
|
print("}", file=result)
|
2018-10-01 10:48:54 -04:00
|
|
|
elif flType == RUBY_T_DATA:
|
|
|
|
tRTypedData = target.FindFirstType("struct RTypedData").GetPointerType()
|
|
|
|
val = val.Cast(tRTypedData)
|
|
|
|
flag = val.GetValueForExpressionPath("->typed_flag")
|
|
|
|
if flag.GetValueAsUnsigned() == 1:
|
2019-09-24 08:05:29 -04:00
|
|
|
print("T_DATA: %s" % val.GetValueForExpressionPath("->type->wrap_struct_name"), file=result)
|
2018-10-03 00:07:16 -04:00
|
|
|
append_command_output(debugger, "p *(struct RTypedData *) %0#x" % val.GetValueAsUnsigned(), result)
|
2018-10-01 10:48:54 -04:00
|
|
|
else:
|
2019-09-24 08:05:29 -04:00
|
|
|
print("T_DATA:", file=result)
|
2018-10-03 00:07:16 -04:00
|
|
|
append_command_output(debugger, "p *(struct RData *) %0#x" % val.GetValueAsUnsigned(), result)
|
2019-08-29 17:51:34 -04:00
|
|
|
elif flType == RUBY_T_NODE:
|
|
|
|
tRTypedData = target.FindFirstType("struct RNode").GetPointerType()
|
|
|
|
nd_type = (flags & RUBY_NODE_TYPEMASK) >> RUBY_NODE_TYPESHIFT
|
|
|
|
append_command_output(debugger, "p (node_type) %d" % nd_type, result)
|
|
|
|
val = val.Cast(tRTypedData)
|
|
|
|
append_command_output(debugger, "p *(struct RNode *) %0#x" % val.GetValueAsUnsigned(), result)
|
2018-10-03 00:27:19 -04:00
|
|
|
else:
|
2019-09-24 08:05:29 -04:00
|
|
|
print("Not-handled type %0#x" % flType, file=result)
|
|
|
|
print(val, file=result)
|
2017-05-27 13:04:31 -04:00
|
|
|
|
2017-08-02 01:27:25 -04: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):
|
2019-09-24 08:05:29 -04:00
|
|
|
print("\rcounting... %d/%d" % (i, num_pages), end="")
|
2017-08-02 01:27:25 -04:00
|
|
|
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
|
|
|
|
|
2019-09-25 03:58:24 -04:00
|
|
|
print("\rTOTAL: %d, FREE: %d" % (total, counts[0x00]))
|
2017-08-02 01:27:25 -04:00
|
|
|
for sym in value_types:
|
2019-09-24 08:05:29 -04:00
|
|
|
print("%s: %d" % (sym, counts[globals()[sym]]))
|
2017-08-02 01:27:25 -04:00
|
|
|
|
|
|
|
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 13:04:31 -04:00
|
|
|
|
|
|
|
def __lldb_init_module(debugger, internal_dict):
|
|
|
|
debugger.HandleCommand("command script add -f lldb_cruby.lldb_rp rp")
|
2017-08-02 01:27:25 -04: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 10:17:25 -04:00
|
|
|
lldb_init(debugger)
|
2019-09-24 08:05:29 -04:00
|
|
|
print("lldb scripts for ruby has been installed.")
|