Merge remote-tracking branch 'wjt/arg0namespace' into arg0namespace-24317
[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 #ifdef DBUS_WINCE
45
46 #ifndef _IOLBF
47 #define _IOLBF 0x40
48 #endif
49 #ifndef _IONBF
50 #define _IONBF 0x04
51 #endif
52
53 void
54 GetSystemTimeAsFileTime (LPFILETIME ftp)
55 {
56   SYSTEMTIME st;
57   GetSystemTime (&st);
58   SystemTimeToFileTime (&st, ftp);
59 }
60 #endif
61
62 static int
63 gettimeofday (struct timeval *__p,
64               void *__t)
65 {
66   union {
67       unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
68       FILETIME           ft;
69     } now;
70
71   GetSystemTimeAsFileTime (&now.ft);
72   __p->tv_usec = (long) ((now.ns100 / 10LL) % DBUS_USEC_IN_SEC);
73   __p->tv_sec  = (long)(((now.ns100 / 10LL) / DBUS_SECONDS_SINCE_1601) - DBUS_SECONDS_SINCE_1601);
74
75   return 0;
76 }
77 #endif
78
79 static DBusHandlerResult
80 monitor_filter_func (DBusConnection     *connection,
81                      DBusMessage        *message,
82                      void               *user_data)
83 {
84   print_message (message, FALSE);
85   
86   if (dbus_message_is_signal (message,
87                               DBUS_INTERFACE_LOCAL,
88                               "Disconnected"))
89     exit (0);
90   
91   /* Conceptually we want this to be
92    * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
93    * some problems.  See bug 1719.
94    */
95   return DBUS_HANDLER_RESULT_HANDLED;
96 }
97
98 #ifdef __APPLE__
99 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%d"
100 #else
101 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
102 #endif
103 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
104
105 typedef enum
106 {
107   PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
108   PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
109   PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
110   PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
111   PROFILE_ATTRIBUTE_FLAG_PATH = 16,
112   PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
113   PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
114   PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128
115 } ProfileAttributeFlags;
116
117 static void
118 profile_print_with_attrs (const char *type, DBusMessage *message,
119   struct timeval *t, ProfileAttributeFlags attrs)
120 {
121   printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
122
123   if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
124     printf ("\t%u", dbus_message_get_serial (message));
125
126   if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
127     printf ("\t%u", dbus_message_get_reply_serial (message));
128
129   if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
130     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
131
132   if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
133     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
134
135   if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
136     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
137
138   if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
139     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
140
141   if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
142     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
143
144   if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
145     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
146
147   printf ("\n");
148 }
149
150 static void
151 print_message_profile (DBusMessage *message)
152 {
153   struct timeval t;
154
155   if (gettimeofday (&t, NULL) < 0)
156     {
157       printf ("un\n");
158       return;
159     }
160
161   switch (dbus_message_get_type (message))
162     {
163       case DBUS_MESSAGE_TYPE_METHOD_CALL:
164         profile_print_with_attrs ("mc", message, &t,
165           PROFILE_ATTRIBUTE_FLAG_SERIAL |
166           PROFILE_ATTRIBUTE_FLAG_SENDER |
167           PROFILE_ATTRIBUTE_FLAG_PATH |
168           PROFILE_ATTRIBUTE_FLAG_INTERFACE |
169           PROFILE_ATTRIBUTE_FLAG_MEMBER);
170         break;
171       case DBUS_MESSAGE_TYPE_METHOD_RETURN:
172         profile_print_with_attrs ("mr", message, &t,
173           PROFILE_ATTRIBUTE_FLAG_SERIAL |
174           PROFILE_ATTRIBUTE_FLAG_DESTINATION |
175           PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
176         break;
177       case DBUS_MESSAGE_TYPE_ERROR:
178         profile_print_with_attrs ("err", message, &t,
179           PROFILE_ATTRIBUTE_FLAG_SERIAL |
180           PROFILE_ATTRIBUTE_FLAG_DESTINATION |
181           PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
182         break;
183       case DBUS_MESSAGE_TYPE_SIGNAL:
184         profile_print_with_attrs ("sig", message, &t,
185           PROFILE_ATTRIBUTE_FLAG_SERIAL |
186           PROFILE_ATTRIBUTE_FLAG_PATH |
187           PROFILE_ATTRIBUTE_FLAG_INTERFACE |
188           PROFILE_ATTRIBUTE_FLAG_MEMBER);
189         break;
190       default:
191         printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
192         break;
193     }
194 }
195
196 static DBusHandlerResult
197 profile_filter_func (DBusConnection     *connection,
198                      DBusMessage        *message,
199                      void               *user_data)
200 {
201   print_message_profile (message);
202
203   if (dbus_message_is_signal (message,
204                               DBUS_INTERFACE_LOCAL,
205                               "Disconnected"))
206     exit (0);
207
208   return DBUS_HANDLER_RESULT_HANDLED;
209 }
210
211 static void
212 usage (char *name, int ecode)
213 {
214   fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name);
215   exit (ecode);
216 }
217
218 static dbus_bool_t sigint_received = FALSE;
219
220 static void
221 sigint_handler (int signum)
222 {
223   sigint_received = TRUE;
224 }
225
226 int
227 main (int argc, char *argv[])
228 {
229   DBusConnection *connection;
230   DBusError error;
231   DBusBusType type = DBUS_BUS_SESSION;
232   DBusHandleMessageFunction filter_func = monitor_filter_func;
233   char *address = NULL;
234   
235   int i = 0, j = 0, numFilters = 0;
236   char **filters = NULL;
237
238   /* Set stdout to be unbuffered; this is basically so that if people
239    * do dbus-monitor > file, then send SIGINT via Control-C, they
240    * don't lose the last chunk of messages.
241    */
242
243 #ifdef DBUS_WIN
244   setvbuf (stdout, NULL, _IONBF, 0);
245 #else
246   setvbuf (stdout, NULL, _IOLBF, 0);
247 #endif
248
249   for (i = 1; i < argc; i++)
250     {
251       char *arg = argv[i];
252
253       if (!strcmp (arg, "--system"))
254         type = DBUS_BUS_SYSTEM;
255       else if (!strcmp (arg, "--session"))
256         type = DBUS_BUS_SESSION;
257       else if (!strcmp (arg, "--address"))
258         {
259           if (i+1 < argc)
260             {
261               address = argv[i+1];
262               i++;
263             }
264           else
265             usage (argv[0], 1);
266         }
267       else if (!strcmp (arg, "--help"))
268         usage (argv[0], 0);
269       else if (!strcmp (arg, "--monitor"))
270         filter_func = monitor_filter_func;
271       else if (!strcmp (arg, "--profile"))
272         filter_func = profile_filter_func;
273       else if (!strcmp (arg, "--"))
274         continue;
275       else if (arg[0] == '-')
276         usage (argv[0], 1);
277       else {
278         numFilters++;
279        filters = (char **)realloc(filters, numFilters * sizeof(char *));
280         filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
281         snprintf(filters[j], strlen(arg) + 1, "%s", arg);
282         j++;
283       }
284     }
285
286   dbus_error_init (&error);
287   
288   if (address != NULL)
289     {
290       connection = dbus_connection_open (address, &error);
291       if (connection)
292         {
293           if (!dbus_bus_register (connection, &error))
294             {
295               fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
296                        address, error.message);
297               dbus_error_free (&error);
298               exit (1);
299             }
300         }
301     }
302   else
303     connection = dbus_bus_get (type, &error);
304   if (connection == NULL)
305     {
306       const char *where;
307       if (address != NULL)
308         where = address;
309       else
310         {
311           switch (type)
312             {
313             case DBUS_BUS_SYSTEM:
314               where = "system bus";
315               break;
316             case DBUS_BUS_SESSION:
317               where = "session bus";
318               break;
319             default:
320               where = "";
321             }
322         }
323       fprintf (stderr, "Failed to open connection to %s: %s\n",
324                where,
325                error.message);
326       dbus_error_free (&error);
327       exit (1);
328     }
329
330   if (numFilters)
331     {
332       for (i = 0; i < j; i++)
333         {
334           dbus_bus_add_match (connection, filters[i], &error);
335           if (dbus_error_is_set (&error))
336             {
337               fprintf (stderr, "Failed to setup match \"%s\": %s\n",
338                        filters[i], error.message);
339               dbus_error_free (&error);
340               exit (1);
341             }
342           free(filters[i]);
343         }
344     }
345   else
346     {
347       dbus_bus_add_match (connection,
348                           "type='signal'",
349                           &error);
350       if (dbus_error_is_set (&error))
351         goto lose;
352       dbus_bus_add_match (connection,
353                           "type='method_call'",
354                           &error);
355       if (dbus_error_is_set (&error))
356         goto lose;
357       dbus_bus_add_match (connection,
358                           "type='method_return'",
359                           &error);
360       if (dbus_error_is_set (&error))
361         goto lose;
362       dbus_bus_add_match (connection,
363                           "type='error'",
364                           &error);
365       if (dbus_error_is_set (&error))
366         goto lose;
367     }
368
369   if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
370     fprintf (stderr, "Couldn't add filter!\n");
371     exit (1);
372   }
373
374   while (dbus_connection_read_write_dispatch(connection, -1))
375     ;
376   exit (0);
377  lose:
378   fprintf (stderr, "Error: %s\n", error.message);
379   exit (1);
380 }
381