diff --git a/emu.c b/emu.c index dad632a..49266da 100644 --- a/emu.c +++ b/emu.c @@ -85,15 +85,41 @@ find_thread(struct ovni_eproc *proc, pid_t tid) } static void -step_emulator(struct ovni_emu *emu) +hook_pre(struct ovni_emu *emu) { //emu_emit(emu); switch(emu->cur_ev->model) { - case 'O': emu_process_ovni_ev(emu); break; - //case 'V': emu_process_nosv_ev(emu); break; - //case 'M': emu_process_tampi_ev(emu); break; + case 'O': hook_pre_ovni(emu); break; + default: + //dbg("unknown model %c\n", emu->cur_ev->model); + break; + } +} + +static void +hook_view(struct ovni_emu *emu) +{ + //emu_emit(emu); + + switch(emu->cur_ev->model) + { + case 'O': hook_view_ovni(emu); break; + default: + //dbg("unknown model %c\n", emu->cur_ev->model); + break; + } +} + +static void +hook_post(struct ovni_emu *emu) +{ + //emu_emit(emu); + + switch(emu->cur_ev->model) + { + case 'O': hook_post_ovni(emu); break; default: //dbg("unknown model %c\n", emu->cur_ev->model); break; @@ -183,73 +209,15 @@ emulate(struct ovni_emu *emu) while(next_event(emu) == 0) { //fprintf(stdout, "step %i\n", i); - step_emulator(emu); + hook_pre(emu); + hook_view(emu); + hook_post(emu); /* Read the next event */ ovni_load_next_event(emu->cur_stream); } } -int -emu_cpu_find_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread) -{ - int i; - - for(i=0; inthreads; i++) - if(cpu->thread[i] == thread) - break; - - /* Not found */ - if(i >= cpu->nthreads) - return -1; - - return i; -} - -void -emu_cpu_remove_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread) -{ - int i, j; - - i = emu_cpu_find_thread(cpu, thread); - - /* Not found, abort */ - if(i < 0) - abort(); - - for(j=i; j+1 < cpu->nthreads; j++) - { - cpu->thread[i] = cpu->thread[j+1]; - } - - cpu->nthreads--; -} - -void -emu_cpu_add_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread) -{ - /* Found, abort */ - if(emu_cpu_find_thread(cpu, thread) >= 0) - abort(); - - assert(cpu->nthreads < OVNI_MAX_THR); - - cpu->thread[cpu->nthreads++] = thread; -} - -struct ovni_cpu * -emu_get_cpu(struct ovni_emu *emu, int cpuid) -{ - assert(cpuid < OVNI_MAX_CPU); - - if(cpuid < 0) - { - return &emu->vcpu; - } - - return &emu->cpu[emu->cpuind[cpuid]]; -} - struct ovni_ethread * emu_get_thread(struct ovni_emu *emu, int tid) { diff --git a/emu.h b/emu.h index 73245cd..9bd562f 100644 --- a/emu.h +++ b/emu.h @@ -104,6 +104,8 @@ struct ovni_cpu { enum ovni_cpu_state state; enum ovni_cpu_type type; + size_t last_nthreads; + /* The threads the cpu is currently running */ size_t nthreads; struct ovni_ethread *thread[OVNI_MAX_THR]; @@ -135,14 +137,10 @@ struct ovni_emu { /* Emulator function declaration */ void emu_emit(struct ovni_emu *emu); -void emu_process_ovni_ev(struct ovni_emu *emu); - -int emu_cpu_find_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread); - -void emu_cpu_remove_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread); - -void emu_cpu_add_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread); +void hook_pre_ovni(struct ovni_emu *emu); +void hook_view_ovni(struct ovni_emu *emu); +void hook_post_ovni(struct ovni_emu *emu); struct ovni_cpu *emu_get_cpu(struct ovni_emu *emu, int cpuid); diff --git a/emu_ovni.c b/emu_ovni.c index d819dd8..2e38092 100644 --- a/emu_ovni.c +++ b/emu_ovni.c @@ -3,6 +3,67 @@ #include +struct ovni_cpu * +emu_get_cpu(struct ovni_emu *emu, int cpuid) +{ + assert(cpuid < OVNI_MAX_CPU); + + if(cpuid < 0) + { + return &emu->vcpu; + } + + return &emu->cpu[emu->cpuind[cpuid]]; +} + +int +emu_cpu_find_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread) +{ + int i; + + for(i=0; inthreads; i++) + if(cpu->thread[i] == thread) + break; + + /* Not found */ + if(i >= cpu->nthreads) + return -1; + + return i; +} + +void +emu_cpu_add_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread) +{ + /* Found, abort */ + if(emu_cpu_find_thread(cpu, thread) >= 0) + abort(); + + assert(cpu->nthreads < OVNI_MAX_THR); + + cpu->thread[cpu->nthreads++] = thread; +} + +void +emu_cpu_remove_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread) +{ + int i, j; + + i = emu_cpu_find_thread(cpu, thread); + + /* Not found, abort */ + if(i < 0) + abort(); + + for(j=i; j+1 < cpu->nthreads; j++) + { + cpu->thread[i] = cpu->thread[j+1]; + } + + cpu->nthreads--; +} + + static void print_threads_state(struct ovni_emu *emu) { @@ -245,7 +306,7 @@ ev_cpu(struct ovni_emu *emu) } void -emu_process_ovni_ev(struct ovni_emu *emu) +hook_pre_ovni(struct ovni_emu *emu) { //emu_emit(emu); @@ -263,3 +324,52 @@ emu_process_ovni_ev(struct ovni_emu *emu) print_threads_state(emu); } + +static void +view_thread_count(struct ovni_emu *emu) +{ + int i; + + /* Check every CPU looking for a change in nthreads */ + for(i=0; incpus; i++) + { + if(emu->cpu[i].last_nthreads == emu->cpu[i].nthreads) + continue; + + /* Emit the number of threads in the cpu */ + dbg("cpu %d runs %d threads\n", i, emu->cpu[i].nthreads); + } + + /* Same with the virtual CPU */ + if(emu->vcpu.last_nthreads != emu->vcpu.nthreads) + dbg("vpu runs %d threads\n", emu->vcpu.nthreads); +} + +void +hook_view_ovni(struct ovni_emu *emu) +{ + switch(emu->cur_ev->class) + { + case 'H': + case 'A': + case 'C': + view_thread_count(emu); + break; + default: + break; + } +} + +void +hook_post_ovni(struct ovni_emu *emu) +{ + int i; + + /* Update last_nthreads in the CPUs */ + + for(i=0; incpus; i++) + emu->cpu[i].last_nthreads = emu->cpu[i].nthreads; + + emu->vcpu.last_nthreads = emu->vcpu.nthreads; +} +