Skip to content

Instantly share code, notes, and snippets.

@oleavr
Created June 5, 2015 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleavr/a3bc08df2b0de0b5cbf9 to your computer and use it in GitHub Desktop.
Save oleavr/a3bc08df2b0de0b5cbf9 to your computer and use it in GitHub Desktop.
diff --git a/glib/Makefile.am b/glib/Makefile.am
index e022c30..8a1f942 100644
--- a/glib/Makefile.am
+++ b/glib/Makefile.am
@@ -209,6 +209,8 @@ libglib_2_0_la_SOURCES = \
gwakeup.c \
gprintf.c \
gprintfint.h \
+ frida-log.c \
+ frida-log.h \
valgrind.h
if OS_UNIX
diff --git a/glib/frida-log.c b/glib/frida-log.c
new file mode 100644
index 0000000..adb7da9
--- /dev/null
+++ b/glib/frida-log.c
@@ -0,0 +1,79 @@
+#include "frida-log.h"
+
+#include "ghash.h"
+#include "gmain.h"
+#include "gslice.h"
+#include "gthread.h"
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef struct _FridaLogThreadData FridaLogThreadData;
+
+struct _FridaLogThreadData
+{
+ gchar name;
+ gint64 last_log;
+};
+
+G_LOCK_DEFINE (frida_log);
+static GHashTable * thread_data_by_handle;
+static gchar next_thread_name = 'A';
+
+void
+frida_log (const gchar * format,
+ ...)
+{
+ int saved_errno;
+ gint64 now;
+ va_list vl;
+ gchar message[256];
+ GThread * thread;
+ FridaLogThreadData * data;
+ gint64 delta;
+
+ saved_errno = errno;
+
+ now = g_get_monotonic_time ();
+
+ va_start (vl, format);
+ vsprintf (message, format, vl);
+ va_end (vl);
+
+ thread = g_thread_self ();
+
+ G_LOCK (frida_log);
+
+ if (thread_data_by_handle == NULL)
+ thread_data_by_handle = g_hash_table_new_full (NULL, NULL, NULL, NULL);
+
+ data = g_hash_table_lookup (thread_data_by_handle, thread);
+ if (data == NULL)
+ {
+ data = g_slice_new (FridaLogThreadData);
+ data->name = next_thread_name++;
+ data->last_log = now;
+ g_hash_table_insert (thread_data_by_handle, thread, data);
+
+ delta = 0;
+ }
+ else
+ {
+ delta = now - data->last_log;
+ data->last_log = now;
+ }
+
+ fprintf (stderr,
+ "\033[01;%dm[Thread %c +%dms] %s\033[00m\n",
+ (int) (31 + (data->name - 'A')),
+ data->name,
+ (int) (delta / 1000),
+ message);
+ fflush (stderr);
+
+ G_UNLOCK (frida_log);
+
+ errno = saved_errno;
+}
diff --git a/glib/frida-log.h b/glib/frida-log.h
new file mode 100644
index 0000000..e2b5547
--- /dev/null
+++ b/glib/frida-log.h
@@ -0,0 +1,9 @@
+#ifndef __FRIDA_LOG_H__
+#define __FRIDA_LOG_H__
+
+#include <glib/gtypes.h>
+
+__attribute__((__format__ (__printf__, 1, 2)))
+G_GNUC_INTERNAL void frida_log (const gchar * format, ...);
+
+#endif
diff --git a/glib/gmain.c b/glib/gmain.c
index be1427e..541a2da 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -95,6 +95,7 @@
#include "gtimer.h"
#endif
+#include "frida-log.h"
#include "gwakeup.h"
#include "gmain-internal.h"
#include "glib-init.h"
@@ -4625,6 +4626,13 @@ g_timeout_dispatch (GSource *source,
again = callback (user_data);
+ frida_log ("timeout name=%s interval=%u%s callback=%p again=%s",
+ g_source_get_name (source),
+ timeout_source->interval,
+ timeout_source->seconds ? "s" : "ms",
+ callback,
+ again ? "TRUE" : "FALSE");
+
if (again)
g_timeout_set_expiration (timeout_source, g_source_get_time (source));
@@ -5475,7 +5483,12 @@ g_idle_dispatch (GSource *source,
return FALSE;
}
- return callback (user_data);
+ gboolean result = callback (user_data);
+ frida_log ("idle name=%s callback=%p again=%s",
+ g_source_get_name (source),
+ callback,
+ result ? "TRUE" : "FALSE");
+ return result;
}
/**
diff --git a/glib/gpoll.c b/glib/gpoll.c
index adc6782..7235bf1 100644
--- a/glib/gpoll.c
+++ b/glib/gpoll.c
@@ -74,6 +74,7 @@
#include <windows.h>
#endif /* G_OS_WIN32 */
+#include "frida-log.h"
#include "gpoll.h"
#ifdef G_OS_WIN32
@@ -379,26 +380,98 @@ g_poll (GPollFD *fds,
maxfd = f->fd;
}
+ memset (&tv, 0, sizeof (tv));
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
+ {
+ GString * requested_events;
+
+ requested_events = g_string_new ("");
+
+ for (f = fds; f < &fds[nfds]; ++f)
+ if (f->fd >= 0)
+ {
+ g_string_append_printf (requested_events, " %d:", f->fd);
+ if (f->events & G_IO_IN)
+ g_string_append (requested_events, " IN");
+ if (f->events & G_IO_OUT)
+ g_string_append (requested_events, " OUT");
+ if (f->events & G_IO_PRI)
+ g_string_append (requested_events, " PRI");
+ }
+
+ if (timeout == -1)
+ {
+ frida_log (">>> select(timeout=%d) nfds=%u maxfd=%u%s",
+ timeout,
+ nfds,
+ maxfd,
+ requested_events->str);
+ }
+ else
+ {
+ frida_log (">>> select(timeout=%d -> {tv_sec=%d tv_usec=%d}) nfds=%u maxfd=%u%s",
+ timeout,
+ (int) tv.tv_sec,
+ (int) tv.tv_usec,
+ nfds,
+ maxfd,
+ requested_events->str);
+ }
+
+ g_string_free (requested_events, TRUE);
+ }
+
ready = select (maxfd + 1, &rset, &wset, &xset,
timeout == -1 ? NULL : &tv);
+
if (ready > 0)
- for (f = fds; f < &fds[nfds]; ++f)
+ {
{
- f->revents = 0;
- if (f->fd >= 0)
+ GString * received_events;
+
+ received_events = g_string_new ("");
+
+ for (f = fds; f < &fds[nfds]; ++f)
{
- if (FD_ISSET (f->fd, &rset))
- f->revents |= G_IO_IN;
- if (FD_ISSET (f->fd, &wset))
- f->revents |= G_IO_OUT;
- if (FD_ISSET (f->fd, &xset))
- f->revents |= G_IO_PRI;
+ if (f->fd >= 0)
+ {
+ if (FD_ISSET (f->fd, &rset))
+ g_string_append_printf (received_events, " %d:IN", f->fd);
+ if (FD_ISSET (f->fd, &wset))
+ g_string_append_printf (received_events, " %d:OUT", f->fd);
+ if (FD_ISSET (f->fd, &xset))
+ g_string_append_printf (received_events, " %d:PRI", f->fd);
+ }
}
+
+ frida_log ("<<< select() ready=%d%s",
+ ready,
+ received_events->str);
+
+ g_string_free (received_events, TRUE);
}
+ for (f = fds; f < &fds[nfds]; ++f)
+ {
+ f->revents = 0;
+ if (f->fd >= 0)
+ {
+ if (FD_ISSET (f->fd, &rset))
+ f->revents |= G_IO_IN;
+ if (FD_ISSET (f->fd, &wset))
+ f->revents |= G_IO_OUT;
+ if (FD_ISSET (f->fd, &xset))
+ f->revents |= G_IO_PRI;
+ }
+ }
+ }
+ else
+ {
+ frida_log ("<<< select() ready=%d", ready);
+ }
+
return ready;
}
diff --git a/glib/gwakeup.c b/glib/gwakeup.c
index c0f1ba0..760a024 100644
--- a/glib/gwakeup.c
+++ b/glib/gwakeup.c
@@ -109,6 +109,7 @@ g_wakeup_free (GWakeup *wakeup)
#else
+#include "frida-log.h"
#include "glib-unix.h"
#include <fcntl.h>
@@ -151,6 +152,7 @@ g_wakeup_new (void)
if (wakeup->fds[0] != -1)
{
wakeup->fds[1] = -1;
+ frida_log ("created eventfd %d", wakeup->fds[0]);
return wakeup;
}
@@ -164,6 +166,8 @@ g_wakeup_new (void)
!g_unix_set_fd_nonblocking (wakeup->fds[1], TRUE, &error))
g_error ("Set pipes non-blocking for GWakeup: %s\n", error->message);
+ frida_log ("created pipes %d and %d", wakeup->fds[0], wakeup->fds[1]);
+
return wakeup;
}
@@ -207,6 +211,7 @@ g_wakeup_acknowledge (GWakeup *wakeup)
char buffer[16];
/* read until it is empty */
+ frida_log ("g_wakeup_acknowledge(%d)", wakeup->fds[0]);
while (read (wakeup->fds[0], buffer, sizeof buffer) == sizeof buffer);
}
@@ -235,6 +240,7 @@ g_wakeup_signal (GWakeup *wakeup)
/* eventfd() case. It requires a 64-bit counter increment value to be
* written. */
+ frida_log ("g_wakeup_signal(eventfd=%d)", wakeup->fds[0]);
do
res = write (wakeup->fds[0], &one, sizeof one);
while (G_UNLIKELY (res == -1 && errno == EINTR));
@@ -243,6 +249,7 @@ g_wakeup_signal (GWakeup *wakeup)
{
guint8 one = 1;
+ frida_log ("g_wakeup_signal(%d)", wakeup->fds[1]);
/* Non-eventfd() case. Only a single byte needs to be written, and it can
* have an arbitrary value. */
do
@@ -263,6 +270,7 @@ g_wakeup_signal (GWakeup *wakeup)
void
g_wakeup_free (GWakeup *wakeup)
{
+ frida_log ("g_wakeup_free(%d, %d)", wakeup->fds[0], wakeup->fds[1]);
close (wakeup->fds[0]);
if (wakeup->fds[1] != -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment