1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "dbus-print-message.h"
31 static DBusHandlerResult
32 monitor_filter_func (DBusConnection *connection,
36 print_message (message, FALSE);
38 if (dbus_message_is_signal (message,
43 /* Conceptually we want this to be
44 * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
45 * some problems. See bug 1719.
47 return DBUS_HANDLER_RESULT_HANDLED;
50 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
51 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
55 PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
56 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
57 PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
58 PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
59 PROFILE_ATTRIBUTE_FLAG_PATH = 16,
60 PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
61 PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
62 PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128,
63 } ProfileAttributeFlags;
66 profile_print_with_attrs (const char *type, DBusMessage *message,
67 struct timeval *t, ProfileAttributeFlags attrs)
69 printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
71 if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
72 printf ("\t%u", dbus_message_get_serial (message));
74 if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
75 printf ("\t%u", dbus_message_get_reply_serial (message));
77 if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
78 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
80 if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
81 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
83 if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
84 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
86 if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
87 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
89 if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
90 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
92 if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
93 printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
99 print_message_profile (DBusMessage *message)
103 if (gettimeofday (&t, NULL) < 0)
109 switch (dbus_message_get_type (message))
111 case DBUS_MESSAGE_TYPE_METHOD_CALL:
112 profile_print_with_attrs ("mc", message, &t,
113 PROFILE_ATTRIBUTE_FLAG_SERIAL |
114 PROFILE_ATTRIBUTE_FLAG_SENDER |
115 PROFILE_ATTRIBUTE_FLAG_PATH |
116 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
117 PROFILE_ATTRIBUTE_FLAG_MEMBER);
119 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
120 profile_print_with_attrs ("mr", message, &t,
121 PROFILE_ATTRIBUTE_FLAG_SERIAL |
122 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
123 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
125 case DBUS_MESSAGE_TYPE_ERROR:
126 profile_print_with_attrs ("err", message, &t,
127 PROFILE_ATTRIBUTE_FLAG_SERIAL |
128 PROFILE_ATTRIBUTE_FLAG_DESTINATION |
129 PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
131 case DBUS_MESSAGE_TYPE_SIGNAL:
132 profile_print_with_attrs ("sig", message, &t,
133 PROFILE_ATTRIBUTE_FLAG_SERIAL |
134 PROFILE_ATTRIBUTE_FLAG_PATH |
135 PROFILE_ATTRIBUTE_FLAG_INTERFACE |
136 PROFILE_ATTRIBUTE_FLAG_MEMBER);
139 printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
144 static DBusHandlerResult
145 profile_filter_func (DBusConnection *connection,
146 DBusMessage *message,
149 print_message_profile (message);
151 if (dbus_message_is_signal (message,
152 DBUS_INTERFACE_LOCAL,
156 return DBUS_HANDLER_RESULT_HANDLED;
160 usage (char *name, int ecode)
162 fprintf (stderr, "Usage: %s [--system | --session] [--monitor | --profile ] [watch expressions]\n", name);
167 main (int argc, char *argv[])
169 DBusConnection *connection;
171 DBusBusType type = DBUS_BUS_SESSION;
172 DBusHandleMessageFunction filter_func = monitor_filter_func;
174 int i = 0, j = 0, numFilters = 0;
175 char **filters = NULL;
176 for (i = 1; i < argc; i++)
180 if (!strcmp (arg, "--system"))
181 type = DBUS_BUS_SYSTEM;
182 else if (!strcmp (arg, "--session"))
183 type = DBUS_BUS_SESSION;
184 else if (!strcmp (arg, "--help"))
186 else if (!strcmp (arg, "--monitor"))
187 filter_func = monitor_filter_func;
188 else if (!strcmp (arg, "--profile"))
189 filter_func = profile_filter_func;
190 else if (!strcmp (arg, "--"))
192 else if (arg[0] == '-')
196 filters = (char **)realloc(filters, numFilters * sizeof(char *));
197 filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
198 snprintf(filters[j], strlen(arg) + 1, "%s", arg);
203 dbus_error_init (&error);
204 connection = dbus_bus_get (type, &error);
205 if (connection == NULL)
207 fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
208 (type == DBUS_BUS_SYSTEM) ? "system" : "session",
210 dbus_error_free (&error);
216 for (i = 0; i < j; i++)
218 dbus_bus_add_match (connection, filters[i], &error);
219 if (dbus_error_is_set (&error))
221 fprintf (stderr, "Failed to setup match \"%s\": %s\n",
222 filters[i], error.message);
223 dbus_error_free (&error);
231 dbus_bus_add_match (connection,
234 if (dbus_error_is_set (&error))
236 dbus_bus_add_match (connection,
237 "type='method_call'",
239 if (dbus_error_is_set (&error))
241 dbus_bus_add_match (connection,
242 "type='method_return'",
244 if (dbus_error_is_set (&error))
246 dbus_bus_add_match (connection,
249 if (dbus_error_is_set (&error))
253 if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
254 fprintf (stderr, "Couldn't add filter!\n");
257 while (dbus_connection_read_write_dispatch(connection, -1))
261 fprintf (stderr, "Error: %s\n", error.message);