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;
243 main (int argc, char *argv[])
245 DBusConnection *connection;
247 DBusBusType type = DBUS_BUS_SESSION;
248 DBusHandleMessageFunction filter_func = monitor_filter_func;
249 char *address = NULL;
250 dbus_bool_t seen_bus_type = FALSE;
252 int i = 0, j = 0, numFilters = 0;
253 char **filters = NULL;
255 /* Set stdout to be unbuffered; this is basically so that if people
256 * do dbus-monitor > file, then send SIGINT via Control-C, they
257 * don't lose the last chunk of messages.
261 setvbuf (stdout, NULL, _IONBF, 0);
263 setvbuf (stdout, NULL, _IOLBF, 0);
266 for (i = 1; i < argc; i++)
270 if (!strcmp (arg, "--system"))
272 only_one_type (&seen_bus_type, argv[0]);
273 type = DBUS_BUS_SYSTEM;
275 else if (!strcmp (arg, "--session"))
277 only_one_type (&seen_bus_type, argv[0]);
278 type = DBUS_BUS_SESSION;
280 else if (!strcmp (arg, "--address"))
282 only_one_type (&seen_bus_type, argv[0]);
292 else if (!strcmp (arg, "--help"))
294 else if (!strcmp (arg, "--monitor"))
295 filter_func = monitor_filter_func;
296 else if (!strcmp (arg, "--profile"))
297 filter_func = profile_filter_func;
298 else if (!strcmp (arg, "--"))
300 else if (arg[0] == '-')
303 unsigned int filter_len;
305 /* Prepend a rule (and a comma) to enable the monitor to eavesdrop.
306 * Prepending allows the user to add eavesdrop=false at command line
307 * in order to disable eavesdropping when needed */
308 filter_len = strlen (EAVESDROPPING_RULE) + 1 + strlen (arg) + 1;
310 filters = (char **) realloc (filters, numFilters * sizeof (char *));
312 oom ("adding a new filter slot");
313 filters[j] = (char *) malloc (filter_len * sizeof (char *));
314 if (filters[j] == NULL)
315 oom ("adding a new filter");
316 snprintf (filters[j], filter_len, "%s,%s", EAVESDROPPING_RULE, arg);
321 dbus_error_init (&error);
325 connection = dbus_connection_open (address, &error);
328 if (!dbus_bus_register (connection, &error))
330 fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
331 address, error.message);
332 dbus_error_free (&error);
338 connection = dbus_bus_get (type, &error);
339 if (connection == NULL)
348 case DBUS_BUS_SYSTEM:
349 where = "system bus";
351 case DBUS_BUS_SESSION:
352 where = "session bus";
358 fprintf (stderr, "Failed to open connection to %s: %s\n",
361 dbus_error_free (&error);
367 for (i = 0; i < j; i++)
369 dbus_bus_add_match (connection, filters[i], &error);
370 if (dbus_error_is_set (&error))
372 fprintf (stderr, "Failed to setup match \"%s\": %s\n",
373 filters[i], error.message);
374 dbus_error_free (&error);
382 dbus_bus_add_match (connection,
383 EAVESDROPPING_RULE ",type='signal'",
385 if (dbus_error_is_set (&error))
387 dbus_bus_add_match (connection,
388 EAVESDROPPING_RULE ",type='method_call'",
390 if (dbus_error_is_set (&error))
392 dbus_bus_add_match (connection,
393 EAVESDROPPING_RULE ",type='method_return'",
395 if (dbus_error_is_set (&error))
397 dbus_bus_add_match (connection,
398 EAVESDROPPING_RULE ",type='error'",
400 if (dbus_error_is_set (&error))
404 if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
405 fprintf (stderr, "Couldn't add filter!\n");
409 while (dbus_connection_read_write_dispatch(connection, -1))
413 fprintf (stderr, "Error: %s\n", error.message);