libgimp: move the debug code to new private files gimp-debug.[ch]

This commit is contained in:
Michael Natterer 2019-08-04 23:26:31 +02:00
parent a74f4de81e
commit 8c1a43dff7
4 changed files with 239 additions and 134 deletions

View File

@ -115,6 +115,8 @@ libgimp_private_sources = \
gimplegacy.h \
gimpplugin-private.c \
gimpplugin-private.h \
gimp-debug.c \
gimp-debug.h \
gimp-private.h \
gimp-shm.c \
gimp-shm.h \

175
libgimp/gimp-debug.c Normal file
View File

@ -0,0 +1,175 @@
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimp-debug.c
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <glib.h>
#ifndef G_OS_WIN32
#include "libgimpbase/gimpsignal.h"
#else
#ifdef HAVE_EXCHNDL
#include <time.h>
#include <exchndl.h>
#endif
#include <signal.h>
#endif
#if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
# ifdef STRICT
# undef STRICT
# endif
# define STRICT
# ifdef _WIN32_WINNT
# undef _WIN32_WINNT
# endif
# define _WIN32_WINNT 0x0601
# include <windows.h>
# include <tlhelp32.h>
# undef RGB
#endif
#include "gimp.h"
#include "gimp-debug.h"
static const GDebugKey gimp_debug_keys[] =
{
{ "pid", GIMP_DEBUG_PID },
{ "fatal-warnings", GIMP_DEBUG_FATAL_WARNINGS },
{ "fw", GIMP_DEBUG_FATAL_WARNINGS },
{ "query", GIMP_DEBUG_QUERY },
{ "init", GIMP_DEBUG_INIT },
{ "run", GIMP_DEBUG_RUN },
{ "quit", GIMP_DEBUG_QUIT },
{ "on", GIMP_DEBUG_DEFAULT }
};
static guint gimp_debug_flags = 0;
void
_gimp_debug_init (const gchar *basename)
{
const gchar *env_string = g_getenv ("GIMP_PLUGIN_DEBUG");
if (env_string)
{
gchar *debug_string;
const gchar *debug_messages;
debug_string = strchr (env_string, ',');
if (debug_string)
{
gint len = debug_string - env_string;
if ((strlen (basename) == len) &&
(strncmp (basename, env_string, len) == 0))
{
gimp_debug_flags =
g_parse_debug_string (debug_string + 1,
gimp_debug_keys,
G_N_ELEMENTS (gimp_debug_keys));
}
}
else if (strcmp (env_string, basename) == 0)
{
gimp_debug_flags = GIMP_DEBUG_DEFAULT;
}
/* make debug output visible by setting G_MESSAGES_DEBUG */
debug_messages = g_getenv ("G_MESSAGES_DEBUG");
if (debug_messages)
{
gchar *tmp = g_strconcat (debug_messages, ",LibGimp", NULL);
g_setenv ("G_MESSAGES_DEBUG", tmp, TRUE);
g_free (tmp);
}
else
{
g_setenv ("G_MESSAGES_DEBUG", "LibGimp", TRUE);
}
}
}
guint
_gimp_debug_flags (void)
{
return gimp_debug_flags;
}
void
_gimp_debug_stop (void)
{
#ifndef G_OS_WIN32
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Waiting for debugger...");
raise (SIGSTOP);
#else
HANDLE hThreadSnap = NULL;
THREADENTRY32 te32 = { 0 };
pid_t opid = getpid ();
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"Debugging (restart externally): %ld",
(long int) opid);
hThreadSnap = CreateToolhelp32Snapshot (TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE)
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"error getting threadsnap - debugging impossible");
return;
}
te32.dwSize = sizeof (THREADENTRY32);
if (Thread32First (hThreadSnap, &te32))
{
do
{
if (te32.th32OwnerProcessID == opid)
{
HANDLE hThread = OpenThread (THREAD_SUSPEND_RESUME, FALSE,
te32.th32ThreadID);
SuspendThread (hThread);
CloseHandle (hThread);
}
}
while (Thread32Next (hThreadSnap, &te32));
}
else
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "error getting threads");
}
CloseHandle (hThreadSnap);
#endif
}

