[PATCH 2/2] On Linux, if appearing to run in gdb, disable grabs



If we are on Linux, us /proc/[pid]/cmdline to try to figure out if we
are running in GNU gdb. If we are, disable grabs, but allow this to be
overridden by passing --gtk-force-grabs. Patch inspired by the Qt
toolkit.
---
 gtk/gtkmain.c |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/gtk/gtkmain.c b/gtk/gtkmain.c
index 9d2ee17..420936a 100644
--- a/gtk/gtkmain.c
+++ b/gtk/gtkmain.c
@@ -407,6 +407,7 @@ gtk_disable_setlocale (void)
 
 static GString *gtk_modules_string = NULL;
 static gboolean g_fatal_warnings = FALSE;
+static gboolean g_force_grabs = FALSE;
 
 #ifdef G_ENABLE_DEBUG
 static gboolean
@@ -452,6 +453,8 @@ static const GOptionEntry gtk_args[] = {
     /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
   { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, 
     /* Description of --g-fatal-warnings in --help output */   N_("Make all warnings fatal"), NULL },
+  { "gtk-force-grabs", 0, 0, G_OPTION_ARG_NONE, &g_force_grabs,
+    /* Description of --gtk-force-grabs in --help output */    N_("Enable grabs also when running in GNU gdb"), NULL },
 #ifdef G_ENABLE_DEBUG
   { "gtk-debug",        0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,    
     /* Description of --gtk-debug=FLAGS in --help output */    N_("GTK+ debugging flags to set"), 
@@ -719,6 +722,26 @@ do_post_parse_initialization (int    *argc,
   _gtk_accel_map_init ();
   _gtk_rc_init ();
 
+  /* Handle automatic disable of grabs */
+#ifdef __linux__
+  if (!g_force_grabs &&
+      !(gtk_debug_flags & GTK_DEBUG_NOGRABS))
+    {
+      gchar *filename = g_strdup_printf ("/proc/%d/cmdline", getppid());
+      gchar *contents = NULL;
+
+      if (g_file_get_contents (filename, &contents, NULL, NULL) &&
+          strcmp ("gdb", contents) == 0)
+        {
+          g_printerr ("GTK+: Appearing to run in GNU gdb, grabs disabled, override with --g-force-grabs\n", contents);
+          gtk_debug_flags |= GTK_DEBUG_NOGRABS;
+        }
+
+      g_free (filename);
+      g_free (contents);
+    }
+#endif  /* __linux__ */
+
   /* Set the 'initialized' flag.
    */
   gtk_initialized = TRUE;
-- 
1.6.2.5


--------------020101080509040401080706--


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]