1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-monitor.c Utility program to monitor messages on the bus
4 * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36 #include "dbus-print-message.h"
38 #define EAVESDROPPING_RULE "eavesdrop=true"
42 /* gettimeofday is not defined on windows */
43 #define DBUS_SECONDS_SINCE_1601 11644473600LL
44 #define DBUS_USEC_IN_SEC 1000000LL
56 GetSystemTimeAsFileTime (LPFILETIME ftp)
60 SystemTimeToFileTime (&st, ftp);
65 gettimeofday (struct timeval *__p,
69 unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
73 GetSystemTimeAsFileTime (&now.ft);
74 __p->tv_usec = (long) ((now.ns100 / 10LL) % DBUS_USEC_IN_SEC);
75 __p->tv_sec = (long)(((now.ns100 / 10LL) / DBUS_SECONDS_SINCE_1601) - DBUS_SECONDS_SINCE_1601);
82 oom (const char *doing)
84 fprintf (stderr, "OOM while %s\n", doing);
88 static DBusHandlerResult
89 monitor_filter_func (DBusConnection *connection,
93 print_message (message, FALSE);
95 if (dbus_message_is_signal (message,
100 /* Conceptually we want this to be
101 * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
102 * some problems. See bug 1719.
104 return DBUS_HANDLER_RESULT_HANDLED;
108 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%d"
110 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
112 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
116 PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
117 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
118 PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
119 PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
120 PROFILE_ATTRIBUTE_FLAG_PATH = 16,
121 PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
122 PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
123 PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128
124 } ProfileAttributeFlags;
127 profile_print_with_attrs (const char *type, DBusMessage *message,
128 struct timeval *t, ProfileAttributeFlags attrs)
130 printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
132 if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
133 printf ("\t%u", dbus_message_get_serial (message));
135 if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
136 printf ("\t%u", dbus_message_get_reply_serial (message));
138 if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
139 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
141 if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
142 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
144 if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
145 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
147 if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
148 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
150 if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
151 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
153 if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
154 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
160 print_message_profile (DBusMessage *message)
164 if (gettimeofday (&t, NULL) < 0)
170 switch (dbus_message_get_type (message))
172 case DBUS_MESSAGE_TYPE_METHOD_CALL:
173 profile_print_with_attrs ("mc", message, &t,
174 PROFILE_ATTRIBUTE_FLAG_SERIAL |
175 PROFILE_ATTRIBUTE_FLAG_SENDER |
176 PROFILE_ATTRIBUTE_FLAG_PATH |
177 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
178 PROFILE_ATTRIBUTE_FLAG_MEMBER);
180 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
181 profile_print_with_attrs ("mr", message, &t,
182 PROFILE_ATTRIBUTE_FLAG_SERIAL |
183 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
184 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
186 case DBUS_MESSAGE_TYPE_ERROR:
187 profile_print_with_attrs ("err", message, &t,
188 PROFILE_ATTRIBUTE_FLAG_SERIAL |
189 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
190 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
192 case DBUS_MESSAGE_TYPE_SIGNAL:
193 profile_print_with_attrs ("sig", message, &t,
194 PROFILE_ATTRIBUTE_FLAG_SERIAL |
195 PROFILE_ATTRIBUTE_FLAG_PATH |
196 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
197 PROFILE_ATTRIBUTE_FLAG_MEMBER);
200 printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
205 static DBusHandlerResult
206 profile_filter_func (DBusConnection *connection,
207 DBusMessage *message,
210 print_message_profile (message);
212 if (dbus_message_is_signal (message,
213 DBUS_INTERFACE_LOCAL,
217 return DBUS_HANDLER_RESULT_HANDLED;
221 usage (char *name, int ecode)
223 fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name);
228 only_one_type (dbus_bool_t *seen_bus_type,
233 fprintf (stderr, "I only support monitoring one bus at a time!\n");
238 *seen_bus_type = TRUE;
242 static dbus_bool_t sigint_received = FALSE;
245 sigint_handler (int signum)
247 sigint_received = TRUE;
251 main (int argc, char *argv[])
253 DBusConnection *connection;
255 DBusBusType type = DBUS_BUS_SESSION;
256 DBusHandleMessageFunction filter_func = monitor_filter_func;
257 char *address = NULL;
258 dbus_bool_t seen_bus_type = FALSE;
260 int i = 0, j = 0, numFilters = 0;
261 char **filters = NULL;
263 /* Set stdout to be unbuffered; this is basically so that if people
264 * do dbus-monitor > file, then send SIGINT via Control-C, they
265 * don't lose the last chunk of messages.
269 setvbuf (stdout, NULL, _IONBF, 0);
271 setvbuf (stdout, NULL, _IOLBF, 0);
274 for (i = 1; i < argc; i++)
278 if (!strcmp (arg, "--system"))
280 only_one_type (&seen_bus_type, argv[0]);
281 type = DBUS_BUS_SYSTEM;
283 else if (!strcmp (arg, "--session"))
285 only_one_type (&seen_bus_type, argv[0]);
286 type = DBUS_BUS_SESSION;
288 else if (!strcmp (arg, "--address"))
290 only_one_type (&seen_bus_type, argv[0]);
300 else if (!strcmp (arg, "--help"))
302 else if (!strcmp (arg, "--monitor"))
303 filter_func = monitor_filter_func;
304 else if (!strcmp (arg, "--profile"))
305 filter_func = profile_filter_func;
306 else if (!strcmp (arg, "--"))
308 else if (arg[0] == '-')
311 unsigned int filter_len;
313 /* Prepend a rule (and a comma) to enable the monitor to eavesdrop.
314 * Prepending allows the user to add eavesdrop=false at command line
315 * in order to disable eavesdropping when needed */
316 filter_len = strlen (EAVESDROPPING_RULE) + 1 + strlen (arg) + 1;
318 filters = (char **) realloc (filters, numFilters * sizeof (char *));
320 oom ("adding a new filter slot");
321 filters[j] = (char *) malloc (filter_len * sizeof (char *));
322 if (filters[j] == NULL)
323 oom ("adding a new filter");
324 snprintf (filters[j], filter_len, "%s,%s", EAVESDROPPING_RULE, arg);
329 dbus_error_init (&error);
333 connection = dbus_connection_open (address, &error);
336 if (!dbus_bus_register (connection, &error))
338 fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
339 address, error.message);
340 dbus_error_free (&error);
346 connection = dbus_bus_get (type, &error);
347 if (connection == NULL)
356 case DBUS_BUS_SYSTEM:
357 where = "system bus";
359 case DBUS_BUS_SESSION:
360 where = "session bus";
366 fprintf (stderr, "Failed to open connection to %s: %s\n",
369 dbus_error_free (&error);
375 for (i = 0; i < j; i++)
377 dbus_bus_add_match (connection, filters[i], &error);
378 if (dbus_error_is_set (&error))
380 fprintf (stderr, "Failed to setup match \"%s\": %s\n",
381 filters[i], error.message);
382 dbus_error_free (&error);
390 dbus_bus_add_match (connection,
391 EAVESDROPPING_RULE ",type='signal'",
393 if (dbus_error_is_set (&error))
395 dbus_bus_add_match (connection,
396 EAVESDROPPING_RULE ",type='method_call'",
398 if (dbus_error_is_set (&error))
400 dbus_bus_add_match (connection,
401 EAVESDROPPING_RULE ",type='method_return'",
403 if (dbus_error_is_set (&error))
405 dbus_bus_add_match (connection,
406 EAVESDROPPING_RULE ",type='error'",
408 if (dbus_error_is_set (&error))
412 if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
413 fprintf (stderr, "Couldn't add filter!\n");
417 while (dbus_connection_read_write_dispatch(connection, -1))
421 fprintf (stderr, "Error: %s\n", error.message);