Bug 2432 - Support --address option for dbus-monitor
[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 <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 | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name);
195   exit (ecode);
196 }
197
198 static 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   char *address = NULL;
214   
215   int i = 0, j = 0, numFilters = 0;
216   char **filters = NULL;
217   for (i = 1; i < argc; i++)
218     {
219       char *arg = argv[i];
220
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"))
226         {
227           if (i+1 < argc)
228             {
229               address = argv[i+1];
230               i++;
231             }
232           else
233             usage (argv[0], 1);
234         }
235       else if (!strcmp (arg, "--help"))
236         usage (argv[0], 0);
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, "--"))
242         continue;
243       else if (arg[0] == '-')
244         usage (argv[0], 1);
245       else {
246         numFilters++;
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);
250         j++;
251       }
252     }
253
254   dbus_error_init (&error);
255   
256   if (address != NULL)
257     {
258       connection = dbus_connection_open (address, &error);
259       if (connection)
260         {
261           if (!dbus_bus_register (connection, &error))
262             {
263               fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
264                        address, error.message);
265               dbus_error_free (&error);
266               exit (1);
267             }
268         }
269     }
270   else
271     connection = dbus_bus_get (type, &error);
272   if (connection == NULL)
273     {
274       const char *where;
275       if (address != NULL)
276         where = address;
277       else
278         {
279           switch (type)
280             {
281             case DBUS_BUS_SYSTEM:
282               where = "system bus";
283               break;
284             case DBUS_BUS_SESSION:
285               where = "session bus";
286               break;
287             default:
288               where = "";
289             }
290         }
291       fprintf (stderr, "Failed to open connection to %s: %s\n",
292                where,
293                error.message);
294       dbus_error_free (&error);
295       exit (1);
296     }
297
298   if (numFilters)
299     {
300       for (i = 0; i < j; i++)
301         {
302           dbus_bus_add_match (connection, filters[i], &error);
303           if (dbus_error_is_set (&error))
304             {
305               fprintf (stderr, "Failed to setup match \"%s\": %s\n",
306                        filters[i], error.message);
307               dbus_error_free (&error);
308               exit (1);
309             }
310           free(filters[i]);
311         }
312     }
313   else
314     {
315       dbus_bus_add_match (connection,
316                           "type='signal'",
317                           &error);
318       if (dbus_error_is_set (&error))
319         goto lose;
320       dbus_bus_add_match (connection,
321                           "type='method_call'",
322                           &error);
323       if (dbus_error_is_set (&error))
324         goto lose;
325       dbus_bus_add_match (connection,
326                           "type='method_return'",
327                           &error);
328       if (dbus_error_is_set (&error))
329         goto lose;
330       dbus_bus_add_match (connection,
331                           "type='error'",
332                           &error);
333       if (dbus_error_is_set (&error))
334         goto lose;
335     }
336
337   if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
338     fprintf (stderr, "Couldn't add filter!\n");
339     exit (1);
340   }
341
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)
345           && !sigint_received)
346     ;
347   exit (0);
348  lose:
349   fprintf (stderr, "Error: %s\n", error.message);
350   exit (1);
351 }
352