On 6/16/2026 9:13 AM, Harry van Haaren | OPENCHIP wrote:
> From: Harry van Haaren <[email protected]>
> 
> Executed instructions are cached in string format inside the
> execlog plugin. These strings are flushed on exit of a TB, improving
> performance. This causes executed instructions to be lost when an
> 'ecall' (riscv system call) occurs that causes the thread to terminate.
> 
> The fix in this patch registers an 'on_exit()' callback, and flushes
> any content in the c->last_exec buffer, to ensure all instructions are
> present in the final instruction log.
> 
> A mutex lock/unlock is added around the plugin exit, to ensure the
> lines are not corrupted.
> 
> Signed-off-by: Harry van Haaren <[email protected]>
> 
> ---
> 
> Please note that LLMs were used to investigate and fix this bug,
> but all code in this patch has been reviewed by me, and I believe
> it to be a good solution.
> ---
>  contrib/plugins/execlog.c | 26 +++++++++++++++++++++++++-
>  1 file changed, 25 insertions(+), 1 deletion(-)
> 
> diff --git a/contrib/plugins/execlog.c b/contrib/plugins/execlog.c
> index e51af9f2df..f362b38268 100644
> --- a/contrib/plugins/execlog.c
> +++ b/contrib/plugins/execlog.c
> @@ -388,7 +388,28 @@ static void vcpu_init(qemu_plugin_id_t id, unsigned int 
> vcpu_index)
>  }
> 
>  /**
> - * On plugin exit, print last instruction in cache
> + * On vCPU exit, flush the last cached instruction for this vCPU.
> + *
> + * The one-instruction-delay pattern stores each instruction in last_exec and
> + * only prints it when the *next* callback fires. When a thread exits via
> + * syscall (e.g. ecall/exit), no subsequent callback fires for that vCPU and
> + * the final instruction is silently dropped. Flushing here guarantees it is
> + * written before the vCPU is torn down.
> + */
> +static void vcpu_exit(qemu_plugin_id_t id, unsigned int vcpu_index)
> +{
> +    CPU *c = qemu_plugin_scoreboard_find(cpus, vcpu_index);
> +    if (c->last_exec && c->last_exec->len) {
> +        g_mutex_lock(&execlog_output_mutex);
> +        qemu_plugin_outs(c->last_exec->str);
> +        qemu_plugin_outs("\n");
> +        g_mutex_unlock(&execlog_output_mutex);
> +        g_string_truncate(c->last_exec, 0);
> +    }
> +}
> +
> +/**
> + * On plugin exit, flush any remaining cached instructions and free state.
>   */
>  static void plugin_exit(qemu_plugin_id_t id, void *p)
>  {
> @@ -396,8 +417,10 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
>      for (int i = 0; i < n; i++) {
>          CPU *c = qemu_plugin_scoreboard_find(cpus, i);
>          if (c->last_exec && c->last_exec->len) {
> +            g_mutex_lock(&execlog_output_mutex);
>              qemu_plugin_outs(c->last_exec->str);
>              qemu_plugin_outs("\n");
> +            g_mutex_unlock(&execlog_output_mutex);
>          }
>      }
>      qemu_plugin_scoreboard_free(cpus);
> @@ -467,6 +490,7 @@ QEMU_PLUGIN_EXPORT int 
> qemu_plugin_install(qemu_plugin_id_t id,
> 
>      /* Register init, translation block and exit callbacks */
>      qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
> +    qemu_plugin_register_vcpu_exit_cb(id, vcpu_exit);
>      qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
>      qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
>

Why not simply make this flush in atexit callback?

> --
> 2.54.0
> 
> 
> 
> The content, data, and any attached documents to this email are addressed 
> exclusively to the addressee and are confidential and/or may be subject to a 
> non-disclosure agreement. Any use, forwarding, disclosure, and/or copying, in 
> whole or in part, without authorization is prohibited. If you have received 
> this email in error, we apologize and, please notify the sender or Openchip 
> immediately, and delete it from your system.
> 
> El contenido, los datos y cualquier documento adjunto a este correo 
> electrónico están dirigidos exclusivamente al destinatario y son 
> confidenciales y/o pueden estar sujetas a un acuerdo de no revelación. Está 
> prohibido cualquier uso, reenvío, divulgación o copia, total o parcial, sin 
> autorización. Si has recibido este correo por error, te pedimos disculpas y 
> agradecemos que lo notifiques de inmediato al remitente o a Openchip, y lo 
> elimines de tu sistema.
> 
> El contingut, les dades i qualsevol document adjunt a aquest correu 
> electrònic estan dirigits exclusivament al destinatari i són confidencials 
> i/o poden estar subjectes a un acord de no revelació. Està prohibit qualsevol 
> ús, reenviament, divulgació o còpia, total o parcial, sense autorització. Si 
> has rebut aquest correu per error, et demanem disculpes i agraïm que ho 
> notifiquis d'immediat al remitent o a Openchip, i l'eliminis del teu sistema


Reply via email to