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;
98 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
99 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
103 PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
104 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
105 PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
106 PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
107 PROFILE_ATTRIBUTE_FLAG_PATH = 16,
108 PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
109 PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
110 PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128
111 } ProfileAttributeFlags;
114 profile_print_with_attrs (const char *type, DBusMessage *message,
115 struct timeval *t, ProfileAttributeFlags attrs)
117 printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
119 if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
120 printf ("\t%u", dbus_message_get_serial (message));
122 if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
123 printf ("\t%u", dbus_message_get_reply_serial (message));
125 if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
126 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
128 if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
129 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
131 if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
132 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
134 if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
135 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
137 if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
138 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
140 if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
141 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
147 print_message_profile (DBusMessage *message)
151 if (gettimeofday (&t, NULL) < 0)
157 switch (dbus_message_get_type (message))
159 case DBUS_MESSAGE_TYPE_METHOD_CALL:
160 profile_print_with_attrs ("mc", message, &t,
161 PROFILE_ATTRIBUTE_FLAG_SERIAL |
162 PROFILE_ATTRIBUTE_FLAG_SENDER |
163 PROFILE_ATTRIBUTE_FLAG_PATH |
164 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
165 PROFILE_ATTRIBUTE_FLAG_MEMBER);
167 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
168 profile_print_with_attrs ("mr", message, &t,
169 PROFILE_ATTRIBUTE_FLAG_SERIAL |
170 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
171 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
173 case DBUS_MESSAGE_TYPE_ERROR:
174 profile_print_with_attrs ("err", message, &t,
175 PROFILE_ATTRIBUTE_FLAG_SERIAL |
176 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
177 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
179 case DBUS_MESSAGE_TYPE_SIGNAL:
180 profile_print_with_attrs ("sig", message, &t,
181 PROFILE_ATTRIBUTE_FLAG_SERIAL |
182 PROFILE_ATTRIBUTE_FLAG_PATH |
183 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
184 PROFILE_ATTRIBUTE_FLAG_MEMBER);
187 printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
192 static DBusHandlerResult
193 profile_filter_func (DBusConnection *connection,
194 DBusMessage *message,
197 print_message_profile (message);
199 if (dbus_message_is_signal (message,
200 DBUS_INTERFACE_LOCAL,
204 return DBUS_HANDLER_RESULT_HANDLED;
208 usage (char *name, int ecode)
210 fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name);
214 static dbus_bool_t sigint_received = FALSE;
217 sigint_handler (int signum)
219 sigint_received = TRUE;
223 main (int argc, char *argv[])
225 DBusConnection *connection;
227 DBusBusType type = DBUS_BUS_SESSION;
228 DBusHandleMessageFunction filter_func = monitor_filter_func;
229 char *address = NULL;
231 int i = 0, j = 0, numFilters = 0;
232 char **filters = NULL;
234 /* Set stdout to be unbuffered; this is basically so that if people
235 * do dbus-monitor > file, then send SIGINT via Control-C, they
236 * don't lose the last chunk of messages.
240 setvbuf (stdout, NULL, _IONBF, 0);
242 setvbuf (stdout, NULL, _IOLBF, 0);
245 for (i = 1; i < argc; i++)
249 if (!strcmp (arg, "--system"))
250 type = DBUS_BUS_SYSTEM;
251 else if (!strcmp (arg, "--session"))
252 type = DBUS_BUS_SESSION;
253 else if (!strcmp (arg, "--address"))
263 else if (!strcmp (arg, "--help"))
265 else if (!strcmp (arg, "--monitor"))
266 filter_func = monitor_filter_func;
267 else if (!strcmp (arg, "--profile"))
268 filter_func = profile_filter_func;
269 else if (!strcmp (arg, "--"))
271 else if (arg[0] == '-')
275 filters = (char **)realloc(filters, numFilters * sizeof(char *));
276 filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
277 snprintf(filters[j], strlen(arg) + 1, "%s", arg);
282 dbus_error_init (&error);
286 connection = dbus_connection_open (address, &error);
289 if (!dbus_bus_register (connection, &error))
291 fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
292 address, error.message);
293 dbus_error_free (&error);
299 connection = dbus_bus_get (type, &error);
300 if (connection == NULL)
309 case DBUS_BUS_SYSTEM:
310 where = "system bus";
312 case DBUS_BUS_SESSION:
313 where = "session bus";
319 fprintf (stderr, "Failed to open connection to %s: %s\n",
322 dbus_error_free (&error);
328 for (i = 0; i < j; i++)
330 dbus_bus_add_match (connection, filters[i], &error);
331 if (dbus_error_is_set (&error))
333 fprintf (stderr, "Failed to setup match \"%s\": %s\n",
334 filters[i], error.message);
335 dbus_error_free (&error);
343 dbus_bus_add_match (connection,
346 if (dbus_error_is_set (&error))
348 dbus_bus_add_match (connection,
349 "type='method_call'",
351 if (dbus_error_is_set (&error))
353 dbus_bus_add_match (connection,
354 "type='method_return'",
356 if (dbus_error_is_set (&error))
358 dbus_bus_add_match (connection,
361 if (dbus_error_is_set (&error))
365 if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
366 fprintf (stderr, "Couldn't add filter!\n");
370 while (dbus_connection_read_write_dispatch(connection, -1))
374 fprintf (stderr, "Error: %s\n", error.message);