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
24 #include "dbus/dbus-internals.h"
39 #include "dbus-print-message.h"
40 #include "tool-common.h"
42 #define EAVESDROPPING_RULE "eavesdrop=true"
45 #define STDOUT_FILENO 1
48 /* http://www.tcpdump.org/linktypes.html */
49 #define LINKTYPE_DBUS 231
51 static DBusHandlerResult
52 monitor_filter_func (DBusConnection *connection,
56 long sec = 0, usec = 0;
58 _dbus_get_real_time (&sec, &usec);
60 print_message (message, FALSE, sec, usec);
62 if (dbus_message_is_signal (message,
67 /* Monitors must not allow libdbus to reply to messages, so we eat
68 * the message. See bug 1719.
70 return DBUS_HANDLER_RESULT_HANDLED;
73 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
77 PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
78 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
79 PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
80 PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
81 PROFILE_ATTRIBUTE_FLAG_PATH = 16,
82 PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
83 PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
84 PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128
85 } ProfileAttributeFlags;
88 profile_print_headers (void)
90 printf ("#type\ttimestamp\tserial\tsender\tdestination\tpath\tinterface\tmember\n");
91 printf ("#\t\t\t\t\tin_reply_to\n");
95 profile_print_with_attrs (const char *type, DBusMessage *message,
96 long sec, long usec, ProfileAttributeFlags attrs)
98 printf ("%s\t%ld.%06ld", type, sec, usec);
100 if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
101 printf ("\t%u", dbus_message_get_serial (message));
103 if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
104 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
106 if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
107 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
109 if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
110 printf ("\t%u", dbus_message_get_reply_serial (message));
112 if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
113 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
115 if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
116 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
118 if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
119 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
121 if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
122 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
128 print_message_profile (DBusMessage *message)
130 static dbus_bool_t first = TRUE;
131 long sec = 0, usec = 0;
135 profile_print_headers ();
139 _dbus_get_real_time (&sec, &usec);
141 switch (dbus_message_get_type (message))
143 case DBUS_MESSAGE_TYPE_METHOD_CALL:
144 profile_print_with_attrs ("mc", message, sec, usec,
145 PROFILE_ATTRIBUTE_FLAG_SERIAL |
146 PROFILE_ATTRIBUTE_FLAG_SENDER |
147 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
148 PROFILE_ATTRIBUTE_FLAG_PATH |
149 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
150 PROFILE_ATTRIBUTE_FLAG_MEMBER);
152 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
153 profile_print_with_attrs ("mr", message, sec, usec,
154 PROFILE_ATTRIBUTE_FLAG_SERIAL |
155 PROFILE_ATTRIBUTE_FLAG_SENDER |
156 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
157 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
159 case DBUS_MESSAGE_TYPE_ERROR:
160 profile_print_with_attrs ("err", message, sec, usec,
161 PROFILE_ATTRIBUTE_FLAG_SERIAL |
162 PROFILE_ATTRIBUTE_FLAG_SENDER |
163 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
164 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
166 case DBUS_MESSAGE_TYPE_SIGNAL:
167 profile_print_with_attrs ("sig", message, sec, usec,
168 PROFILE_ATTRIBUTE_FLAG_SERIAL |
169 PROFILE_ATTRIBUTE_FLAG_SENDER |
170 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
171 PROFILE_ATTRIBUTE_FLAG_PATH |
172 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
173 PROFILE_ATTRIBUTE_FLAG_MEMBER);
176 printf ("%s\t%ld.%06ld", "tun", sec, usec);
181 static DBusHandlerResult
182 profile_filter_func (DBusConnection *connection,
183 DBusMessage *message,
186 print_message_profile (message);
188 if (dbus_message_is_signal (message,
189 DBUS_INTERFACE_LOCAL,
193 return DBUS_HANDLER_RESULT_HANDLED;
202 static DBusHandlerResult
203 binary_filter_func (DBusConnection *connection,
204 DBusMessage *message,
207 BinaryMode mode = _DBUS_POINTER_TO_INT (user_data);
211 /* It would be nice if we could do a zero-copy "peek" one day, but libdbus
212 * is so copy-happy that this isn't really a big deal.
214 if (!dbus_message_marshal (message, &blob, &len))
215 tool_oom ("retrieving message");
219 case BINARY_MODE_PCAP:
221 long tv_sec, tv_usec;
222 /* seconds, microseconds, bytes captured (possibly truncated),
224 * http://wiki.wireshark.org/Development/LibpcapFileFormat
226 dbus_uint32_t header[4] = { 0, 0, len, len };
228 /* If this gets padded then we'd need to write it out in pieces */
229 _DBUS_STATIC_ASSERT (sizeof (header) == 16);
231 _dbus_get_real_time (&tv_sec, &tv_usec);
235 if (!tool_write_all (STDOUT_FILENO, header, sizeof (header)))
237 perror ("dbus-monitor: write");
243 case BINARY_MODE_NOT:
244 _dbus_assert_not_reached ("wrong filter function");
247 case BINARY_MODE_RAW:
249 /* nothing special, just the raw message stream */
253 if (!tool_write_all (STDOUT_FILENO, blob, len))
255 perror ("dbus-monitor: write");
261 if (dbus_message_is_signal (message,
262 DBUS_INTERFACE_LOCAL,
266 return DBUS_HANDLER_RESULT_HANDLED;
269 static void usage (char *name, int ecode) _DBUS_GNUC_NORETURN;
272 usage (char *name, int ecode)
274 fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile | --pcap | --binary ] [watch expressions]\n", name);
279 only_one_type (dbus_bool_t *seen_bus_type,
284 fprintf (stderr, "I only support monitoring one bus at a time!\n");
289 *seen_bus_type = TRUE;
294 become_monitor (DBusConnection *connection,
296 const char * const *filters)
298 DBusError error = DBUS_ERROR_INIT;
302 dbus_uint32_t zero = 0;
303 DBusMessageIter appender, array_appender;
305 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
306 DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
309 tool_oom ("becoming a monitor");
311 dbus_message_iter_init_append (m, &appender);
313 if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
315 tool_oom ("opening string array");
317 for (i = 0; i < numFilters; i++)
319 if (!dbus_message_iter_append_basic (&array_appender, DBUS_TYPE_STRING,
321 tool_oom ("adding filter to array");
324 if (!dbus_message_iter_close_container (&appender, &array_appender) ||
325 !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero))
326 tool_oom ("finishing arguments");
328 r = dbus_connection_send_with_reply_and_block (connection, m, -1, &error);
332 dbus_message_unref (r);
334 else if (dbus_error_has_name (&error, DBUS_ERROR_UNKNOWN_INTERFACE))
336 fprintf (stderr, "dbus-monitor: unable to enable new-style monitoring, "
337 "your dbus-daemon is too old. Falling back to eavesdropping.\n");
338 dbus_error_free (&error);
342 fprintf (stderr, "dbus-monitor: unable to enable new-style monitoring: "
343 "%s: \"%s\". Falling back to eavesdropping.\n",
344 error.name, error.message);
345 dbus_error_free (&error);
348 dbus_message_unref (m);
354 main (int argc, char *argv[])
356 DBusConnection *connection;
358 DBusBusType type = DBUS_BUS_SESSION;
359 DBusHandleMessageFunction filter_func = monitor_filter_func;
360 char *address = NULL;
361 dbus_bool_t seen_bus_type = FALSE;
362 BinaryMode binary_mode = BINARY_MODE_NOT;
363 int i = 0, j = 0, numFilters = 0;
364 char **filters = NULL;
366 /* Set stdout to be unbuffered; this is basically so that if people
367 * do dbus-monitor > file, then send SIGINT via Control-C, they
368 * don't lose the last chunk of messages.
372 setvbuf (stdout, NULL, _IONBF, 0);
374 setvbuf (stdout, NULL, _IOLBF, 0);
377 for (i = 1; i < argc; i++)
381 if (!strcmp (arg, "--system"))
383 only_one_type (&seen_bus_type, argv[0]);
384 type = DBUS_BUS_SYSTEM;
386 else if (!strcmp (arg, "--session"))
388 only_one_type (&seen_bus_type, argv[0]);
389 type = DBUS_BUS_SESSION;
391 else if (!strcmp (arg, "--address"))
393 only_one_type (&seen_bus_type, argv[0]);
403 else if (!strcmp (arg, "--help"))
405 else if (!strcmp (arg, "--monitor"))
407 filter_func = monitor_filter_func;
408 binary_mode = BINARY_MODE_NOT;
410 else if (!strcmp (arg, "--profile"))
412 filter_func = profile_filter_func;
413 binary_mode = BINARY_MODE_NOT;
415 else if (!strcmp (arg, "--binary"))
417 filter_func = binary_filter_func;
418 binary_mode = BINARY_MODE_RAW;
420 else if (!strcmp (arg, "--pcap"))
422 filter_func = binary_filter_func;
423 binary_mode = BINARY_MODE_PCAP;
425 else if (!strcmp (arg, "--"))
427 else if (arg[0] == '-')
430 unsigned int filter_len;
432 /* Prepend a rule (and a comma) to enable the monitor to eavesdrop.
433 * Prepending allows the user to add eavesdrop=false at command line
434 * in order to disable eavesdropping when needed */
435 filter_len = strlen (EAVESDROPPING_RULE) + 1 + strlen (arg) + 1;
437 filters = (char **) realloc (filters, numFilters * sizeof (char *));
439 tool_oom ("adding a new filter slot");
440 filters[j] = (char *) malloc (filter_len);
441 if (filters[j] == NULL)
442 tool_oom ("adding a new filter");
443 snprintf (filters[j], filter_len, "%s,%s", EAVESDROPPING_RULE, arg);
448 dbus_error_init (&error);
452 connection = dbus_connection_open (address, &error);
455 if (!dbus_bus_register (connection, &error))
457 fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
458 address, error.message);
459 dbus_error_free (&error);
465 connection = dbus_bus_get (type, &error);
466 if (connection == NULL)
475 case DBUS_BUS_SYSTEM:
476 where = "system bus";
478 case DBUS_BUS_SESSION:
479 where = "session bus";
481 case DBUS_BUS_STARTER:
483 /* We don't set type to anything else */
484 _dbus_assert_not_reached ("impossible bus type");
487 fprintf (stderr, "Failed to open connection to %s: %s\n",
490 dbus_error_free (&error);
494 /* Receive o.fd.Peer messages as normal messages, rather than having
495 * libdbus handle them internally, which is the wrong thing for
497 dbus_connection_set_route_peer_messages (connection, TRUE);
499 if (!dbus_connection_add_filter (connection, filter_func,
500 _DBUS_INT_TO_POINTER (binary_mode), NULL))
502 fprintf (stderr, "Couldn't add filter!\n");
506 if (become_monitor (connection, numFilters,
507 (const char * const *) filters))
509 /* no more preparation needed */
514 for (i = 0; i < j; i++)
516 dbus_bus_add_match (connection, filters[i] + offset, &error);
517 if (dbus_error_is_set (&error) && i == 0 && offset == 0)
519 /* We might be talking to a pre-1.5.6 dbus-daemon
520 * which wouldn't understand eavesdrop=true.
521 * If this works, carry on with offset > 0
522 * on the remaining iterations. */
523 offset = strlen (EAVESDROPPING_RULE) + 1;
524 dbus_error_free (&error);
525 dbus_bus_add_match (connection, filters[i] + offset, &error);
528 if (dbus_error_is_set (&error))
530 fprintf (stderr, "Failed to setup match \"%s\": %s\n",
531 filters[i], error.message);
532 dbus_error_free (&error);
540 dbus_bus_add_match (connection,
543 if (dbus_error_is_set (&error))
545 dbus_error_free (&error);
546 dbus_bus_add_match (connection,
549 if (dbus_error_is_set (&error))
556 case BINARY_MODE_NOT:
557 case BINARY_MODE_RAW:
559 /* no special header needed */
562 case BINARY_MODE_PCAP:
564 /* We're not using libpcap because the file format is simple
565 * enough not to need it.
566 * http://wiki.wireshark.org/Development/LibpcapFileFormat */
569 dbus_uint16_t major_version;
570 dbus_uint16_t minor_version;
571 dbus_int32_t timezone;
572 dbus_uint32_t precision;
573 dbus_uint32_t max_length;
574 dbus_uint32_t link_type;
576 0xA1B2C3D4U, /* magic number */
578 0, /* capture in GMT */
579 0, /* no opinion on timestamp precision */
580 (1 << 27), /* D-Bus spec says so */
584 /* Assert that there is no padding */
585 _DBUS_STATIC_ASSERT (sizeof (header) == 24);
587 if (!tool_write_all (STDOUT_FILENO, &header, sizeof (header)))
589 perror ("dbus-monitor: write");
596 while (dbus_connection_read_write_dispatch(connection, -1))
600 fprintf (stderr, "Error: %s\n", error.message);