2007-07-13 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / tools / dbus-monitor.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-monitor.c  Utility program to monitor messages on the bus
3  *
4  * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #ifdef DBUS_WIN
28 #include <winsock2.h>
29 #undef interface
30 #else
31 #include <sys/time.h>
32 #endif
33
34 #include <time.h>
35
36 #include <signal.h>
37
38 #include "dbus-print-message.h"
39
40 #ifdef DBUS_WIN
41
42 /* gettimeofday is not defined on windows */
43 #define DBUS_SECONDS_SINCE_1601 11644473600LL
44 #define DBUS_USEC_IN_SEC        1000000LL
45
46 static int
47 gettimeofday (struct timeval *__p,
48               void *__t)
49 {
50   union {
51       unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
52       FILETIME           ft;
53     } now;
54
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);
58
59   return 0;
60 }
61 #endif
62
63 static DBusHandlerResult
64 monitor_filter_func (DBusConnection     *connection,
65                      DBusMessage        *message,
66                      void               *user_data)
67 {
68   print_message (message, FALSE);
69   
70   if (dbus_message_is_signal (message,
71                               DBUS_INTERFACE_LOCAL,
72                               "Disconnected"))
73     exit (0);
74   
75   /* Conceptually we want this to be
76    * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
77    * some problems.  See bug 1719.
78    */
79   return DBUS_HANDLER_RESULT_HANDLED;
80 }
81
82 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
83 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
84
85 typedef enum
86 {
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;
96
97 static void
98 profile_print_with_attrs (const char *type, DBusMessage *message,
99   struct timeval *t, ProfileAttributeFlags attrs)
100 {
101   printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
102
103   if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
104     printf ("\t%u", dbus_message_get_serial (message));
105
106   if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
107     printf ("\t%u", dbus_message_get_reply_serial (message));
108
109   if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
110     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
111
112   if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
113     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
114
115   if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
116     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
117
118   if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
119     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
120
121   if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
122     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
123
124   if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
125     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
126
127   printf ("\n");
128 }
129
130 static void
131 print_message_profile (DBusMessage *message)
132 {
133   struct timeval t;
134
135   if (gettimeofday (&t, NULL) < 0)
136     {
137       printf ("un\n");
138       return;
139     }
140
141   switch (dbus_message_get_type (message))
142     {
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);
150         break;
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);
156         break;
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);
162         break;
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);
169         break;
170       default:
171         printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
172         break;
173     }
174 }
175
176 static DBusHandlerResult
177 profile_filter_func (DBusConnection     *connection,
178                      DBusMessage        *message,
179                      void               *user_data)
180 {
181   print_message_profile (message);
182
183   if (dbus_message_is_signal (message,
184                               DBUS_INTERFACE_LOCAL,
185                               "Disconnected"))
186     exit (0);
187
188   return DBUS_HANDLER_RESULT_HANDLED;
189 }
190
191 static void
192 usage (char *name, int ecode)
193 {
194   fprintf (stderr, "Usage: %s [--system | --session] [--monitor | --profile ] [watch expressions]\n", name);
195   exit (ecode);
196 }
197
198 dbus_bool_t sigint_received = FALSE;
199
200 static void
201 sigint_handler (int signum)
202 {
203   sigint_received = TRUE;
204 }
205
206 int
207 main (int argc, char *argv[])
208 {
209   DBusConnection *connection;
210   DBusError error;
211   DBusBusType type = DBUS_BUS_SESSION;
212   DBusHandleMessageFunction filter_func = monitor_filter_func;
213
214   int i = 0, j = 0, numFilters = 0;
215   char **filters = NULL;
216   for (i = 1; i < argc; i++)
217     {
218       char *arg = argv[i];
219
220       if (!strcmp (arg, "--system"))
221         type = DBUS_BUS_SYSTEM;
222       else if (!strcmp (arg, "--session"))
223         type = DBUS_BUS_SESSION;
224       else if (!strcmp (arg, "--help"))
225         usage (argv[0], 0);
226       else if (!strcmp (arg, "--monitor"))
227         filter_func = monitor_filter_func;
228       else if (!strcmp (arg, "--profile"))
229         filter_func = profile_filter_func;
230       else if (!strcmp (arg, "--"))
231         continue;
232       else if (arg[0] == '-')
233         usage (argv[0], 1);
234       else {
235         numFilters++;
236        filters = (char **)realloc(filters, numFilters * sizeof(char *));
237         filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
238         snprintf(filters[j], strlen(arg) + 1, "%s", arg);
239         j++;
240       }
241     }
242
243   dbus_error_init (&error);
244   connection = dbus_bus_get (type, &error);
245   if (connection == NULL)
246     {
247       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
248                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
249                error.message);
250       dbus_error_free (&error);
251       exit (1);
252     }
253
254   if (numFilters)
255     {
256       for (i = 0; i < j; i++)
257         {
258           dbus_bus_add_match (connection, filters[i], &error);
259           if (dbus_error_is_set (&error))
260             {
261               fprintf (stderr, "Failed to setup match \"%s\": %s\n",
262                        filters[i], error.message);
263               dbus_error_free (&error);
264               exit (1);
265             }
266           free(filters[i]);
267         }
268     }
269   else
270     {
271       dbus_bus_add_match (connection,
272                           "type='signal'",
273                           &error);
274       if (dbus_error_is_set (&error))
275         goto lose;
276       dbus_bus_add_match (connection,
277                           "type='method_call'",
278                           &error);
279       if (dbus_error_is_set (&error))
280         goto lose;
281       dbus_bus_add_match (connection,
282                           "type='method_return'",
283                           &error);
284       if (dbus_error_is_set (&error))
285         goto lose;
286       dbus_bus_add_match (connection,
287                           "type='error'",
288                           &error);
289       if (dbus_error_is_set (&error))
290         goto lose;
291     }
292
293   if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
294     fprintf (stderr, "Couldn't add filter!\n");
295     exit (1);
296   }
297
298   /* we handle SIGINT so exit() is reached and flushes stdout */
299   signal (SIGINT, sigint_handler);
300   while (dbus_connection_read_write_dispatch(connection, -1)
301           && !sigint_received)
302     ;
303   exit (0);
304  lose:
305   fprintf (stderr, "Error: %s\n", error.message);
306   exit (1);
307 }
308