1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*
* SPL - The SPL Programming Language
* Copyright (C) 2004, 2005 Clifford Wolf <clifford@clifford.at>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* dump.c: Dump the SPL VM state
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <utime.h>
#ifdef ENABLE_PTHREAD_SUPPORT
#include <pthread.h>
#endif
#include "spl.h"
static int dump_roof;
static int dump_counter;
static int dump_pass;
static FILE *dump_file;
static struct spl_vm *dump_vm_ptr;
#ifdef ENABLE_PTHREAD_SUPPORT
static pthread_mutex_t dump_lck = PTHREAD_MUTEX_INITIALIZER;
#endif
static void print_string(char *string)
{
unsigned char *ustring = (unsigned char *)string;
fputc('(', dump_file);
for (int i=0; ustring[i]; i++)
switch (ustring[i]) {
case 'a' ... 'z':
case 'A' ... 'Z':
case '0' ... '9':
case ':': case '#':
case '+': case '-':
case '_': case ',':
case '.': case ' ':
fputc(ustring[i], dump_file);
break;
default:
fprintf(dump_file, "%%%02X", ustring[i]);
}
fputc(')', dump_file);
}
static void print_data(unsigned char *data, int size)
{
fputc('{', dump_file);
for (int i=0; i<size; i++)
fprintf(dump_file, "%02X", data[i]);
fputc('}', dump_file);
}
static void dump_code(struct spl_code *code)
{
const char *sha1;
if ( !code ) return;
if ( dump_pass ) {
if (code->dump_tag != 1) return;
code->dump_tag = dump_counter++;
} else {
if (code->dump_tag == 1) return;
code->dump_tag = 1;
dump_roof++;
}
if ( dump_pass ) {
fprintf(dump_file, "CODE=%d CODE_TYPE=%d SIZE=%d ID=",
code->dump_tag, code->code_type, code->size);
if ( code->id )
fprintf(dump_file, "(%s)", code->id);
fprintf(dump_file, " SHAONE=");
if ( (sha1=spl_code_sha1(code)) != 0 )
fprintf(dump_file, "(%s)", sha1);
fprintf(dump_file, " CODE=");
if ( sha1 && dump_vm_ptr->codecache_dir ) {
int fn_size = strlen(dump_vm_ptr->codecache_dir) + 100;
char fn[fn_size];
snprintf(fn, fn_size, "%s/%s.splb",
dump_vm_ptr->codecache_dir, sha1);
if (utime(fn, NULL) != 0) {
FILE *f = fopen(fn, "wb");
if (f) {
fwrite(code->code, code->size, 1, f);
fclose(f);
} else
goto cant_open_code_dump_file;
}
} else {
cant_open_code_dump_file:;
if ( code->code )
print_data(code->code, code->size);
}
fprintf(dump_file, "\n");
}
}
static void dump_node(struct spl_node *node)
{
if ( !node ) return;
if ( dump_pass ) {
if (node->dump_tag != 1) return;
node->dump_tag = dump_counter++;
} else {
if (node->dump_tag == 1) return;
if (node->hnode_name)
spl_hnode_dump(dump_vm_ptr, node);
node->dump_tag = 1;
dump_roof++;
}
dump_node(node->ctx);
dump_node(node->cls);
dump_code(node->code);
struct spl_node_sub *s = node->subs_begin;
while (s) {
dump_node(s->node);
s = s->next;
}
if ( dump_pass ) {
fprintf(dump_file, "NODE=%d FLAGS=%d CLS=%d CTX=%d CTX_TYPE=%d CODE=%d CODE_IP=%d NIDX=%d SUBS=",
node->dump_tag, node->flags,
node->cls ? node->cls->dump_tag : 0,
node->ctx ? node->ctx->dump_tag : 0, node->ctx_type,
node->code ? node->code->dump_tag : 0, node->code_ip,
node->subs_next_idx);
for (s=node->subs_begin; s; s = s->next) {
fprintf(dump_file, "%s%d", s == node->subs_begin ? "" : ",",
s->node ? s->node->dump_tag : 0);
if ( s->module ) {
fprintf(dump_file, ":");
print_string(s->module);
}
if ( s->key )
print_string(s->key);
}
fprintf(dump_file, " VALUE=");
if ( node->value )
print_string(spl_get_string(node));
fprintf(dump_file, " HOSTED=");
if ( node->hnode_name )
print_string(node->hnode_name);
fprintf(dump_file, " HDATA=");
if ( node->hnode_dump )
print_string(node->hnode_dump);
fprintf(dump_file, "\n");
}
}
static void dump_task(struct spl_task *task)
{
if ( dump_pass ) {
if (task->dump_tag != 1) return;
task->dump_tag = dump_counter++;
} else {
if (task->dump_tag == 1) return;
task->dump_tag = 1;
dump_roof++;
}
dump_node(task->ctx);
dump_code(task->code);
struct spl_node_stack *s = task->stack;
while (s) {
dump_node(s->node);
s = s->next;
}
if ( dump_pass ) {
fprintf(dump_file, "TASK=%d CTX=%d CODE=%d CODE_IP=%d FLAGS=%d ID=", task->dump_tag,
task->ctx ? task->ctx->dump_tag : 0,
task->code ? task->code->dump_tag : 0,
task->code_ip, task->flags);
if ( task->id )
print_string(task->id);
fprintf(dump_file, " STACK=");
for (s=task->stack; s; s = s->next)
fprintf(dump_file, "%s%d", s == task->stack ? "" : ",",
s->node ? s->node->dump_tag : 0);
fprintf(dump_file, "\n");
}
}
static void dump_vm(struct spl_vm *vm)
{
if ( dump_pass ) {
fprintf(dump_file, "IDS=0 VALUE=%d VALUE=", dump_roof);
print_string(SPL_SIGNATURE);
fprintf(dump_file, "\n");
}
dump_node(vm->root);
struct spl_task *t = vm->task_list;
while (t) {
dump_task(t);
t = t->next;
}
if ( dump_pass ) {
fprintf(dump_file, "VM=1 ROOT=%d\n", vm->root->dump_tag);
struct spl_module *m = vm->module_list;
while (m) {
fprintf(dump_file, "MOD=1 NAME=");
print_string(m->name);
fprintf(dump_file, "\n");
m = m->next;
}
fprintf(dump_file, "END=1\n");
}
}
int spl_dump_ascii(struct spl_vm *vm, FILE *file)
{
if (vm->undumpable)
fprintf(file, "VM State is undumpable: %s\n",
vm->undumpable_info ? vm->undumpable_info :
"NO UNUMPABLE INFO SET");
#ifdef ENABLE_PTHREAD_SUPPORT
pthread_mutex_lock(&dump_lck);
#endif
dump_file = file;
dump_vm_ptr = vm;
dump_roof = dump_counter = 2;
dump_pass = 0; dump_vm(vm);
dump_pass = 1; dump_vm(vm);
#ifdef ENABLE_PTHREAD_SUPPORT
pthread_mutex_unlock(&dump_lck);
#endif
return vm->undumpable ? 1 : 0;
}
|