main.c.bak

来自「memprof source code, linux」· BAK 代码 · 共 1,816 行 · 第 1/4 页

BAK
1,816 行
字号
}voidabout_cb (GtkWidget *widget){       GladeXML *xml;       GtkWidget *dialog;       ProcessWindow *pwin = pwin_from_widget (widget);	       xml = glade_xml_new (glade_file, "About", NULL);       dialog = glade_xml_get_widget (xml, "About");       g_object_unref (G_OBJECT (xml));       gtk_window_set_transient_for (GTK_WINDOW (dialog),                                     GTK_WINDOW (pwin->main_window));       g_object_set (G_OBJECT (dialog), "version", VERSION, NULL);       gtk_widget_show (dialog);}static voidshow_error_response (GtkDialog *dialog,		     gint       response_id,		     gpointer   user_data){	if (response_id == GTK_RESPONSE_OK)		gtk_widget_destroy (GTK_WIDGET (dialog));}voidshow_error (GtkWidget *parent_window,	    ErrorType error,	    const gchar *format,	    ...){	va_list args;	char *message;	GtkWidget *dialog;	va_start (args, format);	vasprintf (&message, format, args);	va_end (args);	dialog = gtk_message_dialog_new (parent_window ? GTK_WINDOW (parent_window) : NULL,					 GTK_DIALOG_DESTROY_WITH_PARENT,					 (error == ERROR_FATAL) ?					   GTK_MESSAGE_ERROR :					   GTK_MESSAGE_WARNING,					 GTK_BUTTONS_OK, message);	free (message);	gtk_window_set_title (GTK_WINDOW (dialog),			      (error == ERROR_FATAL) ?			      _("MemProf Error") : _("MemProf Warning"));	if (error == ERROR_WARNING) {		gtk_widget_show (dialog);		g_signal_connect (dialog, "response",				  G_CALLBACK (show_error_response), NULL);	} else {		gtk_dialog_run (GTK_DIALOG (dialog));		gtk_widget_destroy (dialog);	}	if (error == ERROR_FATAL)		exit(1);}static voidset_white_bg (GtkWidget *widget){	GtkRcStyle *rc_style = gtk_rc_style_new();	rc_style->color_flags[GTK_STATE_NORMAL] = GTK_RC_BG;	rc_style->bg[GTK_STATE_NORMAL].red = 0xffff;	rc_style->bg[GTK_STATE_NORMAL].green = 0xffff;	rc_style->bg[GTK_STATE_NORMAL].blue = 0xffff;	gtk_widget_modify_style (widget, rc_style);}static voidprocess_window_free (ProcessWindow *pwin){	/* FIXME: we leak the process structure */		if (pwin->leaks)		g_slist_free (pwin->leaks);	if (pwin->profile)		profile_free (pwin->profile);	process_windows = g_slist_remove (process_windows, pwin);	if (!process_windows)		gtk_main_quit ();		g_free (pwin);}static voidprocess_window_destroy (ProcessWindow *pwin){	if (pwin->status_update_timeout)		g_source_remove (pwin->status_update_timeout);	gtk_widget_destroy (pwin->main_window);	check_quit ();}static ProcessWindow *process_window_new (void){       gchar *fullfilename;       GladeXML *xml;       GtkWidget *vpaned;       ProcessWindow *pwin;       GtkWidget *menuitem;       pwin = g_new0 (ProcessWindow, 1);       process_windows = g_slist_prepend (process_windows, pwin);       pwin->process = NULL;       pwin->profile = NULL;       pwin->leaks = NULL;       pwin->usage_max = 32*1024;       pwin->usage_high = 0;       pwin->usage_leaked = 0;       xml = glade_xml_new (glade_file, "MainWindow", NULL);       pwin->main_window = glade_xml_get_widget (xml, "MainWindow");       fullfilename = gnome_program_locate_file (NULL, GNOME_FILE_DOMAIN_PIXMAP, "memprof.png", FALSE, NULL);       gnome_window_icon_set_from_file (GTK_WINDOW (pwin->main_window),					fullfilename);       g_free (fullfilename);       gtk_signal_connect (GTK_OBJECT (pwin->main_window), "delete_event",			   GTK_SIGNAL_FUNC (hide_and_check_quit), pwin);			                 gtk_window_set_default_size (GTK_WINDOW (pwin->main_window), 400, 600);       gtk_object_set_data_full (GTK_OBJECT (pwin->main_window),				 "process-window",				 pwin, (GDestroyNotify)process_window_free);       pwin->main_notebook = glade_xml_get_widget (xml, "main-notebook");       pwin->follow_fork = default_follow_fork;       pwin->follow_exec = default_follow_exec;       menuitem = glade_xml_get_widget (xml, "follow-fork");       gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), pwin->follow_fork);       menuitem = glade_xml_get_widget (xml, "follow-exec");       gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), pwin->follow_exec);       pwin->n_allocations_label = glade_xml_get_widget (xml, "n-allocations-label");       pwin->bytes_per_label = glade_xml_get_widget (xml, "bytes-per-label");       pwin->total_bytes_label = glade_xml_get_widget (xml, "total-bytes-label");       pwin->profile_func_clist = glade_xml_get_widget (xml, "profile-func-clist");       pwin->profile_child_clist =  glade_xml_get_widget (xml, "profile-child-clist");       pwin->profile_caller_clist = glade_xml_get_widget (xml, "profile-caller-clist");       gtk_clist_set_column_width (GTK_CLIST (pwin->profile_func_clist), 0, 250);       gtk_clist_set_column_width (GTK_CLIST (pwin->profile_child_clist), 0, 250);       gtk_clist_set_column_width (GTK_CLIST (pwin->profile_caller_clist), 0, 250);       gtk_signal_connect (GTK_OBJECT (pwin->profile_func_clist), "select_row",			   GTK_SIGNAL_FUNC (profile_func_select_row), pwin);       gtk_signal_connect (GTK_OBJECT (pwin->profile_func_clist), "click_column",			   GTK_SIGNAL_FUNC (profile_func_click_column), NULL);       gtk_signal_connect (GTK_OBJECT (pwin->profile_caller_clist), "button_press_event",			   GTK_SIGNAL_FUNC (profile_ref_button_press), pwin);       gtk_signal_connect (GTK_OBJECT (pwin->profile_child_clist), "button_press_event",			   GTK_SIGNAL_FUNC (profile_ref_button_press), pwin);              pwin->leak_block_clist = glade_xml_get_widget (xml, "leak-block-clist");       pwin->leak_stack_clist =  glade_xml_get_widget (xml, "leak-stack-clist");       gtk_clist_set_column_width (GTK_CLIST (pwin->leak_stack_clist), 0, 250);       gtk_clist_set_column_width (GTK_CLIST (pwin->leak_stack_clist), 2, 500);       gtk_signal_connect (GTK_OBJECT (pwin->leak_block_clist), "select_row",			   GTK_SIGNAL_FUNC (leak_block_select_row), pwin);       gtk_signal_connect (GTK_OBJECT (pwin->leak_stack_clist), "button_press_event",			   GTK_SIGNAL_FUNC (leak_stack_button_press), pwin);       pwin->usage_max_label = glade_xml_get_widget (xml, "usage-max-label");       pwin->usage_canvas = glade_xml_get_widget (xml, "usage-canvas");       set_white_bg (pwin->usage_canvas);       gtk_signal_connect (GTK_OBJECT (pwin->usage_canvas), "size_allocate",			   GTK_SIGNAL_FUNC (usage_canvas_size_allocate), pwin);       pwin->usage_high_bar = gnome_canvas_item_new (gnome_canvas_root (GNOME_CANVAS (pwin->usage_canvas)),						     gnome_canvas_rect_get_type (),						     "x1", 0.0,						     "y1", 0.0,						     "outline_color", "black", 						     "fill_color", "blue",						     NULL);       pwin->usage_current_bar = gnome_canvas_item_new (gnome_canvas_root (GNOME_CANVAS (pwin->usage_canvas)),							gnome_canvas_rect_get_type (),							"x1", 0.0,							"y1", 0.0,							"outline_color", "black", 							"fill_color", "yellow",							NULL);       pwin->usage_leak_bar = gnome_canvas_item_new (gnome_canvas_root (GNOME_CANVAS (pwin->usage_canvas)),						     gnome_canvas_rect_get_type (),						     "x1", 0.0,						     "y1", 0.0,						     "outline_color", "black", 						     "fill_color", "red",						     NULL);       pwin->usage_frame = gnome_canvas_item_new (gnome_canvas_root (GNOME_CANVAS (pwin->usage_canvas)),						  gnome_canvas_rect_get_type (),						  "x1", 0.0,						  "y1", 0.0,						  "outline_color", "black", 						  "fill_color", NULL,						  NULL);                     vpaned = glade_xml_get_widget (xml, "profile-vpaned");       gtk_paned_set_position (GTK_PANED (vpaned), 150);              vpaned = glade_xml_get_widget (xml, "leaks-vpaned");       gtk_paned_set_position (GTK_PANED (vpaned), 150);       glade_xml_signal_autoconnect (xml);       g_object_unref (G_OBJECT (xml));       return pwin;}MPProcess *process_window_get_process (ProcessWindow *pwin){	return pwin->process;}gbooleanprocess_window_visible (ProcessWindow *pwin){	return GTK_WIDGET_VISIBLE (pwin->main_window);}voidprocess_window_show (ProcessWindow *pwin){	if (!process_window_visible (pwin))		gtk_widget_show (pwin->main_window);	else		gdk_window_show (pwin->main_window->window);}voidprocess_window_hide (ProcessWindow *pwin){	if (process_window_visible (pwin)) 		hide_and_check_quit (pwin->main_window);}voidcheck_quit (void){	GList *toplevels, *tmplist;		tmplist = toplevels = gtk_window_list_toplevels ();	while (tmplist) {		if (GTK_WIDGET_VISIBLE (toplevels->data))			return;		tmplist = tmplist->next;	}	g_list_free (toplevels);	gtk_main_quit ();}gbooleanhide_and_check_quit (GtkWidget *window){	gtk_widget_hide (window);	check_quit ();	return TRUE;}voidprocess_window_maybe_detach (ProcessWindow *pwin){	GtkWidget *dialog;	const char *message;	gint response;	if (pwin->process->status == MP_PROCESS_EXITING)		message = _("Really detach from finished process?");	else		message = _("Really detach from running process?");	dialog = gtk_message_dialog_new (GTK_WINDOW (pwin->main_window),					 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,					 GTK_MESSAGE_QUESTION,					 GTK_BUTTONS_YES_NO,					 message);	gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_YES);	response = gtk_dialog_run (GTK_DIALOG (dialog));	gtk_widget_destroy (dialog);	if (response == GTK_RESPONSE_YES)		process_detach (pwin->process);}voidprocess_window_maybe_kill (ProcessWindow *pwin){	if (pwin->process->status == MP_PROCESS_EXITING)		process_window_maybe_detach (pwin);	else {		GtkWidget *dialog;		gint response;		dialog = gtk_message_dialog_new (GTK_WINDOW (pwin->main_window),						 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,						 GTK_MESSAGE_QUESTION,						 GTK_BUTTONS_YES_NO,						 _("Really kill running process?"));		gtk_dialog_set_default_response (GTK_DIALOG (dialog),						 GTK_RESPONSE_YES);		response = gtk_dialog_run (GTK_DIALOG (dialog));		gtk_widget_destroy (dialog);		if (response == GTK_RESPONSE_YES)			process_kill (pwin->process);	}}voidsigchld_handler (int signum){	int old_errno = errno;		while (1) {		int pid = waitpid (WAIT_ANY, NULL, WNOHANG);		if (pid < 0 && errno != ECHILD)			g_error ("waitpid: %s\n", g_strerror (errno));		else if (pid <= 0)			break;	}	errno = old_errno;}intmain(int argc, char **argv){       static const struct poptOption memprof_popt_options [] = {	       { "follow-fork", '\0', POPT_ARG_NONE, &default_follow_fork, 0,		 N_("Create new windows for forked processes"), NULL },	       { "follow-exec", '\0', POPT_ARG_NONE, &default_follow_exec, 0,		 N_("Retain windows for processes after exec()"), NULL },	       { NULL, '\0', 0, NULL, 0 },       };       poptContext ctx;       GnomeProgram *program;       const char **startup_args;       ProcessWindow *initial_window;       /* Set up a handler for SIGCHLD to avoid zombie children	*/       signal (SIGCHLD, sigchld_handler);       bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);       textdomain (GETTEXT_PACKAGE);#ifdef HAVE_BIND_TEXTDOMAIN_CODESET       bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");#endif       program = gnome_program_init (PACKAGE, VERSION,				     LIBGNOMEUI_MODULE,				     argc, argv,				     GNOME_PARAM_POPT_TABLE, memprof_popt_options,				     NULL);       g_object_get (G_OBJECT (program),		     GNOME_PARAM_POPT_CONTEXT, &ctx,		     NULL);       glade_gnome_init ();       glade_file = "./memprof.glade";       if (!g_file_exists (glade_file)) {	       glade_file = g_concat_dir_and_file (DATADIR, "memprof.glade");       }       if (!g_file_exists (glade_file)) {	       show_error (NULL, ERROR_FATAL, _("Cannot find memprof.glade"));       }       global_server = mp_server_new ();       g_signal_connect (G_OBJECT (global_server), "process_created",		       G_CALLBACK (process_created_cb), NULL);              initial_window = process_window_new ();       gconf_client_add_dir (gconf_client_get_default (),		             "/apps/memprof",			     GCONF_CLIENT_PRELOAD_NONE, NULL);       skip_funcs = gconf_client_get_list (gconf_client_get_default (),					   "/apps/memprof/skip_funcs",					   GCONF_VALUE_STRING, NULL);       skip_regexes = gconf_client_get_list (gconf_client_get_default (),					     "/apps/memprof/skip_regexes",					     GCONF_VALUE_STRING, NULL);       stack_command = gconf_client_get_string (gconf_client_get_default (),						"/apps/memprof/stack_command",						NULL);       gconf_client_notify_add (gconf_client_get_default (),			        "/apps/memprof/stack_command",			        preferences_stack_command_notify,			        NULL, NULL, NULL);       gconf_client_notify_add (gconf_client_get_default (),				"/apps/memprof/skip_funcs",				preferences_list_notify,				NULL, NULL, NULL);       gconf_client_notify_add (gconf_client_get_default (),				"/apps/memprof/skip_regexes",				preferences_list_notify,				NULL, NULL, NULL);       gtk_widget_show (initial_window->main_window);       startup_args = poptGetArgs (ctx);       if (startup_args)	       run_file (initial_window, (char **)startup_args);       poptFreeContext (ctx);       gtk_main ();       return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?