1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-print-message.h Utility function to print out a message
4 * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
5 * Copyright (C) 2003 Red Hat, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "dbus-print-message.h"
28 type_to_name (int message_type)
32 case DBUS_MESSAGE_TYPE_SIGNAL:
34 case DBUS_MESSAGE_TYPE_METHOD_CALL:
36 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
37 return "method return";
38 case DBUS_MESSAGE_TYPE_ERROR:
41 return "(unknown message type)";
51 printf (" "); /* INDENT spaces. */
55 print_hex (unsigned char *bytes, unsigned int len, int depth)
59 printf ("array of bytes [\n");
63 /* Each byte takes 3 cells (two hexits, and a space), except the last one. */
64 columns = (80 - ((depth + 1) * INDENT)) / 3;
73 printf ("%02x", bytes[i]);
95 #define DEFAULT_SIZE 100
98 print_ay (DBusMessageIter *iter, int depth)
100 /* Not using DBusString because it's not public API. It's 2009, and I'm
101 * manually growing a string chunk by chunk.
103 unsigned char *bytes = malloc (DEFAULT_SIZE + 1);
104 unsigned int len = 0;
105 unsigned int max = DEFAULT_SIZE;
106 dbus_bool_t all_ascii = TRUE;
109 while ((current_type = dbus_message_iter_get_arg_type (iter))
110 != DBUS_TYPE_INVALID)
114 dbus_message_iter_get_basic (iter, &val);
118 if (val < 32 || val > 126)
124 bytes = realloc (bytes, max + 1);
127 dbus_message_iter_next (iter);
133 printf ("array of bytes \"%s\"\n", bytes);
137 print_hex (bytes, len, depth);
144 print_iter (DBusMessageIter *iter, dbus_bool_t literal, int depth)
148 int type = dbus_message_iter_get_arg_type (iter);
150 if (type == DBUS_TYPE_INVALID)
157 case DBUS_TYPE_STRING:
160 dbus_message_iter_get_basic (iter, &val);
162 printf ("string \"");
169 case DBUS_TYPE_SIGNATURE:
172 dbus_message_iter_get_basic (iter, &val);
174 printf ("signature \"");
181 case DBUS_TYPE_OBJECT_PATH:
184 dbus_message_iter_get_basic (iter, &val);
186 printf ("object path \"");
193 case DBUS_TYPE_INT16:
196 dbus_message_iter_get_basic (iter, &val);
197 printf ("int16 %d\n", val);
201 case DBUS_TYPE_UINT16:
204 dbus_message_iter_get_basic (iter, &val);
205 printf ("uint16 %u\n", val);
209 case DBUS_TYPE_INT32:
212 dbus_message_iter_get_basic (iter, &val);
213 printf ("int32 %d\n", val);
217 case DBUS_TYPE_UINT32:
220 dbus_message_iter_get_basic (iter, &val);
221 printf ("uint32 %u\n", val);
225 case DBUS_TYPE_INT64:
228 dbus_message_iter_get_basic (iter, &val);
229 #ifdef DBUS_INT64_PRINTF_MODIFIER
230 printf ("int64 %" DBUS_INT64_PRINTF_MODIFIER "d\n", val);
232 printf ("int64 (omitted)\n");
237 case DBUS_TYPE_UINT64:
240 dbus_message_iter_get_basic (iter, &val);
241 #ifdef DBUS_INT64_PRINTF_MODIFIER
242 printf ("uint64 %" DBUS_INT64_PRINTF_MODIFIER "u\n", val);
244 printf ("uint64 (omitted)\n");
249 case DBUS_TYPE_DOUBLE:
252 dbus_message_iter_get_basic (iter, &val);
253 printf ("double %g\n", val);
260 dbus_message_iter_get_basic (iter, &val);
261 printf ("byte %d\n", val);
265 case DBUS_TYPE_BOOLEAN:
268 dbus_message_iter_get_basic (iter, &val);
269 printf ("boolean %s\n", val ? "true" : "false");
273 case DBUS_TYPE_VARIANT:
275 DBusMessageIter subiter;
277 dbus_message_iter_recurse (iter, &subiter);
280 print_iter (&subiter, literal, depth+1);
283 case DBUS_TYPE_ARRAY:
286 DBusMessageIter subiter;
288 dbus_message_iter_recurse (iter, &subiter);
290 current_type = dbus_message_iter_get_arg_type (&subiter);
292 if (current_type == DBUS_TYPE_BYTE)
294 print_ay (&subiter, depth);
299 while (current_type != DBUS_TYPE_INVALID)
301 print_iter (&subiter, literal, depth+1);
303 dbus_message_iter_next (&subiter);
304 current_type = dbus_message_iter_get_arg_type (&subiter);
306 if (current_type != DBUS_TYPE_INVALID)
313 case DBUS_TYPE_DICT_ENTRY:
315 DBusMessageIter subiter;
317 dbus_message_iter_recurse (iter, &subiter);
319 printf("dict entry(\n");
320 print_iter (&subiter, literal, depth+1);
321 dbus_message_iter_next (&subiter);
322 print_iter (&subiter, literal, depth+1);
328 case DBUS_TYPE_STRUCT:
331 DBusMessageIter subiter;
333 dbus_message_iter_recurse (iter, &subiter);
335 printf("struct {\n");
336 while ((current_type = dbus_message_iter_get_arg_type (&subiter)) != DBUS_TYPE_INVALID)
338 print_iter (&subiter, literal, depth+1);
339 dbus_message_iter_next (&subiter);
340 if (dbus_message_iter_get_arg_type (&subiter) != DBUS_TYPE_INVALID)
349 printf (" (dbus-monitor too dumb to decipher arg type '%c')\n", type);
352 } while (dbus_message_iter_next (iter));
356 print_message (DBusMessage *message, dbus_bool_t literal)
358 DBusMessageIter iter;
360 const char *destination;
363 message_type = dbus_message_get_type (message);
364 sender = dbus_message_get_sender (message);
365 destination = dbus_message_get_destination (message);
369 printf ("%s sender=%s -> dest=%s",
370 type_to_name (message_type),
371 sender ? sender : "(null sender)",
372 destination ? destination : "(null destination)");
374 switch (message_type)
376 case DBUS_MESSAGE_TYPE_METHOD_CALL:
377 case DBUS_MESSAGE_TYPE_SIGNAL:
378 printf (" serial=%u path=%s; interface=%s; member=%s\n",
379 dbus_message_get_serial (message),
380 dbus_message_get_path (message),
381 dbus_message_get_interface (message),
382 dbus_message_get_member (message));
385 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
386 printf (" reply_serial=%u\n",
387 dbus_message_get_reply_serial (message));
390 case DBUS_MESSAGE_TYPE_ERROR:
391 printf (" error_name=%s reply_serial=%u\n",
392 dbus_message_get_error_name (message),
393 dbus_message_get_reply_serial (message));
402 dbus_message_iter_init (message, &iter);
403 print_iter (&iter, literal, 1);