Skip to content

Commit

Permalink
[mce] Add valgrind mode to ease memory leak debugging
Browse files Browse the repository at this point in the history
Using valgrind to find memory issues from mce can be tricky.

Add --valgrind-mode command line option for running mce in valgrind friendly
manner when needed.

Do not unmap plugins so that valgrind can locate symbol information for them
at exit time.

Flush cached dbus message objects before exiting.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed May 12, 2017
1 parent c3addcb commit 87a6633
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mce-dbus.c
Expand Up @@ -4197,5 +4197,11 @@ void mce_dbus_exit(void)
dbus_connection = NULL;
}

/* When debugging, tell libdbus to release all dynamic resouces */
if( mce_in_valgrind_mode() ) {
mce_log(LL_WARN, "dbus shutdown");
dbus_shutdown();
}

return;
}
18 changes: 18 additions & 0 deletions mce-modules.c
Expand Up @@ -214,6 +214,24 @@ void mce_modules_exit(void)

if (modules != NULL) {
for (i = 0; (module = g_slist_nth_data(modules, i)) != NULL; i++) {
if( mce_in_valgrind_mode() ) {
/* Do not actually unmap the plugins so that
* valgrind can still locate the symbols at
* exit time. */
gpointer addr = 0;
g_module_symbol(module, "g_module_unload", &addr);
if( addr ) {
mce_log(LL_WARN, "simulating module %s unload",
g_module_name(module));
void (*unload)(GModule *) = addr;
unload(module);
}
else {
mce_log(LL_WARN, "skipping module %s unload",
g_module_name(module));
}
continue;
}
g_module_close(module);
}

Expand Down
19 changes: 19 additions & 0 deletions mce.c
Expand Up @@ -657,6 +657,7 @@ static struct
bool systembus;
bool show_module_info;
bool systemd_notify;
bool valgrind_mode;
int auto_exit;
} mce_args =
{
Expand All @@ -666,9 +667,15 @@ static struct
.systembus = true,
.show_module_info = false,
.systemd_notify = false,
.valgrind_mode = false,
.auto_exit = -1,
};

bool mce_in_valgrind_mode(void)
{
return mce_args.valgrind_mode;
}

static bool mce_do_help(const char *arg);
static bool mce_do_version(const char *arg);

Expand Down Expand Up @@ -698,6 +705,12 @@ static bool mce_do_auto_exit(const char *arg)
mce_args.auto_exit = arg ? strtol(arg, 0, 0) : 5;
return true;
}
static bool mce_do_valgrind_mode(const char *arg)
{
(void)arg;
mce_args.valgrind_mode = true;
return true;
}
static bool mce_do_log_function(const char *arg)
{
mce_log_add_pattern(arg);
Expand Down Expand Up @@ -859,6 +872,12 @@ static const mce_opt_t options[] =
"\n"
"This is usefult for mce startup debugging only.\n"
},
{
.name = "valgrind-mode",
.without_arg = mce_do_valgrind_mode,
.usage =
"Enable run-under valgrind mode\n"
},
// sentinel
{
.name = 0
Expand Down
1 change: 1 addition & 0 deletions mce.h
Expand Up @@ -418,6 +418,7 @@ submode_t mce_get_submode_int32(void);
gboolean mce_add_submode_int32(const submode_t submode);
gboolean mce_rem_submode_int32(const submode_t submode);

bool mce_in_valgrind_mode(void);
void mce_abort(void) __attribute__((noreturn));
void mce_quit_mainloop(void);
void mce_signal_handlers_remove(void);
Expand Down

0 comments on commit 87a6633

Please sign in to comment.