Merge branch 'my-dbus-1.2'
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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 "dbus-print-message.h"
37
38 #ifdef DBUS_WIN
39
40 /* gettimeofday is not defined on windows */
41 #define DBUS_SECONDS_SINCE_1601 11644473600LL
42 #define DBUS_USEC_IN_SEC        1000000LL
43
44 static int
45 gettimeofday (struct timeval *__p,
46               void *__t)
47 {
48   union {
49       unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
50       FILETIME           ft;
51     } now;
52
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);
56
57   return 0;
58 }
59 #endif
60
61 static DBusHandlerResult
62 monitor_filter_func (DBusConnection     *connection,
63                      DBusMessage        *message,
64                      void               *user_data)
65 {
66   print_message (message, FALSE);
67   
68   if (dbus_message_is_signal (message,
69                               DBUS_INTERFACE_LOCAL,
70                               "Disconnected"))
71     exit (0);
72   
73   /* Conceptually we want this to be
74    * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
75    * some problems.  See bug 1719.
76    */
77   return DBUS_HANDLER_RESULT_HANDLED;
78 }
79
80 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
81 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
82
83 typedef enum
84 {
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;
94
95 static void
96 profile_print_with_attrs (const char *type, DBusMessage *message,
97   struct timeval *t, ProfileAttributeFlags attrs)
98 {
99   printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
100
101   if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
102     printf ("\t%u", dbus_message_get_serial (message));
103
104   if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
105     printf ("\t%u", dbus_message_get_reply_serial (message));
106
107   if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
108     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
109
110   if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
111     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
112
113   if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
114     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
115
116   if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
117     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
118
119   if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
120     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
121
122   if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
123     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
124
125   printf ("\n");
126 }
127
128 static void
129 print_message_profile (DBusMessage *message)
130 {
131   struct timeval t;
132
133   if (gettimeofday (&t, NULL) < 0)
134     {
135       printf ("un\n");
136       return;
137     }
138
139   switch (dbus_message_get_type (message))
140     {
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);
148         break;
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);
154         break;
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);
160         break;
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);
167         break;
168       default:
169         printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
170         break;
171     }
172 }
173
174 static DBusHandlerResult
175 profile_filter_func (DBusConnection     *connection,
176                      DBusMessage        *message,
177                      void               *user_data)
178 {
179   print_message_profile (message);
180
181   if (dbus_message_is_signal (message,
182                               DBUS_INTERFACE_LOCAL,
183                               "Disconnected"))
184     exit (0);
185
186   return DBUS_HANDLER_RESULT_HANDLED;
187 }
188
189 static void
190 usage (char *name, int ecode)
191 {
192   fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name);
193   exit (ecode);
194 }
195
196 static dbus_bool_t sigint_received = FALSE;
197
198 static void
199 sigint_handler (int signum)
200 {
201   sigint_received = TRUE;
202 }
203
204 int
205 main (int argc, char *argv[])
206 {
207   DBusConnection *connection;
208   DBusError error;
209   DBusBusType type = DBUS_BUS_SESSION;
210   DBusHandleMessageFunction filter_func = monitor_filter_func;
211   char *address = NULL;
212   
213   int i = 0, j = 0, numFilters = 0;
214   char **filters = NULL;
215
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.
219    */
220   setvbuf (stdout, NULL, _IOLBF, 0);
221
222   for (i = 1; i < argc; i++)
223     {
224       char *arg = argv[i];
225
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"))
231         {
232           if (i+1 < argc)
233             {
234               address = argv[i+1];
235               i++;
236             }
237           else
238             usage (argv[0], 1);
239         }
240       else if (!strcmp (arg, "--help"))
241         usage (argv[0], 0);
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, "--"))
247         continue;
248       else if (arg[0] == '-')
249         usage (argv[0], 1);
250       else {
251         numFilters++;
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);
255         j++;
256       }
257     }
258
259   dbus_error_init (&error);
260   
261   if (address != NULL)
262     {
263       connection = dbus_connection_open (address, &error);
264       if (connection)
265         {
266           if (!dbus_bus_register (connection, &error))
267             {
268               fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
269                        address, error.message);
270               dbus_error_free (&error);
271               exit (1);
272             }
273         }
274     }
275   else
276     connection = dbus_bus_get (type, &error);
277   if (connection == NULL)
278     {
279       const char *where;
280       if (address != NULL)
281         where = address;
282       else
283         {
284           switch (type)
285             {
286             case DBUS_BUS_SYSTEM:
287               where = "system bus";
288               break;
289             case DBUS_BUS_SESSION:
290               where = "session bus";
291               break;
292             default:
293               where = "";
294             }
295         }
296       fprintf (stderr, "Failed to open connection to %s: %s\n",
297                where,
298                error.message);
299       dbus_error_free (&error);
300       exit (1);
301     }
302
303   if (numFilters)
304     {
305       for (i = 0; i < j; i++)
306         {
307           dbus_bus_add_match (connection, filters[i], &error);
308           if (dbus_error_is_set (&error))
309             {
310               fprintf (stderr, "Failed to setup match \"%s\": %s\n",
311                        filters[i], error.message);
312               dbus_error_free (&error);
313               exit (1);
314             }
315           free(filters[i]);
316         }
317     }
318   else
319     {
320       dbus_bus_add_match (connection,
321                           "type='signal'",
322                           &error);
323       if (dbus_error_is_set (&error))
324         goto lose;
325       dbus_bus_add_match (connection,
326                           "type='method_call'",
327                           &error);
328       if (dbus_error_is_set (&error))
329         goto lose;
330       dbus_bus_add_match (connection,
331                           "type='method_return'",
332                           &error);
333       if (dbus_error_is_set (&error))
334         goto lose;
335       dbus_bus_add_match (connection,
336                           "type='error'",
337                           &error);
338       if (dbus_error_is_set (&error))
339         goto lose;
340     }
341
342   if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
343     fprintf (stderr, "Couldn't add filter!\n");
344     exit (1);
345   }
346
347   while (dbus_connection_read_write_dispatch(connection, -1))
348     ;
349   exit (0);
350  lose:
351   fprintf (stderr, "Error: %s\n", error.message);
352   exit (1);
353 }
354