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"
40 /* gettimeofday is not defined on windows */
41 #define DBUS_SECONDS_SINCE_1601 11644473600LL
42 #define DBUS_USEC_IN_SEC 1000000LL
51 GetSystemTimeAsFileTime (LPFILETIME ftp)
55 SystemTimeToFileTime (&st, ftp);
60 gettimeofday (struct timeval *__p,
64 unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
68 GetSystemTimeAsFileTime (&now.ft);
69 __p->tv_usec = (long) ((now.ns100 / 10LL) % DBUS_USEC_IN_SEC);
70 __p->tv_sec = (long)(((now.ns100 / 10LL) / DBUS_SECONDS_SINCE_1601) - DBUS_SECONDS_SINCE_1601);
76 static DBusHandlerResult
77 monitor_filter_func (DBusConnection *connection,
81 print_message (message, FALSE);
83 if (dbus_message_is_signal (message,
88 /* Conceptually we want this to be
89 * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
90 * some problems. See bug 1719.
92 return DBUS_HANDLER_RESULT_HANDLED;
95 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
96 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
100 PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
101 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
102 PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
103 PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
104 PROFILE_ATTRIBUTE_FLAG_PATH = 16,
105 PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
106 PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
107 PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128
108 } ProfileAttributeFlags;
111 profile_print_with_attrs (const char *type, DBusMessage *message,
112 struct timeval *t, ProfileAttributeFlags attrs)
114 printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
116 if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
117 printf ("\t%u", dbus_message_get_serial (message));
119 if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
120 printf ("\t%u", dbus_message_get_reply_serial (message));
122 if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
123 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
125 if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
126 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
128 if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
129 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
131 if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
132 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
134 if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
135 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
137 if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
138 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
144 print_message_profile (DBusMessage *message)
148 if (gettimeofday (&t, NULL) < 0)
154 switch (dbus_message_get_type (message))
156 case DBUS_MESSAGE_TYPE_METHOD_CALL:
157 profile_print_with_attrs ("mc", message, &t,
158 PROFILE_ATTRIBUTE_FLAG_SERIAL |
159 PROFILE_ATTRIBUTE_FLAG_SENDER |
160 PROFILE_ATTRIBUTE_FLAG_PATH |
161 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
162 PROFILE_ATTRIBUTE_FLAG_MEMBER);
164 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
165 profile_print_with_attrs ("mr", message, &t,
166 PROFILE_ATTRIBUTE_FLAG_SERIAL |
167 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
168 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
170 case DBUS_MESSAGE_TYPE_ERROR:
171 profile_print_with_attrs ("err", message, &t,
172 PROFILE_ATTRIBUTE_FLAG_SERIAL |
173 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
174 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
176 case DBUS_MESSAGE_TYPE_SIGNAL:
177 profile_print_with_attrs ("sig", message, &t,
178 PROFILE_ATTRIBUTE_FLAG_SERIAL |
179 PROFILE_ATTRIBUTE_FLAG_PATH |
180 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
181 PROFILE_ATTRIBUTE_FLAG_MEMBER);
184 printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
189 static DBusHandlerResult
190 profile_filter_func (DBusConnection *connection,
191 DBusMessage *message,
194 print_message_profile (message);
196 if (dbus_message_is_signal (message,
197 DBUS_INTERFACE_LOCAL,
201 return DBUS_HANDLER_RESULT_HANDLED;
205 usage (char *name, int ecode)
207 fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name);
211 static dbus_bool_t sigint_received = FALSE;
214 sigint_handler (int signum)
216 sigint_received = TRUE;
220 main (int argc, char *argv[])
222 DBusConnection *connection;
224 DBusBusType type = DBUS_BUS_SESSION;
225 DBusHandleMessageFunction filter_func = monitor_filter_func;
226 char *address = NULL;
228 int i = 0, j = 0, numFilters = 0;
229 char **filters = NULL;
231 /* Set stdout to be unbuffered; this is basically so that if people
232 * do dbus-monitor > file, then send SIGINT via Control-C, they
233 * don't lose the last chunk of messages.
237 setvbuf (stdout, NULL, _IONBF, 0);
239 setvbuf (stdout, NULL, _IOLBF, 0);
242 for (i = 1; i < argc; i++)
246 if (!strcmp (arg, "--system"))
247 type = DBUS_BUS_SYSTEM;
248 else if (!strcmp (arg, "--session"))
249 type = DBUS_BUS_SESSION;
250 else if (!strcmp (arg, "--address"))
260 else if (!strcmp (arg, "--help"))
262 else if (!strcmp (arg, "--monitor"))
263 filter_func = monitor_filter_func;
264 else if (!strcmp (arg, "--profile"))
265 filter_func = profile_filter_func;
266 else if (!strcmp (arg, "--"))
268 else if (arg[0] == '-')
272 filters = (char **)realloc(filters, numFilters * sizeof(char *));
273 filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
274 snprintf(filters[j], strlen(arg) + 1, "%s", arg);
279 dbus_error_init (&error);
283 connection = dbus_connection_open (address, &error);
286 if (!dbus_bus_register (connection, &error))
288 fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
289 address, error.message);
290 dbus_error_free (&error);
296 connection = dbus_bus_get (type, &error);
297 if (connection == NULL)
306 case DBUS_BUS_SYSTEM:
307 where = "system bus";
309 case DBUS_BUS_SESSION:
310 where = "session bus";
316 fprintf (stderr, "Failed to open connection to %s: %s\n",
319 dbus_error_free (&error);
325 for (i = 0; i < j; i++)
327 dbus_bus_add_match (connection, filters[i], &error);
328 if (dbus_error_is_set (&error))
330 fprintf (stderr, "Failed to setup match \"%s\": %s\n",
331 filters[i], error.message);
332 dbus_error_free (&error);
340 dbus_bus_add_match (connection,
343 if (dbus_error_is_set (&error))
345 dbus_bus_add_match (connection,
346 "type='method_call'",
348 if (dbus_error_is_set (&error))
350 dbus_bus_add_match (connection,
351 "type='method_return'",
353 if (dbus_error_is_set (&error))
355 dbus_bus_add_match (connection,
358 if (dbus_error_is_set (&error))
362 if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
363 fprintf (stderr, "Couldn't add filter!\n");
367 while (dbus_connection_read_write_dispatch(connection, -1))
371 fprintf (stderr, "Error: %s\n", error.message);