diff --git a/src/emu/metadata.c b/src/emu/metadata.c index b28253b..37706f8 100644 --- a/src/emu/metadata.c +++ b/src/emu/metadata.c @@ -9,6 +9,7 @@ #include "ovni.h" #include "parson.h" #include "proc.h" +#include "thread.h" static JSON_Object * load_json(const char *path) @@ -45,19 +46,6 @@ check_version(JSON_Object *meta) return -1; } - JSON_Value *mversion_val = json_object_get_value(meta, "model_version"); - if (mversion_val == NULL) { - err("missing attribute \"model_version\""); - return -1; - } - - const char *mversion = json_string(mversion_val); - if (strcmp(mversion, OVNI_MODEL_VERSION) != 0) { - err("model version mismatch '%s' (expected '%s')", - mversion, OVNI_MODEL_VERSION); - return -1; - } - return 0; } @@ -121,11 +109,11 @@ load_cpus(struct loom *loom, JSON_Object *meta) } int -metadata_load(const char *path, struct loom *loom, struct proc *proc) +metadata_load_proc(const char *path, struct loom *loom, struct proc *proc) { JSON_Object *meta = load_json(path); if (meta == NULL) { - err("cannot load metadata from file %s", path); + err("cannot load proc metadata from file %s", path); return -1; } @@ -147,3 +135,25 @@ metadata_load(const char *path, struct loom *loom, struct proc *proc) return 0; } + +int +metadata_load_thread(const char *path, struct thread *thread) +{ + JSON_Object *meta = load_json(path); + if (meta == NULL) { + err("cannot load thread metadata from file %s", path); + return -1; + } + + if (check_version(meta) != 0) { + err("version check failed"); + return -1; + } + + if (thread_load_metadata(thread, meta) != 0) { + err("cannot load thread attributes"); + return -1; + } + + return 0; +} diff --git a/src/emu/metadata.h b/src/emu/metadata.h index a604660..3d6e9a9 100644 --- a/src/emu/metadata.h +++ b/src/emu/metadata.h @@ -7,7 +7,9 @@ #include "common.h" struct loom; struct proc; +struct thread; -USE_RET int metadata_load(const char *path, struct loom *loom, struct proc *proc); +USE_RET int metadata_load_proc(const char *path, struct loom *loom, struct proc *proc); +USE_RET int metadata_load_thread(const char *path, struct thread *thread); #endif /* METADATA_H */ diff --git a/src/emu/system.c b/src/emu/system.c index bd72ead..7ba5691 100644 --- a/src/emu/system.c +++ b/src/emu/system.c @@ -24,7 +24,7 @@ struct bay; static struct thread * -create_thread(struct proc *proc, const char *relpath) +create_thread(struct proc *proc, const char *tracedir, const char *relpath) { int tid; if (thread_relpath_get_tid(relpath, &tid) != 0) { @@ -50,6 +50,19 @@ create_thread(struct proc *proc, const char *relpath) return NULL; } + /* Build metadata path */ + char mpath[PATH_MAX]; + if (snprintf(mpath, PATH_MAX, "%s/%s/thread.%d.json", + tracedir, proc->id, tid) >= PATH_MAX) { + err("path too long"); + return NULL; + } + + if (metadata_load_thread(mpath, thread) != 0) { + err("cannot load metadata from %s", mpath); + return NULL; + } + if (proc_add_thread(proc, thread) != 0) { err("failed to add thread to proc"); return NULL; @@ -94,7 +107,7 @@ create_proc(struct loom *loom, const char *tracedir, const char *relpath) } /* Load metadata too */ - if (metadata_load(mpath, loom, proc) != 0) { + if (metadata_load_proc(mpath, loom, proc) != 0) { err("cannot load metadata from %s", mpath); return NULL; } @@ -185,7 +198,7 @@ create_system(struct system *sys, struct trace *trace) return -1; } - struct thread *thread = create_thread(proc, s->relpath); + struct thread *thread = create_thread(proc, dir, s->relpath); if (thread == NULL) { err("create_thread failed"); return -1; diff --git a/src/emu/thread.c b/src/emu/thread.c index 0536a2d..7660c38 100644 --- a/src/emu/thread.c +++ b/src/emu/thread.c @@ -152,6 +152,11 @@ thread_init_end(struct thread *th) return -1; } + if (th->meta == NULL) { + err("missing metadata for thread %s", th->id); + return -1; + } + for (int i = 0; i < TH_CHAN_MAX; i++) { chan_init(&th->chan[i], CHAN_SINGLE, chan_fmt, th->gindex, chan_name[i]); @@ -439,3 +444,16 @@ thread_migrate_cpu(struct thread *th, struct cpu *cpu) return 0; } + +int +thread_load_metadata(struct thread *thread, JSON_Object *meta) +{ + if (thread->meta != NULL) { + err("thread %s already loaded metadata", thread->id); + return -1; + } + + thread->meta = meta; + + return 0; +} diff --git a/src/emu/thread.h b/src/emu/thread.h index 281bdd6..97bc6ae 100644 --- a/src/emu/thread.h +++ b/src/emu/thread.h @@ -11,6 +11,7 @@ #include "common.h" #include "extend.h" #include "uthash.h" +#include "parson.h" struct bay; struct cpu; struct mux; @@ -70,6 +71,9 @@ struct thread { struct chan chan[TH_CHAN_MAX]; + /* Metadata */ + JSON_Object *meta; + struct extend ext; UT_hash_handle hh; /* threads in the process */ @@ -78,6 +82,7 @@ struct thread { USE_RET int thread_relpath_get_tid(const char *relpath, int *tid); USE_RET int thread_init_begin(struct thread *thread, const char *relpath); USE_RET int thread_init_end(struct thread *thread); +USE_RET int thread_load_metadata(struct thread *thread, JSON_Object *meta); USE_RET int thread_set_state(struct thread *th, enum thread_state state); USE_RET int thread_set_cpu(struct thread *th, struct cpu *cpu); USE_RET int thread_unset_cpu(struct thread *th);