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.
235 setvbuf (stdout, NULL, _IOLBF, 0);
237 for (i = 1; i < argc; i++)
241 if (!strcmp (arg, "--system"))
242 type = DBUS_BUS_SYSTEM;
243 else if (!strcmp (arg, "--session"))
244 type = DBUS_BUS_SESSION;
245 else if (!strcmp (arg, "--address"))
255 else if (!strcmp (arg, "--help"))
257 else if (!strcmp (arg, "--monitor"))
258 filter_func = monitor_filter_func;
259 else if (!strcmp (arg, "--profile"))
260 filter_func = profile_filter_func;
261 else if (!strcmp (arg, "--"))
263 else if (arg[0] == '-')
267 filters = (char **)realloc(filters, numFilters * sizeof(char *));
268 filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
269 snprintf(filters[j], strlen(arg) + 1, "%s", arg);
274 dbus_error_init (&error);
278 connection = dbus_connection_open (address, &error);
281 if (!dbus_bus_register (connection, &error))
283 fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
284 address, error.message);
285 dbus_error_free (&error);
291 connection = dbus_bus_get (type, &error);
292 if (connection == NULL)
301 case DBUS_BUS_SYSTEM:
302 where = "system bus";
304 case DBUS_BUS_SESSION:
305 where = "session bus";
311 fprintf (stderr, "Failed to open connection to %s: %s\n",
314 dbus_error_free (&error);
320 for (i = 0; i < j; i++)
322 dbus_bus_add_match (connection, filters[i], &error);
323 if (dbus_error_is_set (&error))
325 fprintf (stderr, "Failed to setup match \"%s\": %s\n",
326 filters[i], error.message);
327 dbus_error_free (&error);
335 dbus_bus_add_match (connection,
338 if (dbus_error_is_set (&error))
340 dbus_bus_add_match (connection,
341 "type='method_call'",
343 if (dbus_error_is_set (&error))
345 dbus_bus_add_match (connection,
346 "type='method_return'",
348 if (dbus_error_is_set (&error))
350 dbus_bus_add_match (connection,
353 if (dbus_error_is_set (&error))
357 if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
358 fprintf (stderr, "Couldn't add filter!\n");
362 while (dbus_connection_read_write_dispatch(connection, -1))
366 fprintf (stderr, "Error: %s\n", error.message);