48
libgimp/gimp-debug.h Normal file
View File

@ -0,0 +1,48 @@
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimp-debug.h
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#ifndef __GIMP_DEBUG_H__
#define __GIMP_DEBUG_H__
G_BEGIN_DECLS
typedef enum
{
GIMP_DEBUG_PID = 1 << 0,
GIMP_DEBUG_FATAL_WARNINGS = 1 << 1,
GIMP_DEBUG_QUERY = 1 << 2,
GIMP_DEBUG_INIT = 1 << 3,
GIMP_DEBUG_RUN = 1 << 4,
GIMP_DEBUG_QUIT = 1 << 5,
GIMP_DEBUG_DEFAULT = (GIMP_DEBUG_RUN | GIMP_DEBUG_FATAL_WARNINGS)
} GimpDebugFlag;
void _gimp_debug_init (const gchar *basename);
guint _gimp_debug_flags (void);
void _gimp_debug_stop (void);
G_END_DECLS
#endif /* __GIMP_DEBUG_H__ */

View File

@ -96,6 +96,7 @@
#include "libgimpbase/gimpprotocol.h"
#include "libgimpbase/gimpwire.h"
#include "gimp-debug.h"
#include "gimp-private.h"
#include "gimp-shm.h"
#include "gimpgpcompat.h"
@ -106,19 +107,6 @@
#include "libgimp-intl.h"
/* Maybe this should go in a public header if we add other things to it */
typedef enum
{
GIMP_DEBUG_PID = 1 << 0,
GIMP_DEBUG_FATAL_WARNINGS = 1 << 1,
GIMP_DEBUG_QUERY = 1 << 2,
GIMP_DEBUG_INIT = 1 << 3,
GIMP_DEBUG_RUN = 1 << 4,
GIMP_DEBUG_QUIT = 1 << 5,
GIMP_DEBUG_DEFAULT = (GIMP_DEBUG_RUN | GIMP_DEBUG_FATAL_WARNINGS)
} GimpDebugFlag;
#define WRITE_BUFFER_SIZE 1024
@ -128,7 +116,6 @@ static gint gimp_main_internal (GType plug_in_ty
gchar *argv[]);
static void gimp_close (void);
static void gimp_debug_stop (void);
static void gimp_message_func (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
@ -187,20 +174,6 @@ static gulong write_buffer_index = 0;
static GimpStackTraceMode stack_trace_mode = GIMP_STACK_TRACE_NEVER;
static guint gimp_debug_flags = 0;
static const GDebugKey gimp_debug_keys[] =
{
{ "pid", GIMP_DEBUG_PID },
{ "fatal-warnings", GIMP_DEBUG_FATAL_WARNINGS },
{ "fw", GIMP_DEBUG_FATAL_WARNINGS },
{ "query", GIMP_DEBUG_QUERY },
{ "init", GIMP_DEBUG_INIT },
{ "run", GIMP_DEBUG_RUN },
{ "quit", GIMP_DEBUG_QUIT },
{ "on", GIMP_DEBUG_DEFAULT }
};
static GimpPlugIn *PLUG_IN = NULL;
static GimpPlugInInfo PLUG_IN_INFO = { 0, };
@ -268,9 +241,8 @@ gimp_main_internal (GType plug_in_type,
N_ARGS
};
gchar *basename;
const gchar *env_string;
gint protocol_version;
gchar *basename;
gint protocol_version;
#ifdef G_OS_WIN32
gint i, j, k;
@ -456,47 +428,7 @@ gimp_main_internal (GType plug_in_type,
return EXIT_FAILURE;
}
env_string = g_getenv ("GIMP_PLUGIN_DEBUG");
if (env_string)
{
gchar *debug_string;
const gchar *debug_messages;
debug_string = strchr (env_string, ',');
if (debug_string)
{
gint len = debug_string - env_string;
if ((strlen (basename) == len) &&
(strncmp (basename, env_string, len) == 0))
{
gimp_debug_flags =
g_parse_debug_string (debug_string + 1,
gimp_debug_keys,
G_N_ELEMENTS (gimp_debug_keys));
}
}
else if (strcmp (env_string, basename) == 0)
{
gimp_debug_flags = GIMP_DEBUG_DEFAULT;
}
/* make debug output visible by setting G_MESSAGES_DEBUG */
debug_messages = g_getenv ("G_MESSAGES_DEBUG");
if (debug_messages)
{
gchar *tmp = g_strconcat (debug_messages, ",LibGimp", NULL);
g_setenv ("G_MESSAGES_DEBUG", tmp, TRUE);
g_free (tmp);
}
else
{
g_setenv ("G_MESSAGES_DEBUG", "LibGimp", TRUE);
}
}
_gimp_debug_init (basename);
g_free (basename);
@ -647,7 +579,7 @@ gimp_main_internal (GType plug_in_type,
NULL);
}
if (gimp_debug_flags & GIMP_DEBUG_FATAL_WARNINGS)
if (_gimp_debug_flags () & GIMP_DEBUG_FATAL_WARNINGS)
{
GLogLevelFlags fatal_mask;
@ -693,8 +625,8 @@ gimp_main_internal (GType plug_in_type,
gp_has_init_write (_gimp_writechannel, NULL);
}
if (gimp_debug_flags & GIMP_DEBUG_QUERY)
gimp_debug_stop ();
if (_gimp_debug_flags () & GIMP_DEBUG_QUERY)
_gimp_debug_stop ();
if (PLUG_IN)
{
@ -713,8 +645,8 @@ gimp_main_internal (GType plug_in_type,
if (strcmp (argv[ARG_MODE], "-init") == 0)
{
if (gimp_debug_flags & GIMP_DEBUG_INIT)
gimp_debug_stop ();
if (_gimp_debug_flags () & GIMP_DEBUG_INIT)
_gimp_debug_stop ();
if (PLUG_IN)
{
@ -731,9 +663,9 @@ gimp_main_internal (GType plug_in_type,
return EXIT_SUCCESS;
}
if (gimp_debug_flags & GIMP_DEBUG_RUN)
gimp_debug_stop ();
else if (gimp_debug_flags & GIMP_DEBUG_PID)
if (_gimp_debug_flags () & GIMP_DEBUG_RUN)
_gimp_debug_stop ();
else if (_gimp_debug_flags () & GIMP_DEBUG_PID)
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Here I am!");
g_io_add_watch (_gimp_readchannel,
@ -1157,8 +1089,8 @@ gimp_get_progname (void)
static void
gimp_close (void)
{
if (gimp_debug_flags & GIMP_DEBUG_QUIT)
gimp_debug_stop ();
if (_gimp_debug_flags () & GIMP_DEBUG_QUIT)
_gimp_debug_stop ();
if (PLUG_IN)
{
@ -1175,58 +1107,6 @@ gimp_close (void)
gp_quit_write (_gimp_writechannel, NULL);
}
static void
gimp_debug_stop (void)
{
#ifndef G_OS_WIN32
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Waiting for debugger...");
raise (SIGSTOP);
#else
HANDLE hThreadSnap = NULL;
THREADENTRY32 te32 = { 0 };
pid_t opid = getpid ();
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"Debugging (restart externally): %ld",
(long int) opid);
hThreadSnap = CreateToolhelp32Snapshot (TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE)
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"error getting threadsnap - debugging impossible");
return;
}
te32.dwSize = sizeof (THREADENTRY32);
if (Thread32First (hThreadSnap, &te32))
{
do
{
if (te32.th32OwnerProcessID == opid)
{
HANDLE hThread = OpenThread (THREAD_SUSPEND_RESUME, FALSE,
te32.th32ThreadID);
SuspendThread (hThread);
CloseHandle (hThread);
}
}
while (Thread32Next (hThreadSnap, &te32));
}
else
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "error getting threads");
}
CloseHandle (hThreadSnap);
#endif
}
static void
gimp_message_func (const gchar *log_domain,
GLogLevelFlags log_level,