Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drcov plugin won't work if program doesn't exit normally #57

Closed
bruce30262 opened this issue Jan 26, 2024 · 2 comments
Closed

drcov plugin won't work if program doesn't exit normally #57

bruce30262 opened this issue Jan 26, 2024 · 2 comments

Comments

@bruce30262
Copy link

First of all thanks @JRomainG and @vanhauser-thc for PR #56. The drcov plugin is very useful.

However I found that when a program doesn't exit normally, the plugin won't dump the coverage info into the file. So for example a simple program like:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    int *p = NULL;
    *p = 123; // crash
    return 0;
}

Will results in an empty drcov file.

I'm not familiar with QEMU TCG plugin but I think it's because it only calls plugin_exit in the atexit callback :

qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);

So when program exit abnormally ( crash / hang / timeout / ctrl-c ...etc ) it won't dump the coverage info.

Is it possible to register plugin_exit in other signal handlers, so when the program receive those signals and terminate itself it will also dump the coverage info ? Here are some signals I can think of :

  • SIGTERM
  • SIGINT
  • SIGALRM
  • SIGKILL
  • SIGSEGV
@JRomainG
Copy link

Unfortunately, it doesn't seem to be that easy, as QEMU registers its own signal handlers. Ideally, a callback would be added to the dump_core_and_abort function, but I do not believe that this is natively supported. You can however add it with a simple patch:

diff --git a/linux-user/signal.c b/linux-user/signal.c
index 8b23f67821..21bf7fcc60 100644
--- a/linux-user/signal.c
+++ b/linux-user/signal.c
@@ -632,6 +632,9 @@ static void QEMU_NORETURN dump_core_and_abort(int target_sig)
     trace_user_force_sig(env, target_sig, host_sig);
     gdb_signalled(env, target_sig);
 
+    /* notify plugins that we are about to exit */
+    qemu_plugin_atexit_cb();
+
     /* dump core if supported by target binary format */
     if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
         stop_all_tasks();

@bruce30262
Copy link
Author

I see, thanks for the reply !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants