[DGD] Control block dumper

Raymond Jennings shentino at gmail.com
Tue Dec 18 14:13:04 CET 2018


Ok, I tried to send this as an attachment but it got bounced.

Anyway, the following patch will allow DGD to dump control blocks to a
debug file when they're restored from a snapshot.

It will print the name of the object, followed by a list of variable
declarations, followed by a list of function declarations.

So far it's proved useful in checking a snapshot's conformity to the
associated LPC files.  This in turn should help my ongoing work with
skotoslib.

diff --git a/src/sdata.cpp b/src/sdata.cpp
index 88bb8297..e49914de 100644
--- a/src/sdata.cpp
+++ b/src/sdata.cpp
@@ -155,6 +155,7 @@ static sector nctrl; /* # control blocks */
 static sector ndata; /* # dataspace blocks */
 static bool conv_14; /* convert arrays & strings? */
 static bool converted; /* conversion complete? */
+static FILE *cdump;


 /*
@@ -169,6 +170,7 @@ void d_init()
     nctrl = ndata = 0;
     conv_14 = FALSE;
     converted = FALSE;
+    cdump = fopen("../cdump", "w");
 }

 /*
@@ -2718,6 +2720,218 @@ static Dataspace *d_conv_dataspace(Object
*obj, Uint *counttab,
     return data;
 }

+static void dump_class(char c)
+{
+ if (c & C_UNDEFINED) {
+ fprintf(cdump, "extern ");
+ }
+
+ if (c & C_PRIVATE) {
+ fprintf(cdump, "private ");
+ } else if (c & C_STATIC) {
+ fprintf(cdump, "static ");
+ }
+
+ if (c & C_NOMASK) {
+ fprintf(cdump, "nomask ");
+ }
+
+ if (c & C_ATOMIC) {
+ fprintf(cdump, "atomic ");
+ }
+}
+
+static void dump_type(char type)
+{
+ switch (type & T_TYPE) {
+ case T_INT:
+ fprintf(cdump, "int");
+ break;
+
+ case T_FLOAT:
+ fprintf(cdump, "float");
+ break;
+
+ case T_STRING:
+ fprintf(cdump, "string");
+ break;
+
+ case T_OBJECT:
+ fprintf(cdump, "object");
+ break;
+
+ case T_ARRAY:
+ fprintf(cdump, "array");
+ break;
+
+ case T_MAPPING:
+ fprintf(cdump, "mapping");
+ break;
+
+ case T_CLASS:
+ fprintf(cdump, "class");
+ break;
+
+ case T_MIXED:
+ fprintf(cdump, "mixed");
+ break;
+
+ case T_VOID:
+ fprintf(cdump, "void");
+ break;
+
+ default:
+ fprintf(cdump, "something");
+ break;
+ }
+}
+
+static void dump_stars(char type)
+{
+ for (int n = (type & T_REF) >> REFSHIFT; --n >= 0; ) {
+ fprintf(cdump, "*");
+ }
+}
+
+static char *dump_argument(char *args)
+{
+ char ptype;
+ int sclass;
+
+ ptype = *args++;
+
+ if ((ptype & T_TYPE) == T_CLASS) {
+ FETCH3U(args, sclass);
+ }
+
+ dump_type(ptype);
+
+ if (ptype & T_REF) {
+ fprintf(cdump, " ");
+ dump_stars(ptype);
+ }
+
+ return args;
+}
+
+static void prime_ctrl(Control *ctrl)
+{
+ dvardef *vds;
+ dfuncdef *fds;
+
+ vds = d_get_vardefs(ctrl);
+
+ for (int sz = ctrl->nvardefs; --sz >= 0; ) {
+ d_get_strconst(ctrl, vds[sz].inherit, vds[sz].index);
+ }
+
+ fds = d_get_funcdefs(ctrl);
+
+ for (int sz = ctrl->nfuncdefs; --sz >= 0; ) {
+ d_get_strconst(ctrl, fds[sz].inherit, fds[sz].index);
+ }
+}
+
+static void dump_ctrl(Object *obj)
+{
+ Control *ctrl;
+ int sz;
+
+ ctrl = obj->ctrl;
+
+ prime_ctrl(ctrl);
+
+ fprintf(cdump, "/%s\n", obj->name);
+
+ sz = ctrl->nvardefs;
+
+ if (sz) {
+ dvardef *vds;
+
+ vds = d_get_vardefs(ctrl);
+
+ for (int i = 0; i < sz; i++) {
+ String *str;
+ char vtype;
+
+ fprintf(cdump, "\t");
+
+ dump_class(vds[i].sclass);
+
+ vtype = vds[i].type;
+
+ dump_type(vtype);
+ fprintf(cdump, " ");
+ dump_stars(vtype);
+
+ str = d_get_strconst(ctrl, vds[i].inherit, vds[i].index);
+ fwrite(str->text, 1, str->len, cdump);
+ fprintf(cdump, ";\n");
+ }
+
+ fprintf(cdump, "\n");
+ }
+
+ sz = ctrl->nfuncdefs;
+
+ if (sz) {
+ dfuncdef *fds;
+ char *pc;
+
+ fds = d_get_funcdefs(ctrl);
+ pc = d_get_prog(ctrl);
+
+ for (int i = 0; i < sz; i++) {
+ String *str;
+ char *proto;
+ int c;
+ char ftype;
+
+ proto = pc + fds[i].offset;
+
+ c = fds[i].sclass;
+
+ fprintf(cdump, "\t");
+
+ dump_class(c);
+ ftype = PROTO_FTYPE(proto);
+
+ dump_type(ftype);
+ fprintf(cdump, " ");
+ dump_stars(ftype);
+
+ str = d_get_strconst(ctrl, fds[i].inherit, fds[i].index);
+ fwrite(str->text, 1, str->len, cdump);
+ fprintf(cdump, "(");
+
+ char *args;
+ char nargs;
+
+ nargs = PROTO_NARGS(proto);
+ args = PROTO_ARGS(proto);
+
+ if (nargs) {
+ for (int a = 0; a < nargs - 1; a++) {
+ args = dump_argument(args);
+ fprintf(cdump, ", ");
+ }
+ args = dump_argument(args);
+ }
+
+ fprintf(cdump, ")");
+ fprintf(cdump, ";\n");
+ }
+
+ fprintf(cdump, "\n");
+ }
+
+ if (!ctrl->nvardefs && !ctrl->nfuncdefs) {
+ fprintf(cdump, "\n");
+ }
+
+ fflush(cdump);
+}
+
 /*
  * NAME: data->restore_ctrl()
  * DESCRIPTION: restore a control block
@@ -2745,6 +2959,8 @@ Control *d_restore_ctrl(Object *obj, Uint instance,
  }
  d_save_control(ctrl);
  obj->ctrl = ctrl;
+
+ dump_ctrl(obj);
     }

     return ctrl;



More information about the DGD mailing list