* tools/dbus-monitor.c: add profiling tools to dbus-monitor
[platform/upstream/dbus.git] / tools / dbus-monitor.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <sys/time.h>
27 #include <time.h>
28
29 #include "dbus-print-message.h"
30
31 static DBusHandlerResult
32 monitor_filter_func (DBusConnection     *connection,
33                      DBusMessage        *message,
34                      void               *user_data)
35 {
36   print_message (message, FALSE);
37   
38   if (dbus_message_is_signal (message,
39                               DBUS_INTERFACE_LOCAL,
40                               "Disconnected"))
41     exit (0);
42   
43   /* Conceptually we want this to be
44    * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
45    * some problems.  See bug 1719.
46    */
47   return DBUS_HANDLER_RESULT_HANDLED;
48 }
49
50 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
51 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
52
53 typedef enum
54 {
55   PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
56   PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
57   PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
58   PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
59   PROFILE_ATTRIBUTE_FLAG_PATH = 16,
60   PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
61   PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
62   PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128,
63 } ProfileAttributeFlags;
64
65 static void
66 profile_print_with_attrs (const char *type, DBusMessage *message,
67   struct timeval *t, ProfileAttributeFlags attrs)
68 {
69   printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
70
71   if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
72     printf ("\t%u", dbus_message_get_serial (message));
73
74   if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
75     printf ("\t%u", dbus_message_get_reply_serial (message));
76
77   if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
78     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
79
80   if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
81     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
82
83   if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
84     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
85
86   if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
87     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
88
89   if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
90     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
91
92   if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
93     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
94
95   printf ("\n");
96 }
97
98 static void
99 print_message_profile (DBusMessage *message)
100 {
101   struct timeval t;
102
103   if (gettimeofday (&t, NULL) < 0)
104     {
105       printf ("un\n");
106       return;
107     }
108
109   switch (dbus_message_get_type (message))
110     {
111       case DBUS_MESSAGE_TYPE_METHOD_CALL:
112         profile_print_with_attrs ("mc", message, &t,
113           PROFILE_ATTRIBUTE_FLAG_SERIAL |
114           PROFILE_ATTRIBUTE_FLAG_SENDER |
115           PROFILE_ATTRIBUTE_FLAG_PATH |
116           PROFILE_ATTRIBUTE_FLAG_INTERFACE |
117           PROFILE_ATTRIBUTE_FLAG_MEMBER);
118         break;
119       case DBUS_MESSAGE_TYPE_METHOD_RETURN:
120         profile_print_with_attrs ("mr", message, &t,
121           PROFILE_ATTRIBUTE_FLAG_SERIAL |
122           PROFILE_ATTRIBUTE_FLAG_DESTINATION |
123           PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
124         break;
125       case DBUS_MESSAGE_TYPE_ERROR:
126         profile_print_with_attrs ("err", message, &t,
127           PROFILE_ATTRIBUTE_FLAG_SERIAL |
128           PROFILE_ATTRIBUTE_FLAG_DESTINATION |
129           PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
130         break;
131       case DBUS_MESSAGE_TYPE_SIGNAL:
132         profile_print_with_attrs ("sig", message, &t,
133           PROFILE_ATTRIBUTE_FLAG_SERIAL |
134           PROFILE_ATTRIBUTE_FLAG_PATH |
135           PROFILE_ATTRIBUTE_FLAG_INTERFACE |
136           PROFILE_ATTRIBUTE_FLAG_MEMBER);
137         break;
138       default:
139         printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
140         break;
141     }
142 }
143
144 static DBusHandlerResult
145 profile_filter_func (DBusConnection     *connection,
146                      DBusMessage        *message,
147                      void               *user_data)
148 {
149   print_message_profile (message);
150
151   if (dbus_message_is_signal (message,
152                               DBUS_INTERFACE_LOCAL,
153                               "Disconnected"))
154     exit (0);
155
156   return DBUS_HANDLER_RESULT_HANDLED;
157 }
158
159 static void
160 usage (char *name, int ecode)
161 {
162   fprintf (stderr, "Usage: %s [--system | --session] [--monitor | --profile ] [watch expressions]\n", name);
163   exit (ecode);
164 }
165
166 int
167 main (int argc, char *argv[])
168 {
169   DBusConnection *connection;
170   DBusError error;
171   DBusBusType type = DBUS_BUS_SESSION;
172   DBusHandleMessageFunction filter_func = monitor_filter_func;
173
174   int i = 0, j = 0, numFilters = 0;
175   char **filters = NULL;
176   for (i = 1; i < argc; i++)
177     {
178       char *arg = argv[i];
179
180       if (!strcmp (arg, "--system"))
181         type = DBUS_BUS_SYSTEM;
182       else if (!strcmp (arg, "--session"))
183         type = DBUS_BUS_SESSION;
184       else if (!strcmp (arg, "--help"))
185         usage (argv[0], 0);
186       else if (!strcmp (arg, "--monitor"))
187         filter_func = monitor_filter_func;
188       else if (!strcmp (arg, "--profile"))
189         filter_func = profile_filter_func;
190       else if (!strcmp (arg, "--"))
191         continue;
192       else if (arg[0] == '-')
193         usage (argv[0], 1);
194       else {
195         numFilters++;
196        filters = (char **)realloc(filters, numFilters * sizeof(char *));
197         filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
198         snprintf(filters[j], strlen(arg) + 1, "%s", arg);
199         j++;
200       }
201     }
202
203   dbus_error_init (&error);
204   connection = dbus_bus_get (type, &error);
205   if (connection == NULL)
206     {
207       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
208                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
209                error.message);
210       dbus_error_free (&error);
211       exit (1);
212     }
213
214   if (numFilters)
215     {
216       for (i = 0; i < j; i++)
217         {
218           dbus_bus_add_match (connection, filters[i], &error);
219           if (dbus_error_is_set (&error))
220             {
221               fprintf (stderr, "Failed to setup match \"%s\": %s\n",
222                        filters[i], error.message);
223               dbus_error_free (&error);
224               exit (1);
225             }
226           free(filters[i]);
227         }
228     }
229   else
230     {
231       dbus_bus_add_match (connection,
232                           "type='signal'",
233                           &error);
234       if (dbus_error_is_set (&error))
235         goto lose;
236       dbus_bus_add_match (connection,
237                           "type='method_call'",
238                           &error);
239       if (dbus_error_is_set (&error))
240         goto lose;
241       dbus_bus_add_match (connection,
242                           "type='method_return'",
243                           &error);
244       if (dbus_error_is_set (&error))
245         goto lose;
246       dbus_bus_add_match (connection,
247                           "type='error'",
248                           &error);
249       if (dbus_error_is_set (&error))
250         goto lose;
251     }
252
253   if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
254     fprintf (stderr, "Couldn't add filter!\n");
255     exit (1);
256   }
257   while (dbus_connection_read_write_dispatch(connection, -1))
258     ;
259   exit (0);
260  lose:
261   fprintf (stderr, "Error: %s\n", error.message);
262   exit (1);
263 }
264