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
54 GetSystemTimeAsFileTime (LPFILETIME ftp)
58 SystemTimeToFileTime (&st, ftp);
63 gettimeofday (struct timeval *__p,
67 unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
71 GetSystemTimeAsFileTime (&now.ft);
72 __p->tv_usec = (long) ((now.ns100 / 10LL) % DBUS_USEC_IN_SEC);
73 __p->tv_sec = (long)(((now.ns100 / 10LL) / DBUS_SECONDS_SINCE_1601) - DBUS_SECONDS_SINCE_1601);
79 static DBusHandlerResult
80 monitor_filter_func (DBusConnection *connection,
84 print_message (message, FALSE);
86 if (dbus_message_is_signal (message,
91 /* Conceptually we want this to be
92 * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
93 * some problems. See bug 1719.
95 return DBUS_HANDLER_RESULT_HANDLED;
99 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%d"
101 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
103 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
107 PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
108 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
109 PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
110 PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
111 PROFILE_ATTRIBUTE_FLAG_PATH = 16,
112 PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
113 PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
114 PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128
115 } ProfileAttributeFlags;
118 profile_print_with_attrs (const char *type, DBusMessage *message,
119 struct timeval *t, ProfileAttributeFlags attrs)
121 printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
123 if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
124 printf ("\t%u", dbus_message_get_serial (message));
126 if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
127 printf ("\t%u", dbus_message_get_reply_serial (message));
129 if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
130 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
132 if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
133 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
135 if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
136 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
138 if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
139 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
141 if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
142 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
144 if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
145 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
151 print_message_profile (DBusMessage *message)
155 if (gettimeofday (&t, NULL) < 0)
161 switch (dbus_message_get_type (message))
163 case DBUS_MESSAGE_TYPE_METHOD_CALL:
164 profile_print_with_attrs ("mc", message, &t,
165 PROFILE_ATTRIBUTE_FLAG_SERIAL |
166 PROFILE_ATTRIBUTE_FLAG_SENDER |
167 PROFILE_ATTRIBUTE_FLAG_PATH |
168 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
169 PROFILE_ATTRIBUTE_FLAG_MEMBER);
171 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
172 profile_print_with_attrs ("mr", message, &t,
173 PROFILE_ATTRIBUTE_FLAG_SERIAL |
174 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
175 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
177 case DBUS_MESSAGE_TYPE_ERROR:
178 profile_print_with_attrs ("err", message, &t,
179 PROFILE_ATTRIBUTE_FLAG_SERIAL |
180 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
181 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
183 case DBUS_MESSAGE_TYPE_SIGNAL:
184 profile_print_with_attrs ("sig", message, &t,
185 PROFILE_ATTRIBUTE_FLAG_SERIAL |
186 PROFILE_ATTRIBUTE_FLAG_PATH |
187 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
188 PROFILE_ATTRIBUTE_FLAG_MEMBER);
191 printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
196 static DBusHandlerResult
197 profile_filter_func (DBusConnection *connection,
198 DBusMessage *message,
201 print_message_profile (message);
203 if (dbus_message_is_signal (message,
204 DBUS_INTERFACE_LOCAL,
208 return DBUS_HANDLER_RESULT_HANDLED;
212 usage (char *name, int ecode)
214 fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name);
219 only_one_type (dbus_bool_t *seen_bus_type,
224 fprintf (stderr, "I only support monitoring one bus at a time!\n");
229 *seen_bus_type = TRUE;
233 static dbus_bool_t sigint_received = FALSE;
236 sigint_handler (int signum)
238 sigint_received = TRUE;
242 main (int argc, char *argv[])
244 DBusConnection *connection;
246 DBusBusType type = DBUS_BUS_SESSION;
247 DBusHandleMessageFunction filter_func = monitor_filter_func;
248 char *address = NULL;
249 dbus_bool_t seen_bus_type = FALSE;
251 int i = 0, j = 0, numFilters = 0;
252 char **filters = NULL;
254 /* Set stdout to be unbuffered; this is basically so that if people
255 * do dbus-monitor > file, then send SIGINT via Control-C, they
256 * don't lose the last chunk of messages.
260 setvbuf (stdout, NULL, _IONBF, 0);
262 setvbuf (stdout, NULL, _IOLBF, 0);
265 for (i = 1; i < argc; i++)
269 if (!strcmp (arg, "--system"))
271 only_one_type (&seen_bus_type, argv[0]);
272 type = DBUS_BUS_SYSTEM;
274 else if (!strcmp (arg, "--session"))
276 only_one_type (&seen_bus_type, argv[0]);
277 type = DBUS_BUS_SESSION;
279 else if (!strcmp (arg, "--address"))
281 only_one_type (&seen_bus_type, argv[0]);
291 else if (!strcmp (arg, "--help"))
293 else if (!strcmp (arg, "--monitor"))
294 filter_func = monitor_filter_func;
295 else if (!strcmp (arg, "--profile"))
296 filter_func = profile_filter_func;
297 else if (!strcmp (arg, "--"))
299 else if (arg[0] == '-')
303 filters = (char **)realloc(filters, numFilters * sizeof(char *));
304 filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
305 snprintf(filters[j], strlen(arg) + 1, "%s", arg);
310 dbus_error_init (&error);
314 connection = dbus_connection_open (address, &error);
317 if (!dbus_bus_register (connection, &error))
319 fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
320 address, error.message);
321 dbus_error_free (&error);
327 connection = dbus_bus_get (type, &error);
328 if (connection == NULL)
337 case DBUS_BUS_SYSTEM:
338 where = "system bus";
340 case DBUS_BUS_SESSION:
341 where = "session bus";
347 fprintf (stderr, "Failed to open connection to %s: %s\n",
350 dbus_error_free (&error);
356 for (i = 0; i < j; i++)
358 dbus_bus_add_match (connection, filters[i], &error);
359 if (dbus_error_is_set (&error))
361 fprintf (stderr, "Failed to setup match \"%s\": %s\n",
362 filters[i], error.message);
363 dbus_error_free (&error);
371 dbus_bus_add_match (connection,
374 if (dbus_error_is_set (&error))
376 dbus_bus_add_match (connection,
377 "type='method_call'",
379 if (dbus_error_is_set (&error))
381 dbus_bus_add_match (connection,
382 "type='method_return'",
384 if (dbus_error_is_set (&error))
386 dbus_bus_add_match (connection,
389 if (dbus_error_is_set (&error))
393 if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
394 fprintf (stderr, "Couldn't add filter!\n");
398 while (dbus_connection_read_write_dispatch(connection, -1))
402 fprintf (stderr, "Error: %s\n", error.message);