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
38 #include "dbus-print-message.h"
42 /* gettimeofday is not defined on windows */
43 #define DBUS_SECONDS_SINCE_1601 11644473600LL
44 #define DBUS_USEC_IN_SEC 1000000LL
47 gettimeofday (struct timeval *__p,
51 unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
55 GetSystemTimeAsFileTime (&now.ft);
56 __p->tv_usec = (long) ((now.ns100 / 10LL) % DBUS_USEC_IN_SEC);
57 __p->tv_sec = (long)(((now.ns100 / 10LL) / DBUS_SECONDS_SINCE_1601) - DBUS_SECONDS_SINCE_1601);
63 static DBusHandlerResult
64 monitor_filter_func (DBusConnection *connection,
68 print_message (message, FALSE);
70 if (dbus_message_is_signal (message,
75 /* Conceptually we want this to be
76 * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
77 * some problems. See bug 1719.
79 return DBUS_HANDLER_RESULT_HANDLED;
82 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
83 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
87 PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
88 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
89 PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
90 PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
91 PROFILE_ATTRIBUTE_FLAG_PATH = 16,
92 PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
93 PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
94 PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128
95 } ProfileAttributeFlags;
98 profile_print_with_attrs (const char *type, DBusMessage *message,
99 struct timeval *t, ProfileAttributeFlags attrs)
101 printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
103 if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
104 printf ("\t%u", dbus_message_get_serial (message));
106 if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
107 printf ("\t%u", dbus_message_get_reply_serial (message));
109 if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
110 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
112 if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
113 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
115 if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
116 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
118 if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
119 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
121 if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
122 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
124 if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
125 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
131 print_message_profile (DBusMessage *message)
135 if (gettimeofday (&t, NULL) < 0)
141 switch (dbus_message_get_type (message))
143 case DBUS_MESSAGE_TYPE_METHOD_CALL:
144 profile_print_with_attrs ("mc", message, &t,
145 PROFILE_ATTRIBUTE_FLAG_SERIAL |
146 PROFILE_ATTRIBUTE_FLAG_SENDER |
147 PROFILE_ATTRIBUTE_FLAG_PATH |
148 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
149 PROFILE_ATTRIBUTE_FLAG_MEMBER);
151 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
152 profile_print_with_attrs ("mr", message, &t,
153 PROFILE_ATTRIBUTE_FLAG_SERIAL |
154 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
155 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
157 case DBUS_MESSAGE_TYPE_ERROR:
158 profile_print_with_attrs ("err", message, &t,
159 PROFILE_ATTRIBUTE_FLAG_SERIAL |
160 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
161 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
163 case DBUS_MESSAGE_TYPE_SIGNAL:
164 profile_print_with_attrs ("sig", message, &t,
165 PROFILE_ATTRIBUTE_FLAG_SERIAL |
166 PROFILE_ATTRIBUTE_FLAG_PATH |
167 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
168 PROFILE_ATTRIBUTE_FLAG_MEMBER);
171 printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
176 static DBusHandlerResult
177 profile_filter_func (DBusConnection *connection,
178 DBusMessage *message,
181 print_message_profile (message);
183 if (dbus_message_is_signal (message,
184 DBUS_INTERFACE_LOCAL,
188 return DBUS_HANDLER_RESULT_HANDLED;
192 usage (char *name, int ecode)
194 fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name);
198 static dbus_bool_t sigint_received = FALSE;
201 sigint_handler (int signum)
203 sigint_received = TRUE;
207 main (int argc, char *argv[])
209 DBusConnection *connection;
211 DBusBusType type = DBUS_BUS_SESSION;
212 DBusHandleMessageFunction filter_func = monitor_filter_func;
213 char *address = NULL;
215 int i = 0, j = 0, numFilters = 0;
216 char **filters = NULL;
217 for (i = 1; i < argc; i++)
221 if (!strcmp (arg, "--system"))
222 type = DBUS_BUS_SYSTEM;
223 else if (!strcmp (arg, "--session"))
224 type = DBUS_BUS_SESSION;
225 else if (!strcmp (arg, "--address"))
235 else if (!strcmp (arg, "--help"))
237 else if (!strcmp (arg, "--monitor"))
238 filter_func = monitor_filter_func;
239 else if (!strcmp (arg, "--profile"))
240 filter_func = profile_filter_func;
241 else if (!strcmp (arg, "--"))
243 else if (arg[0] == '-')
247 filters = (char **)realloc(filters, numFilters * sizeof(char *));
248 filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
249 snprintf(filters[j], strlen(arg) + 1, "%s", arg);
254 dbus_error_init (&error);
258 connection = dbus_connection_open (address, &error);
261 if (!dbus_bus_register (connection, &error))
263 fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
264 address, error.message);
265 dbus_error_free (&error);
271 connection = dbus_bus_get (type, &error);
272 if (connection == NULL)
281 case DBUS_BUS_SYSTEM:
282 where = "system bus";
284 case DBUS_BUS_SESSION:
285 where = "session bus";
291 fprintf (stderr, "Failed to open connection to %s: %s\n",
294 dbus_error_free (&error);
300 for (i = 0; i < j; i++)
302 dbus_bus_add_match (connection, filters[i], &error);
303 if (dbus_error_is_set (&error))
305 fprintf (stderr, "Failed to setup match \"%s\": %s\n",
306 filters[i], error.message);
307 dbus_error_free (&error);
315 dbus_bus_add_match (connection,
318 if (dbus_error_is_set (&error))
320 dbus_bus_add_match (connection,
321 "type='method_call'",
323 if (dbus_error_is_set (&error))
325 dbus_bus_add_match (connection,
326 "type='method_return'",
328 if (dbus_error_is_set (&error))
330 dbus_bus_add_match (connection,
333 if (dbus_error_is_set (&error))
337 if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
338 fprintf (stderr, "Couldn't add filter!\n");
342 /* we handle SIGINT so exit() is reached and flushes stdout */
343 signal (SIGINT, sigint_handler);
344 while (dbus_connection_read_write_dispatch(connection, -1)
349 fprintf (stderr, "Error: %s\n", error.message);