From 96d661ce36e343a3d29e2a2d29679ffaf31b9453 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Mon, 23 Feb 2015 22:00:46 +0100 Subject: [PATCH 1/2] dbus-monitor: Add timestamp to --monitor mode. Use cross platform function _dbus_get_real_time() for fetching current time. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=88896 [smcv: use %ld to avoid needing casts; reinstate printing the timestamp; libdbus-1 is sufficient now that fd.o#83115 is fixed; print timestamp for non-literal dbus-send replies too] --- dbus/dbus-sysdeps.h | 1 + tools/dbus-monitor.c | 71 ++++++++++------------------------------------ tools/dbus-print-message.c | 22 ++++++++++---- tools/dbus-print-message.h | 2 +- tools/dbus-send.c | 6 +++- 5 files changed, 38 insertions(+), 64 deletions(-) diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index e7a556b..2dbc420 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -336,6 +336,7 @@ DBUS_PRIVATE_EXPORT void _dbus_get_monotonic_time (long *tv_sec, long *tv_usec); +DBUS_PRIVATE_EXPORT void _dbus_get_real_time (long *tv_sec, long *tv_usec); diff --git a/tools/dbus-monitor.c b/tools/dbus-monitor.c index 14ceae9..0aea2e7 100644 --- a/tools/dbus-monitor.c +++ b/tools/dbus-monitor.c @@ -48,53 +48,16 @@ /* http://www.tcpdump.org/linktypes.html */ #define LINKTYPE_DBUS 231 -#ifdef DBUS_WIN - -/* gettimeofday is not defined on windows */ -#define DBUS_SECONDS_SINCE_1601 11644473600LL -#define DBUS_USEC_IN_SEC 1000000LL - -#ifdef DBUS_WINCE - -#ifndef _IOLBF -#define _IOLBF 0x40 -#endif -#ifndef _IONBF -#define _IONBF 0x04 -#endif - -void -GetSystemTimeAsFileTime (LPFILETIME ftp) -{ - SYSTEMTIME st; - GetSystemTime (&st); - SystemTimeToFileTime (&st, ftp); -} -#endif - -static int -gettimeofday (struct timeval *__p, - void *__t) -{ - union { - unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ - FILETIME ft; - } now; - - GetSystemTimeAsFileTime (&now.ft); - __p->tv_usec = (long) ((now.ns100 / 10LL) % DBUS_USEC_IN_SEC); - __p->tv_sec = (long)(((now.ns100 / 10LL) / DBUS_SECONDS_SINCE_1601) - DBUS_SECONDS_SINCE_1601); - - return 0; -} -#endif - static DBusHandlerResult monitor_filter_func (DBusConnection *connection, DBusMessage *message, void *user_data) { - print_message (message, FALSE); + long sec = 0, usec = 0; + + _dbus_get_real_time (&sec, &usec); + + print_message (message, FALSE, sec, usec); if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, @@ -130,9 +93,9 @@ profile_print_headers (void) static void profile_print_with_attrs (const char *type, DBusMessage *message, - struct timeval *t, ProfileAttributeFlags attrs) + long sec, long usec, ProfileAttributeFlags attrs) { - printf ("%s\t%lu.%06lu", type, (unsigned long) t->tv_sec, (unsigned long) t->tv_usec); + printf ("%s\t%ld.%06ld", type, sec, usec); if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL) printf ("\t%u", dbus_message_get_serial (message)); @@ -165,13 +128,7 @@ static void print_message_profile (DBusMessage *message) { static dbus_bool_t first = TRUE; - struct timeval t; - - if (gettimeofday (&t, NULL) < 0) - { - printf ("un\n"); - return; - } + long sec = 0, usec = 0; if (first) { @@ -179,10 +136,12 @@ print_message_profile (DBusMessage *message) first = FALSE; } + _dbus_get_real_time (&sec, &usec); + switch (dbus_message_get_type (message)) { case DBUS_MESSAGE_TYPE_METHOD_CALL: - profile_print_with_attrs ("mc", message, &t, + profile_print_with_attrs ("mc", message, sec, usec, PROFILE_ATTRIBUTE_FLAG_SERIAL | PROFILE_ATTRIBUTE_FLAG_SENDER | PROFILE_ATTRIBUTE_FLAG_DESTINATION | @@ -191,21 +150,21 @@ print_message_profile (DBusMessage *message) PROFILE_ATTRIBUTE_FLAG_MEMBER); break; case DBUS_MESSAGE_TYPE_METHOD_RETURN: - profile_print_with_attrs ("mr", message, &t, + profile_print_with_attrs ("mr", message, sec, usec, PROFILE_ATTRIBUTE_FLAG_SERIAL | PROFILE_ATTRIBUTE_FLAG_SENDER | PROFILE_ATTRIBUTE_FLAG_DESTINATION | PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL); break; case DBUS_MESSAGE_TYPE_ERROR: - profile_print_with_attrs ("err", message, &t, + profile_print_with_attrs ("err", message, sec, usec, PROFILE_ATTRIBUTE_FLAG_SERIAL | PROFILE_ATTRIBUTE_FLAG_SENDER | PROFILE_ATTRIBUTE_FLAG_DESTINATION | PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL); break; case DBUS_MESSAGE_TYPE_SIGNAL: - profile_print_with_attrs ("sig", message, &t, + profile_print_with_attrs ("sig", message, sec, usec, PROFILE_ATTRIBUTE_FLAG_SERIAL | PROFILE_ATTRIBUTE_FLAG_SENDER | PROFILE_ATTRIBUTE_FLAG_DESTINATION | @@ -214,7 +173,7 @@ print_message_profile (DBusMessage *message) PROFILE_ATTRIBUTE_FLAG_MEMBER); break; default: - printf ("%s\t%lu.%06lu", "tun", (unsigned long) t.tv_sec, (unsigned long) t.tv_usec); + printf ("%s\t%ld.%06ld", "tun", sec, usec); break; } } diff --git a/tools/dbus-print-message.c b/tools/dbus-print-message.c index 27c8896..d598b53 100644 --- a/tools/dbus-print-message.c +++ b/tools/dbus-print-message.c @@ -529,7 +529,7 @@ print_iter (DBusMessageIter *iter, dbus_bool_t literal, int depth) } void -print_message (DBusMessage *message, dbus_bool_t literal) +print_message (DBusMessage *message, dbus_bool_t literal, long sec, long usec) { DBusMessageIter iter; const char *sender; @@ -542,11 +542,21 @@ print_message (DBusMessage *message, dbus_bool_t literal) if (!literal) { - printf ("%s sender=%s -> dest=%s", - type_to_name (message_type), - sender ? sender : "(null sender)", - destination ? destination : "(null destination)"); - + if (sec != 0 || usec != 0) + { + printf ("%s time=%ld.%06ld sender=%s -> dest=%s", + type_to_name (message_type), sec, usec, + sender ? sender : "(null sender)", + destination ? destination : "(null destination)"); + } + else + { + printf ("%s sender=%s -> dest=%s", + type_to_name (message_type), + sender ? sender : "(null sender)", + destination ? destination : "(null destination)"); + } + switch (message_type) { case DBUS_MESSAGE_TYPE_METHOD_CALL: diff --git a/tools/dbus-print-message.h b/tools/dbus-print-message.h index 26700d8..d45bc79 100644 --- a/tools/dbus-print-message.h +++ b/tools/dbus-print-message.h @@ -26,6 +26,6 @@ #include #include -void print_message (DBusMessage *message, dbus_bool_t literal); +void print_message (DBusMessage *message, dbus_bool_t literal, long sec, long usec); #endif /* DBUS_PRINT_MESSAGE_H */ diff --git a/tools/dbus-send.c b/tools/dbus-send.c index d3ff258..2a99e9a 100644 --- a/tools/dbus-send.c +++ b/tools/dbus-send.c @@ -25,6 +25,7 @@ #include #include +#include "dbus/dbus-internals.h" #ifndef HAVE_STRTOLL #undef strtoll @@ -573,7 +574,10 @@ main (int argc, char *argv[]) if (reply) { - print_message (reply, print_reply_literal); + long sec, usec; + + _dbus_get_real_time (&sec, &usec); + print_message (reply, print_reply_literal, sec, usec); dbus_message_unref (reply); } } -- 2.1.4