Do proper error-handling.

2006-03-05 Soeren Sandmann <sandmann@redhat.com>

        * sysprof-text.c, collector.c, sysprof.c: Do proper
        error-handling.
This commit is contained in:
Soeren Sandmann
2006-03-05 21:11:07 +00:00
committed by Søren Sandmann Pedersen
parent ebb7728768
commit 849efc820d
5 changed files with 102 additions and 59 deletions

View File

@ -1,3 +1,8 @@
2006-03-05 Soeren Sandmann <sandmann@redhat.com>
* sysprof-text.c, collector.c, sysprof.c: Do proper
error-handling.
Fri Mar 3 22:28:03 2006 Soeren Sandmann <sandmann@redhat.com> Fri Mar 3 22:28:03 2006 Soeren Sandmann <sandmann@redhat.com>
* process.c (process_lookup_symbol): Check that the inodes match. * process.c (process_lookup_symbol): Check that the inodes match.

View File

@ -29,6 +29,9 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
static void set_no_module_error (GError **err);
static void set_cant_open_error (GError **err, int eno);
struct Collector struct Collector
{ {
CollectorFunc callback; CollectorFunc callback;
@ -179,12 +182,13 @@ open_fd (Collector *collector,
if (load_module()) if (load_module())
{ {
GTimer *timer = g_timer_new (); GTimer *timer = g_timer_new ();
while (fd < 0 && g_timer_elapsed (timer, NULL) < 0.5) while (fd < 0 && g_timer_elapsed (timer, NULL) < 0.5)
{ {
/* Wait for udev to discover the new device */ /* Wait for udev to discover the new device */
usleep (100000); usleep (100000);
errno = 0;
fd = open (SYSPROF_FILE, O_RDONLY); fd = open (SYSPROF_FILE, O_RDONLY);
} }
@ -192,12 +196,15 @@ open_fd (Collector *collector,
if (fd < 0) if (fd < 0)
{ {
/* FIXME: set "module is loaded but no file error" */ set_cant_open_error (err, errno);
return FALSE;
} }
} }
if (fd < 0) if (fd < 0)
{ {
set_no_module_error (err);
/* FIXME: set error */ /* FIXME: set error */
return FALSE; return FALSE;
} }
@ -323,3 +330,39 @@ collector_create_profile (Collector *collector)
return profile_new (info.resolved_stash); return profile_new (info.resolved_stash);
} }
static void
set_no_module_error (GError **err)
{
g_set_error (err,
COLLECTOR_ERROR,
COLLECTOR_ERROR_CANT_OPEN_FILE,
"Can't open " SYSPROF_FILE ". You need to insert "
"the sysprof kernel module. Run\n"
"\n"
" modprobe sysprof-module\n"
"\n"
"as root");
}
static void
set_cant_open_error (GError **err,
int eno)
{
g_set_error (err,
COLLECTOR_ERROR,
COLLECTOR_ERROR_CANT_OPEN_FILE,
"Can't open " SYSPROF_FILE ": %s",
g_strerror (eno));
}
GQuark
collector_error_quark (void)
{
static GQuark q = 0;
if (q == 0)
q = g_quark_from_static_string ("collector-error-quark");
return q;
}

View File

@ -23,11 +23,20 @@ typedef struct Collector Collector;
typedef void (* CollectorFunc) (gpointer data); typedef void (* CollectorFunc) (gpointer data);
#define COLLECTOR_ERROR collector_error_quark ()
GQuark collector_error_quark (void);
typedef enum
{
COLLECTOR_ERROR_CANT_OPEN_FILE
} CollectorError;
/* callback is called whenever a new sample arrives */ /* callback is called whenever a new sample arrives */
Collector *collector_new (CollectorFunc callback, Collector *collector_new (CollectorFunc callback,
gpointer data); gpointer data);
gboolean collector_start (Collector *collector, gboolean collector_start (Collector *collector,
GError **err); GError **err);
void collector_stop (Collector *collector); void collector_stop (Collector *collector);
void collector_reset (Collector *collector); void collector_reset (Collector *collector);
int collector_get_n_samples (Collector *collector); int collector_get_n_samples (Collector *collector);

View File

@ -47,91 +47,81 @@ dump_data (Application *app)
{ {
GError *err = NULL; GError *err = NULL;
Profile *profile = collector_create_profile (app->collector); Profile *profile = collector_create_profile (app->collector);
profile_save (profile, app->outfile, &err); profile_save (profile, app->outfile, &err);
if (err) if (err)
{ {
fprintf (stderr, "%s: %s\n", app->outfile, err->message); fprintf (stderr, "Error saving %s: %s\n", app->outfile, err->message);
exit (1); exit (1);
} }
else
{
printf ("Saved profile in %s\n\n", app->outfile);
}
} }
void void
signal_handler (int signo, gpointer data) signal_handler (int signo,
gpointer data)
{ {
Application *app = data; Application *app = data;
dump_data (app); dump_data (app);
while (g_main_iteration (FALSE)) while (g_main_iteration (FALSE))
; ;
g_main_loop_quit (app->main_loop); g_main_loop_quit (app->main_loop);
} }
static void static char *
no_module (void) usage_msg (const char *name)
{ {
perror (SYSPROF_FILE); return g_strdup_printf (
fprintf (stderr, "Usage: \n"
"\n" " %s <outfile>\n"
"Can't open " SYSPROF_FILE ". You need to insert " "\n"
"the sysprof kernel module. Run\n" "On SIGTERM or SIGINT (Ctrl-C) the profile will be written to <outfile>",
"\n" name);
" modprobe sysprof-module\n"
"\n"
"as root.\n");
} }
static void static void
usage (const char *name) die (const char *err_msg)
{ {
fprintf (stderr, if (err_msg)
"\n" fprintf (stderr, "\n%s\n\n", err_msg);
"Usage: \n"
" %s <outfile>\n" exit (-1);
"\n"
"On SIGTERM or SIGINT (Ctrl-C) the profile will be written to <outfile>\n"
"\n",
name);
} }
int int
main (int argc, main (int argc,
char *argv[]) char *argv[])
{ {
gboolean quit;
Application *app = g_new0 (Application, 1); Application *app = g_new0 (Application, 1);
GError *err;
app->collector = collector_new (NULL, NULL); app->collector = collector_new (NULL, NULL);
app->outfile = g_strdup (argv[1]); app->outfile = g_strdup (argv[1]);
app->main_loop = g_main_loop_new (NULL, 0); app->main_loop = g_main_loop_new (NULL, 0);
/* FIXME: get the real error */
quit = FALSE;
if (!collector_start (app->collector, NULL)) err = NULL;
{
no_module(); if (!collector_start (app->collector, &err))
quit = TRUE; die (err->message);
}
if (argc < 2) if (argc < 2)
{ die (usage_msg (argv[0]));
usage (argv[0]);
quit = TRUE; if (!signal_set_handler (SIGTERM, signal_handler, app, &err))
} die (err->message);
if (quit) if (!signal_set_handler (SIGINT, signal_handler, app, &err))
return -1; die (err->message);
/* FIXME: check the errors */
signal_set_handler (SIGTERM, signal_handler, app, NULL);
signal_set_handler (SIGINT, signal_handler, app, NULL);
g_main_loop_run (app->main_loop); g_main_loop_run (app->main_loop);
signal_unset_handler (SIGTERM); signal_unset_handler (SIGTERM);
signal_unset_handler (SIGINT); signal_unset_handler (SIGINT);

View File

@ -352,6 +352,7 @@ static void
on_start_toggled (GtkWidget *widget, gpointer data) on_start_toggled (GtkWidget *widget, gpointer data)
{ {
Application *app = data; Application *app = data;
GError *err = NULL;
if (!gtk_toggle_tool_button_get_active ( if (!gtk_toggle_tool_button_get_active (
GTK_TOGGLE_TOOL_BUTTON (app->start_button))) GTK_TOGGLE_TOOL_BUTTON (app->start_button)))
@ -359,7 +360,7 @@ on_start_toggled (GtkWidget *widget, gpointer data)
return; return;
} }
if (collector_start (app->collector, NULL)) if (collector_start (app->collector, &err))
{ {
delete_data (app); delete_data (app);
@ -367,14 +368,9 @@ on_start_toggled (GtkWidget *widget, gpointer data)
} }
else else
{ {
/* FIXME: get the real error message */ sorry (app->main_window, err->message);
sorry (app->main_window,
"Can't open " SYSPROF_FILE ". You need to insert\n" g_error_free (err);
"the sysprof kernel module. Run\n"
"\n"
" modprobe sysprof-module\n"
"\n"
"as root.");
} }
update_screenshot_window (app); update_screenshot_window (app);