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"
27 type_to_name (int message_type)
31 case DBUS_MESSAGE_TYPE_SIGNAL:
33 case DBUS_MESSAGE_TYPE_METHOD_CALL:
35 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
36 return "method return";
37 case DBUS_MESSAGE_TYPE_ERROR:
40 return "(unknown message type)";
50 printf (" "); /* INDENT spaces. */
54 print_hex (unsigned char *bytes, unsigned int len, int depth)
58 printf ("array of bytes [\n");
62 /* Each byte takes 3 cells (two hexits, and a space), except the last one. */
63 columns = (80 - ((depth + 1) * INDENT)) / 3;
72 printf ("%02x", bytes[i]);
94 #define DEFAULT_SIZE 100
97 print_ay (DBusMessageIter *iter, int depth)
99 /* Not using DBusString because it's not public API. It's 2009, and I'm
100 * manually growing a string chunk by chunk.
102 unsigned char *bytes = malloc (DEFAULT_SIZE + 1);
103 unsigned int len = 0;
104 unsigned int max = DEFAULT_SIZE;
105 dbus_bool_t all_ascii = TRUE;
108 while ((current_type = dbus_message_iter_get_arg_type (iter))
109 != DBUS_TYPE_INVALID)
113 dbus_message_iter_get_basic (iter, &val);
117 if (val < 32 || val > 126)
123 bytes = realloc (bytes, max + 1);
126 dbus_message_iter_next (iter);
132 printf ("array of bytes \"%s\"\n", bytes);
136 print_hex (bytes, len, depth);
143 print_iter (DBusMessageIter *iter, dbus_bool_t literal, int depth)
147 int type = dbus_message_iter_get_arg_type (iter);
149 if (type == DBUS_TYPE_INVALID)
156 case DBUS_TYPE_STRING:
159 dbus_message_iter_get_basic (iter, &val);
161 printf ("string \"");
168 case DBUS_TYPE_SIGNATURE:
171 dbus_message_iter_get_basic (iter, &val);
173 printf ("signature \"");
180 case DBUS_TYPE_OBJECT_PATH:
183 dbus_message_iter_get_basic (iter, &val);
185 printf ("object path \"");
192 case DBUS_TYPE_INT16:
195 dbus_message_iter_get_basic (iter, &val);
196 printf ("int16 %d\n", val);
200 case DBUS_TYPE_UINT16:
203 dbus_message_iter_get_basic (iter, &val);
204 printf ("uint16 %u\n", val);
208 case DBUS_TYPE_INT32:
211 dbus_message_iter_get_basic (iter, &val);
212 printf ("int32 %d\n", val);
216 case DBUS_TYPE_UINT32:
219 dbus_message_iter_get_basic (iter, &val);
220 printf ("uint32 %u\n", val);
224 case DBUS_TYPE_INT64:
227 dbus_message_iter_get_basic (iter, &val);
228 printf ("int64 %lld\n", val);
232 case DBUS_TYPE_UINT64:
235 dbus_message_iter_get_basic (iter, &val);
236 printf ("uint64 %llu\n", val);
240 case DBUS_TYPE_DOUBLE:
243 dbus_message_iter_get_basic (iter, &val);
244 printf ("double %g\n", val);
251 dbus_message_iter_get_basic (iter, &val);
252 printf ("byte %d\n", val);
256 case DBUS_TYPE_BOOLEAN:
259 dbus_message_iter_get_basic (iter, &val);
260 printf ("boolean %s\n", val ? "true" : "false");
264 case DBUS_TYPE_VARIANT:
266 DBusMessageIter subiter;
268 dbus_message_iter_recurse (iter, &subiter);
271 print_iter (&subiter, literal, depth+1);
274 case DBUS_TYPE_ARRAY:
277 DBusMessageIter subiter;
279 dbus_message_iter_recurse (iter, &subiter);
281 current_type = dbus_message_iter_get_arg_type (&subiter);
283 if (current_type == DBUS_TYPE_BYTE)
285 print_ay (&subiter, depth);
290 while (current_type != DBUS_TYPE_INVALID)
292 print_iter (&subiter, literal, depth+1);
294 dbus_message_iter_next (&subiter);
295 current_type = dbus_message_iter_get_arg_type (&subiter);
297 if (current_type != DBUS_TYPE_INVALID)
304 case DBUS_TYPE_DICT_ENTRY:
306 DBusMessageIter subiter;
308 dbus_message_iter_recurse (iter, &subiter);
310 printf("dict entry(\n");
311 print_iter (&subiter, literal, depth+1);
312 dbus_message_iter_next (&subiter);
313 print_iter (&subiter, literal, depth+1);
319 case DBUS_TYPE_STRUCT:
322 DBusMessageIter subiter;
324 dbus_message_iter_recurse (iter, &subiter);
326 printf("struct {\n");
327 while ((current_type = dbus_message_iter_get_arg_type (&subiter)) != DBUS_TYPE_INVALID)
329 print_iter (&subiter, literal, depth+1);
330 dbus_message_iter_next (&subiter);
331 if (dbus_message_iter_get_arg_type (&subiter) != DBUS_TYPE_INVALID)
340 printf (" (dbus-monitor too dumb to decipher arg type '%c')\n", type);
343 } while (dbus_message_iter_next (iter));
347 print_message (DBusMessage *message, dbus_bool_t literal)
349 DBusMessageIter iter;
351 const char *destination;
354 message_type = dbus_message_get_type (message);
355 sender = dbus_message_get_sender (message);
356 destination = dbus_message_get_destination (message);
360 printf ("%s sender=%s -> dest=%s",
361 type_to_name (message_type),
362 sender ? sender : "(null sender)",
363 destination ? destination : "(null destination)");
365 switch (message_type)
367 case DBUS_MESSAGE_TYPE_METHOD_CALL:
368 case DBUS_MESSAGE_TYPE_SIGNAL:
369 printf (" serial=%u path=%s; interface=%s; member=%s\n",
370 dbus_message_get_serial (message),
371 dbus_message_get_path (message),
372 dbus_message_get_interface (message),
373 dbus_message_get_member (message));
376 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
377 printf (" reply_serial=%u\n",
378 dbus_message_get_reply_serial (message));
381 case DBUS_MESSAGE_TYPE_ERROR:
382 printf (" error_name=%s reply_serial=%u\n",
383 dbus_message_get_error_name (message),
384 dbus_message_get_reply_serial (message));
393 dbus_message_iter_init (message, &iter);
394 print_iter (&iter, literal, 1);