X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=tools%2Fdbus-monitor.c;h=a4b5478205a4217bfbc794084f3412223674a948;hb=d6ad37c3044ed6c72bf652c6e505c6ad9aa9e889;hp=cbd7a4894d504d2874ebd1d6d0d0bc05fc84daf3;hpb=eb3b99e7c610988823804f5e6c92aa13459605c7;p=platform%2Fupstream%2Fdbus.git diff --git a/tools/dbus-monitor.c b/tools/dbus-monitor.c index cbd7a48..a4b5478 100644 --- a/tools/dbus-monitor.c +++ b/tools/dbus-monitor.c @@ -15,7 +15,7 @@ * * 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 * */ @@ -33,16 +33,34 @@ #include -#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) @@ -60,6 +78,13 @@ gettimeofday (struct timeval *__p, } #endif +inline static void +oom (const char *doing) +{ + fprintf (stderr, "OOM while %s\n", doing); + exit (1); +} + static DBusHandlerResult monitor_filter_func (DBusConnection *connection, DBusMessage *message, @@ -79,7 +104,11 @@ monitor_filter_func (DBusConnection *connection, 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 @@ -191,16 +220,23 @@ profile_filter_func (DBusConnection *connection, static void usage (char *name, int ecode) { - fprintf (stderr, "Usage: %s [--system | --session] [--monitor | --profile ] [watch expressions]\n", name); + fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name); exit (ecode); } -static dbus_bool_t sigint_received = FALSE; - static void -sigint_handler (int signum) +only_one_type (dbus_bool_t *seen_bus_type, + char *name) { - sigint_received = TRUE; + 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 @@ -210,17 +246,49 @@ main (int argc, char *argv[]) DBusError error; DBusBusType type = DBUS_BUS_SESSION; 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")) @@ -232,20 +300,63 @@ main (int argc, char *argv[]) else if (arg[0] == '-') usage (argv[0], 1); else { - numFilters++; - filters = (char **)realloc(filters, numFilters * sizeof(char *)); - filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *)); - snprintf(filters[j], strlen(arg) + 1, "%s", arg); - j++; + 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; + + 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); @@ -269,22 +380,22 @@ main (int argc, char *argv[]) else { dbus_bus_add_match (connection, - "type='signal'", + EAVESDROPPING_RULE ",type='signal'", &error); if (dbus_error_is_set (&error)) goto lose; dbus_bus_add_match (connection, - "type='method_call'", + EAVESDROPPING_RULE ",type='method_call'", &error); if (dbus_error_is_set (&error)) goto lose; dbus_bus_add_match (connection, - "type='method_return'", + EAVESDROPPING_RULE ",type='method_return'", &error); if (dbus_error_is_set (&error)) goto lose; dbus_bus_add_match (connection, - "type='error'", + EAVESDROPPING_RULE ",type='error'", &error); if (dbus_error_is_set (&error)) goto lose; @@ -295,10 +406,7 @@ main (int argc, char *argv[]) exit (1); } - /* we handle SIGINT so exit() is reached and flushes stdout */ - signal (SIGINT, sigint_handler); - while (dbus_connection_read_write_dispatch(connection, -1) - && !sigint_received) + while (dbus_connection_read_write_dispatch(connection, -1)) ; exit (0); lose: