X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=tools%2Fdbus-monitor.c;h=a4b5478205a4217bfbc794084f3412223674a948;hb=61d97215c317a4154df47fbfb882aab60b92fbab;hp=c7293abb5bf914198bdb5520b56a5b67b579a37d;hpb=68a3c593b9e77b33614726363c7b6fd85d113021;p=platform%2Fupstream%2Fdbus.git diff --git a/tools/dbus-monitor.c b/tools/dbus-monitor.c index c7293ab..a4b5478 100644 --- a/tools/dbus-monitor.c +++ b/tools/dbus-monitor.c @@ -1,4 +1,4 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ /* dbus-monitor.c Utility program to monitor messages on the bus * * Copyright (C) 2003 Philip Blundell @@ -15,91 +15,402 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ +#include #include #include #include -#include -#include - /* Don't copy this, for programs outside the dbus tree it's dbus/dbus-glib.h */ -#include +#ifdef DBUS_WIN +#include +#undef interface +#else +#include +#endif + +#include + #include "dbus-print-message.h" +#define EAVESDROPPING_RULE "eavesdrop=true" + +#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 + +inline static void +oom (const char *doing) +{ + fprintf (stderr, "OOM while %s\n", doing); + exit (1); +} + static DBusHandlerResult -handler_func (DBusMessageHandler *handler, - DBusConnection *connection, - DBusMessage *message, - void *user_data) +monitor_filter_func (DBusConnection *connection, + DBusMessage *message, + void *user_data) { - print_message (message); + print_message (message, FALSE); if (dbus_message_is_signal (message, - DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, + DBUS_INTERFACE_LOCAL, "Disconnected")) exit (0); - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + /* Conceptually we want this to be + * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises + * some problems. See bug 1719. + */ + return DBUS_HANDLER_RESULT_HANDLED; +} + +#ifdef __APPLE__ +#define PROFILE_TIMED_FORMAT "%s\t%lu\t%d" +#else +#define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu" +#endif +#define TRAP_NULL_STRING(str) ((str) ? (str) : "") + +typedef enum +{ + PROFILE_ATTRIBUTE_FLAG_SERIAL = 1, + PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2, + PROFILE_ATTRIBUTE_FLAG_SENDER = 4, + PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8, + PROFILE_ATTRIBUTE_FLAG_PATH = 16, + PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32, + PROFILE_ATTRIBUTE_FLAG_MEMBER = 64, + PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128 +} ProfileAttributeFlags; + +static void +profile_print_with_attrs (const char *type, DBusMessage *message, + struct timeval *t, ProfileAttributeFlags attrs) +{ + printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec); + + if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL) + printf ("\t%u", dbus_message_get_serial (message)); + + if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL) + printf ("\t%u", dbus_message_get_reply_serial (message)); + + if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER) + printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message))); + + if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION) + printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message))); + + if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH) + printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message))); + + if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE) + printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message))); + + if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER) + printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message))); + + if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME) + printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message))); + + printf ("\n"); +} + +static void +print_message_profile (DBusMessage *message) +{ + struct timeval t; + + if (gettimeofday (&t, NULL) < 0) + { + printf ("un\n"); + return; + } + + switch (dbus_message_get_type (message)) + { + case DBUS_MESSAGE_TYPE_METHOD_CALL: + profile_print_with_attrs ("mc", message, &t, + PROFILE_ATTRIBUTE_FLAG_SERIAL | + PROFILE_ATTRIBUTE_FLAG_SENDER | + PROFILE_ATTRIBUTE_FLAG_PATH | + PROFILE_ATTRIBUTE_FLAG_INTERFACE | + PROFILE_ATTRIBUTE_FLAG_MEMBER); + break; + case DBUS_MESSAGE_TYPE_METHOD_RETURN: + profile_print_with_attrs ("mr", message, &t, + PROFILE_ATTRIBUTE_FLAG_SERIAL | + PROFILE_ATTRIBUTE_FLAG_DESTINATION | + PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL); + break; + case DBUS_MESSAGE_TYPE_ERROR: + profile_print_with_attrs ("err", message, &t, + PROFILE_ATTRIBUTE_FLAG_SERIAL | + PROFILE_ATTRIBUTE_FLAG_DESTINATION | + PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL); + break; + case DBUS_MESSAGE_TYPE_SIGNAL: + profile_print_with_attrs ("sig", message, &t, + PROFILE_ATTRIBUTE_FLAG_SERIAL | + PROFILE_ATTRIBUTE_FLAG_PATH | + PROFILE_ATTRIBUTE_FLAG_INTERFACE | + PROFILE_ATTRIBUTE_FLAG_MEMBER); + break; + default: + printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec); + break; + } +} + +static DBusHandlerResult +profile_filter_func (DBusConnection *connection, + DBusMessage *message, + void *user_data) +{ + print_message_profile (message); + + if (dbus_message_is_signal (message, + DBUS_INTERFACE_LOCAL, + "Disconnected")) + exit (0); + + return DBUS_HANDLER_RESULT_HANDLED; } static void usage (char *name, int ecode) { - fprintf (stderr, "Usage: %s [--system | --session]\n", name); + fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name); exit (ecode); } +static void +only_one_type (dbus_bool_t *seen_bus_type, + char *name) +{ + if (*seen_bus_type) + { + fprintf (stderr, "I only support monitoring one bus at a time!\n"); + usage (name, 1); + } + else + { + *seen_bus_type = TRUE; + } +} + int main (int argc, char *argv[]) { DBusConnection *connection; DBusError error; DBusBusType type = DBUS_BUS_SESSION; - DBusMessageHandler *handler; - GMainLoop *loop; - int i; + DBusHandleMessageFunction filter_func = monitor_filter_func; + char *address = NULL; + dbus_bool_t seen_bus_type = FALSE; + + int i = 0, j = 0, numFilters = 0; + char **filters = NULL; + + /* Set stdout to be unbuffered; this is basically so that if people + * do dbus-monitor > file, then send SIGINT via Control-C, they + * don't lose the last chunk of messages. + */ + +#ifdef DBUS_WIN + setvbuf (stdout, NULL, _IONBF, 0); +#else + setvbuf (stdout, NULL, _IOLBF, 0); +#endif for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!strcmp (arg, "--system")) - type = DBUS_BUS_SYSTEM; + { + only_one_type (&seen_bus_type, argv[0]); + type = DBUS_BUS_SYSTEM; + } else if (!strcmp (arg, "--session")) - type = DBUS_BUS_SESSION; + { + only_one_type (&seen_bus_type, argv[0]); + type = DBUS_BUS_SESSION; + } + else if (!strcmp (arg, "--address")) + { + only_one_type (&seen_bus_type, argv[0]); + + if (i+1 < argc) + { + address = argv[i+1]; + i++; + } + else + usage (argv[0], 1); + } else if (!strcmp (arg, "--help")) usage (argv[0], 0); + else if (!strcmp (arg, "--monitor")) + filter_func = monitor_filter_func; + else if (!strcmp (arg, "--profile")) + filter_func = profile_filter_func; else if (!strcmp (arg, "--")) - break; + continue; else if (arg[0] == '-') usage (argv[0], 1); - } - - if (argc > 2) - usage (argv[0], 1); + else { + unsigned int filter_len; + numFilters++; + /* Prepend a rule (and a comma) to enable the monitor to eavesdrop. + * Prepending allows the user to add eavesdrop=false at command line + * in order to disable eavesdropping when needed */ + filter_len = strlen (EAVESDROPPING_RULE) + 1 + strlen (arg) + 1; - loop = g_main_loop_new (NULL, FALSE); + filters = (char **) realloc (filters, numFilters * sizeof (char *)); + if (filters == NULL) + oom ("adding a new filter slot"); + filters[j] = (char *) malloc (filter_len * sizeof (char *)); + if (filters[j] == NULL) + oom ("adding a new filter"); + snprintf (filters[j], filter_len, "%s,%s", EAVESDROPPING_RULE, arg); + j++; + } + } dbus_error_init (&error); - connection = dbus_bus_get (type, &error); + + if (address != NULL) + { + connection = dbus_connection_open (address, &error); + if (connection) + { + if (!dbus_bus_register (connection, &error)) + { + fprintf (stderr, "Failed to register connection to bus at %s: %s\n", + address, error.message); + dbus_error_free (&error); + exit (1); + } + } + } + else + connection = dbus_bus_get (type, &error); if (connection == NULL) { - fprintf (stderr, "Failed to open connection to %s message bus: %s\n", - (type == DBUS_BUS_SYSTEM) ? "system" : "session", + const char *where; + if (address != NULL) + where = address; + else + { + switch (type) + { + case DBUS_BUS_SYSTEM: + where = "system bus"; + break; + case DBUS_BUS_SESSION: + where = "session bus"; + break; + default: + where = ""; + } + } + fprintf (stderr, "Failed to open connection to %s: %s\n", + where, error.message); dbus_error_free (&error); exit (1); } - dbus_connection_setup_with_g_main (connection, NULL); - - handler = dbus_message_handler_new (handler_func, NULL, NULL); - dbus_connection_add_filter (connection, handler); + if (numFilters) + { + for (i = 0; i < j; i++) + { + dbus_bus_add_match (connection, filters[i], &error); + if (dbus_error_is_set (&error)) + { + fprintf (stderr, "Failed to setup match \"%s\": %s\n", + filters[i], error.message); + dbus_error_free (&error); + exit (1); + } + free(filters[i]); + } + } + else + { + dbus_bus_add_match (connection, + EAVESDROPPING_RULE ",type='signal'", + &error); + if (dbus_error_is_set (&error)) + goto lose; + dbus_bus_add_match (connection, + EAVESDROPPING_RULE ",type='method_call'", + &error); + if (dbus_error_is_set (&error)) + goto lose; + dbus_bus_add_match (connection, + EAVESDROPPING_RULE ",type='method_return'", + &error); + if (dbus_error_is_set (&error)) + goto lose; + dbus_bus_add_match (connection, + EAVESDROPPING_RULE ",type='error'", + &error); + if (dbus_error_is_set (&error)) + goto lose; + } - g_main_loop_run (loop); + if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) { + fprintf (stderr, "Couldn't add filter!\n"); + exit (1); + } + while (dbus_connection_read_write_dispatch(connection, -1)) + ; exit (0); + lose: + fprintf (stderr, "Error: %s\n", error.message); + exit (1); } +