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