Skip to content

Instantly share code, notes, and snippets.

@mraleph
Last active September 30, 2015 15:34
Show Gist options
  • Save mraleph/94e3c6091ece4c91a640 to your computer and use it in GitHub Desktop.
Save mraleph/94e3c6091ece4c91a640 to your computer and use it in GitHub Desktop.
local level = 2
while true do
local info = debug.getinfo (level)
if not info then break end
p(string.format("-- #%d | [%s] function %s at %s:%d (defined at %d)", level, tostring(info.func), info.name or "?", info.source or "", info.currentline or -1, info.linedefined or -1))
local localIdx = 1
while true do
local name, value = debug.getlocal(level, localIdx)
if not name then break end
local strValue = tostring(value)
p(string.format(" %s = %s", name, strValue))
localIdx = localIdx + 1
end
p("")
level = level + 1
end
diff --git a/src/lib_ffi.c b/src/lib_ffi.c
index b2b2d37..1a3c7b5 100644
--- a/src/lib_ffi.c
+++ b/src/lib_ffi.c
@@ -7,6 +7,7 @@
#define LUA_LIB
#include <errno.h>
+#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
@@ -278,6 +279,45 @@ LJLIB_CF(ffi_meta___unm) LJLIB_REC(cdata_arith MM_unm)
}
/* End of contiguous ORDER MM. */
+void xxx_print_cdata(lua_State *L, GCcdata *cd) {
+ const char *msg = "cdata<%s>: %p";
+ CTypeID id = cd->ctypeid;
+ void *p = cdataptr(cd);
+ if (id == CTID_CTYPEID) {
+ msg = "ctype<%s>";
+ id = *(CTypeID *)p;
+ } else {
+ CTState *cts = ctype_cts(L);
+ CType *ct = ctype_raw(cts, id);
+ if (ctype_isref(ct->info)) {
+ p = *(void **)p;
+ ct = ctype_rawchild(cts, ct);
+ }
+ if (ctype_iscomplex(ct->info)) {
+ fprintf(stderr, "%s",
+ strdata(lj_ctype_repr_complex(L, cdataptr(cd), ct->size)));
+ goto checkgc;
+ } else if (ct->size == 8 && ctype_isinteger(ct->info)) {
+ fprintf(stderr, "%s", strdata(lj_ctype_repr_int64(L, *(uint64_t *)cdataptr(cd),
+ (ct->info & CTF_UNSIGNED))));
+ goto checkgc;
+ } else if (ctype_isfunc(ct->info)) {
+ p = *(void **)p;
+ } else if (ctype_isenum(ct->info)) {
+ msg = "cdata<%s>: %d";
+ p = (void *)(uintptr_t)*(uint32_t **)p;
+ } else {
+ if (ctype_isptr(ct->info)) {
+ p = cdata_getptr(p, ct->size);
+ ct = ctype_rawchild(cts, ct);
+ }
+ }
+ }
+ fprintf(stderr, msg, strdata(lj_ctype_repr(L, id, NULL)), p);
+checkgc:
+ return;
+}
+
LJLIB_CF(ffi_meta___tostring)
{
GCcdata *cd = ffi_checkcdata(L, 1);
@@ -548,6 +588,20 @@ LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new)
return 1;
}
+void lj_ctype_dump(CTState *cts, CType *ct, int level);
+
+LJLIB_CF(ffi_dump)
+{
+ CTState *cts = ctype_cts(L);
+ GCcdata *cd = ffi_checkcdata(L, 1);
+ CTypeID id = ffi_checkctype(L, cts, L->base+1);
+ CType *ct = ctype_get(cts, id);
+ fprintf(stderr, "dumping %p\n", cd);
+ lj_ctype_dump(cts, ct, 1);
+ return 0;
+}
+
+
LJLIB_CF(ffi_typeof) LJLIB_REC(.)
{
CTState *cts = ctype_cts(L);
diff --git a/src/lj_asm_x86.h b/src/lj_asm_x86.h
index 941d091..4d255f5 100644
--- a/src/lj_asm_x86.h
+++ b/src/lj_asm_x86.h
@@ -1346,6 +1346,28 @@ static void asm_ahuvload(ASMState *as, IRIns *ir)
emit_u32(as, LJ_TISNUM);
emit_mrm(as, XO_ARITHi, XOg_CMP, RID_MRM);
} else {
+#if 0
+ if (ir->o == IR_HLOAD && irt_type(ir->t) == IRT_FALSE &&
+ IR(ir->op1)->o == IR_HREF &&
+ IR(IR(ir->op1)->op2)->o == IR_ULOAD) {
+ IRRef ref = ir->op1;
+ int found = 0;
+ while (IR(++ref) != ir) {
+ IRIns *load = IR(ref);
+ if (load->op1 == ir->op1 &&
+ irt_type(load->t) == IRT_FALSE) {
+ found = 1;
+ break;
+ }
+ }
+
+ if (found) {
+ MCLabel l_skip = emit_label(as);
+ emit_i8(as, 0xcc);
+ emit_sjcc(as, CC_E, l_skip);
+ }
+ }
+#endif
emit_i8(as, irt_toitype(ir->t));
emit_mrm(as, XO_ARITHi8, XOg_CMP, RID_MRM);
}
diff --git a/src/lj_ctype.c b/src/lj_ctype.c
index eda070c..48c75f8 100644
--- a/src/lj_ctype.c
+++ b/src/lj_ctype.c
@@ -5,6 +5,8 @@
#include "lj_obj.h"
+#include <stdio.h>
+
#if LJ_HASFFI
#include "lj_gc.h"
@@ -264,6 +266,24 @@ CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name, CTSize *ofs,
return NULL; /* Not found. */
}
+/* Get a struct/union/enum/function field by name. */
+void lj_ctype_dump(CTState *cts, CType *ct, int level)
+{
+ while (ct->sib) {
+ ct = ctype_get(cts, ct->sib);
+ fprintf(stderr, "%*s [%p] %s\n", level, "", strref(ct->name), strdata(strref(ct->name)));
+ if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
+ CType *cct = ctype_child(cts, ct);
+ CTInfo q = 0;
+ while (ctype_isattrib(cct->info)) {
+ if (ctype_attrib(cct->info) == CTA_QUAL) q |= cct->size;
+ cct = ctype_child(cts, cct);
+ }
+ lj_ctype_dump(cts, cct, level + 1);
+ }
+ }
+}
+
/* -- C type information -------------------------------------------------- */
/* Follow references and get raw type for a C type ID. */
diff --git a/src/lj_snap.c b/src/lj_snap.c
index fa9abb7..1bd6262 100644
--- a/src/lj_snap.c
+++ b/src/lj_snap.c
@@ -25,6 +25,8 @@
#include "lj_ctype.h"
#include "lj_cdata.h"
#endif
+#include <stdio.h>
+#include <inttypes.h>
/* Some local macros to save typing. Undef'd at the end. */
#define IR(ref) (&J->cur.ir[(ref)])
@@ -788,6 +790,8 @@ static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex,
}
}
+void xxx_print_cdata(lua_State *L, GCcdata *cd);
+
/* Restore interpreter state from exit state with the help of a snapshot. */
const BCIns *lj_snap_restore(jit_State *J, void *exptr)
{
@@ -831,6 +835,15 @@ const BCIns *lj_snap_restore(jit_State *J, void *exptr)
}
snap_unsink(J, T, ex, snapno, rfilt, ir, o);
dupslot:
+ /*
+ fprintf(stderr, "UNSUNK [%p|%p] <- %" PRIx64 "\n", o, L->base, o->u64);
+ */
+ if (tviscdata(o)) {
+ fprintf(stderr, "UNSUNK CDATA:");
+ xxx_print_cdata(L, cdataV(o));
+ fprintf(stderr, "\n");
+ }
+
continue;
}
snap_restoreval(J, T, ex, snapno, rfilt, ref, o);
@@ -842,6 +855,7 @@ const BCIns *lj_snap_restore(jit_State *J, void *exptr)
lua_assert(!LJ_FR2); /* TODO_FR2: store 64 bit PCs. */
/* Overwrite tag with frame link. */
setframe_ftsz(o, snap_slot(sn) != 0 ? (int32_t)*flinks-- : ftsz0);
+ // fprintf(stderr, "LINK [%p|%p] <- %" PRIx64 "\n", o, L->base, o->u64);
L->base = o+1;
}
}
local function dump(m)
local p = ffi.cast("uint32_t*", m)
local t = { }
for i = 0, 15 do
table.insert(t, string.format("0x%x", p[i]))
end
print(string.format("{%s}", table.concat(t, ',')))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment