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
45 gettimeofday (struct timeval *__p,
49 unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
53 GetSystemTimeAsFileTime (&now.ft);
54 __p->tv_usec = (long) ((now.ns100 / 10LL) % DBUS_USEC_IN_SEC);
55 __p->tv_sec = (long)(((now.ns100 / 10LL) / DBUS_SECONDS_SINCE_1601) - DBUS_SECONDS_SINCE_1601);
61 static DBusHandlerResult
62 monitor_filter_func (DBusConnection *connection,
66 print_message (message, FALSE);
68 if (dbus_message_is_signal (message,
73 /* Conceptually we want this to be
74 * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
75 * some problems. See bug 1719.
77 return DBUS_HANDLER_RESULT_HANDLED;
80 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
81 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
85 PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
86 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
87 PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
88 PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
89 PROFILE_ATTRIBUTE_FLAG_PATH = 16,
90 PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
91 PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
92 PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128
93 } ProfileAttributeFlags;
96 profile_print_with_attrs (const char *type, DBusMessage *message,
97 struct timeval *t, ProfileAttributeFlags attrs)
99 printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
101 if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
102 printf ("\t%u", dbus_message_get_serial (message));
104 if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
105 printf ("\t%u", dbus_message_get_reply_serial (message));
107 if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
108 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
110 if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
111 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
113 if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
114 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
116 if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
117 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
119 if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
120 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
122 if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
123 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
129 print_message_profile (DBusMessage *message)
133 if (gettimeofday (&t, NULL) < 0)
139 switch (dbus_message_get_type (message))
141 case DBUS_MESSAGE_TYPE_METHOD_CALL:
142 profile_print_with_attrs ("mc", message, &t,
143 PROFILE_ATTRIBUTE_FLAG_SERIAL |
144 PROFILE_ATTRIBUTE_FLAG_SENDER |
145 PROFILE_ATTRIBUTE_FLAG_PATH |
146 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
147 PROFILE_ATTRIBUTE_FLAG_MEMBER);
149 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
150 profile_print_with_attrs ("mr", message, &t,
151 PROFILE_ATTRIBUTE_FLAG_SERIAL |
152 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
153 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
155 case DBUS_MESSAGE_TYPE_ERROR:
156 profile_print_with_attrs ("err", message, &t,
157 PROFILE_ATTRIBUTE_FLAG_SERIAL |
158 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
159 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
161 case DBUS_MESSAGE_TYPE_SIGNAL:
162 profile_print_with_attrs ("sig", message, &t,
163 PROFILE_ATTRIBUTE_FLAG_SERIAL |
164 PROFILE_ATTRIBUTE_FLAG_PATH |
165 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
166 PROFILE_ATTRIBUTE_FLAG_MEMBER);
169 printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
174 static DBusHandlerResult
175 profile_filter_func (DBusConnection *connection,
176 DBusMessage *message,
179 print_message_profile (message);
181 if (dbus_message_is_signal (message,
182 DBUS_INTERFACE_LOCAL,
186 return DBUS_HANDLER_RESULT_HANDLED;
190 usage (char *name, int ecode)
192 fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name);
196 static dbus_bool_t sigint_received = FALSE;
199 sigint_handler (int signum)
201 sigint_received = TRUE;
205 main (int argc, char *argv[])
207 DBusConnection *connection;
209 DBusBusType type = DBUS_BUS_SESSION;
210 DBusHandleMessageFunction filter_func = monitor_filter_func;
211 char *address = NULL;
213 int i = 0, j = 0, numFilters = 0;
214 char **filters = NULL;
216 /* Set stdout to be unbuffered; this is basically so that if people
217 * do dbus-monitor > file, then send SIGINT via Control-C, they
218 * don't lose the last chunk of messages.
220 setvbuf (stdout, NULL, _IOLBF, 0);
222 for (i = 1; i < argc; i++)
226 if (!strcmp (arg, "--system"))
227 type = DBUS_BUS_SYSTEM;
228 else if (!strcmp (arg, "--session"))
229 type = DBUS_BUS_SESSION;
230 else if (!strcmp (arg, "--address"))
240 else if (!strcmp (arg, "--help"))
242 else if (!strcmp (arg, "--monitor"))
243 filter_func = monitor_filter_func;
244 else if (!strcmp (arg, "--profile"))
245 filter_func = profile_filter_func;
246 else if (!strcmp (arg, "--"))
248 else if (arg[0] == '-')
252 filters = (char **)realloc(filters, numFilters * sizeof(char *));
253 filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
254 snprintf(filters[j], strlen(arg) + 1, "%s", arg);
259 dbus_error_init (&error);
263 connection = dbus_connection_open (address, &error);
266 if (!dbus_bus_register (connection, &error))
268 fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
269 address, error.message);
270 dbus_error_free (&error);
276 connection = dbus_bus_get (type, &error);
277 if (connection == NULL)
286 case DBUS_BUS_SYSTEM:
287 where = "system bus";
289 case DBUS_BUS_SESSION:
290 where = "session bus";
296 fprintf (stderr, "Failed to open connection to %s: %s\n",
299 dbus_error_free (&error);
305 for (i = 0; i < j; i++)
307 dbus_bus_add_match (connection, filters[i], &error);
308 if (dbus_error_is_set (&error))
310 fprintf (stderr, "Failed to setup match \"%s\": %s\n",
311 filters[i], error.message);
312 dbus_error_free (&error);
320 dbus_bus_add_match (connection,
323 if (dbus_error_is_set (&error))
325 dbus_bus_add_match (connection,
326 "type='method_call'",
328 if (dbus_error_is_set (&error))
330 dbus_bus_add_match (connection,
331 "type='method_return'",
333 if (dbus_error_is_set (&error))
335 dbus_bus_add_match (connection,
338 if (dbus_error_is_set (&error))
342 if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
343 fprintf (stderr, "Couldn't add filter!\n");
347 while (dbus_connection_read_write_dispatch(connection, -1))
351 fprintf (stderr, "Error: %s\n", error.message);