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
24 #include "dbus-print-message.h"
30 type_to_name (int message_type)
34 case DBUS_MESSAGE_TYPE_SIGNAL:
36 case DBUS_MESSAGE_TYPE_METHOD_CALL:
38 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
39 return "method return";
40 case DBUS_MESSAGE_TYPE_ERROR:
43 return "(unknown message type)";
53 printf (" "); /* INDENT spaces. */
57 print_hex (unsigned char *bytes, unsigned int len, int depth)
59 unsigned int i, columns;
61 printf ("array of bytes [\n");
65 /* Each byte takes 3 cells (two hexits, and a space), except the last one. */
66 columns = (80 - ((depth + 1) * INDENT)) / 3;
75 printf ("%02x", bytes[i]);
97 #define DEFAULT_SIZE 100
100 print_ay (DBusMessageIter *iter, int depth)
102 /* Not using DBusString because it's not public API. It's 2009, and I'm
103 * manually growing a string chunk by chunk.
105 unsigned char *bytes = malloc (DEFAULT_SIZE + 1);
106 unsigned int len = 0;
107 unsigned int max = DEFAULT_SIZE;
108 dbus_bool_t all_ascii = TRUE;
111 while ((current_type = dbus_message_iter_get_arg_type (iter))
112 != DBUS_TYPE_INVALID)
116 dbus_message_iter_get_basic (iter, &val);
120 if (val < 32 || val > 126)
126 bytes = realloc (bytes, max + 1);
129 dbus_message_iter_next (iter);
135 printf ("array of bytes \"%s\"\n", bytes);
139 print_hex (bytes, len, depth);
146 print_iter (DBusMessageIter *iter, dbus_bool_t literal, int depth)
150 int type = dbus_message_iter_get_arg_type (iter);
152 if (type == DBUS_TYPE_INVALID)
159 case DBUS_TYPE_STRING:
162 dbus_message_iter_get_basic (iter, &val);
164 printf ("string \"");
171 case DBUS_TYPE_SIGNATURE:
174 dbus_message_iter_get_basic (iter, &val);
176 printf ("signature \"");
183 case DBUS_TYPE_OBJECT_PATH:
186 dbus_message_iter_get_basic (iter, &val);
188 printf ("object path \"");
195 case DBUS_TYPE_INT16:
198 dbus_message_iter_get_basic (iter, &val);
199 printf ("int16 %d\n", val);
203 case DBUS_TYPE_UINT16:
206 dbus_message_iter_get_basic (iter, &val);
207 printf ("uint16 %u\n", val);
211 case DBUS_TYPE_INT32:
214 dbus_message_iter_get_basic (iter, &val);
215 printf ("int32 %d\n", val);
219 case DBUS_TYPE_UINT32:
222 dbus_message_iter_get_basic (iter, &val);
223 printf ("uint32 %u\n", val);
227 case DBUS_TYPE_INT64:
230 dbus_message_iter_get_basic (iter, &val);
231 #ifdef DBUS_INT64_PRINTF_MODIFIER
232 printf ("int64 %" DBUS_INT64_PRINTF_MODIFIER "d\n", val);
234 printf ("int64 (omitted)\n");
239 case DBUS_TYPE_UINT64:
242 dbus_message_iter_get_basic (iter, &val);
243 #ifdef DBUS_INT64_PRINTF_MODIFIER
244 printf ("uint64 %" DBUS_INT64_PRINTF_MODIFIER "u\n", val);
246 printf ("uint64 (omitted)\n");
251 case DBUS_TYPE_DOUBLE:
254 dbus_message_iter_get_basic (iter, &val);
255 printf ("double %g\n", val);
262 dbus_message_iter_get_basic (iter, &val);
263 printf ("byte %d\n", val);
267 case DBUS_TYPE_BOOLEAN:
270 dbus_message_iter_get_basic (iter, &val);
271 printf ("boolean %s\n", val ? "true" : "false");
275 case DBUS_TYPE_VARIANT:
277 DBusMessageIter subiter;
279 dbus_message_iter_recurse (iter, &subiter);
282 print_iter (&subiter, literal, depth+1);
285 case DBUS_TYPE_ARRAY:
288 DBusMessageIter subiter;
290 dbus_message_iter_recurse (iter, &subiter);
292 current_type = dbus_message_iter_get_arg_type (&subiter);
294 if (current_type == DBUS_TYPE_BYTE)
296 print_ay (&subiter, depth);
301 while (current_type != DBUS_TYPE_INVALID)
303 print_iter (&subiter, literal, depth+1);
305 dbus_message_iter_next (&subiter);
306 current_type = dbus_message_iter_get_arg_type (&subiter);
308 if (current_type != DBUS_TYPE_INVALID)
315 case DBUS_TYPE_DICT_ENTRY:
317 DBusMessageIter subiter;
319 dbus_message_iter_recurse (iter, &subiter);
321 printf("dict entry(\n");
322 print_iter (&subiter, literal, depth+1);
323 dbus_message_iter_next (&subiter);
324 print_iter (&subiter, literal, depth+1);
330 case DBUS_TYPE_STRUCT:
333 DBusMessageIter subiter;
335 dbus_message_iter_recurse (iter, &subiter);
337 printf("struct {\n");
338 while ((current_type = dbus_message_iter_get_arg_type (&subiter)) != DBUS_TYPE_INVALID)
340 print_iter (&subiter, literal, depth+1);
341 dbus_message_iter_next (&subiter);
342 if (dbus_message_iter_get_arg_type (&subiter) != DBUS_TYPE_INVALID)
351 printf (" (dbus-monitor too dumb to decipher arg type '%c')\n", type);
354 } while (dbus_message_iter_next (iter));
358 print_message (DBusMessage *message, dbus_bool_t literal)
360 DBusMessageIter iter;
362 const char *destination;
365 message_type = dbus_message_get_type (message);
366 sender = dbus_message_get_sender (message);
367 destination = dbus_message_get_destination (message);
371 printf ("%s sender=%s -> dest=%s",
372 type_to_name (message_type),
373 sender ? sender : "(null sender)",
374 destination ? destination : "(null destination)");
376 switch (message_type)
378 case DBUS_MESSAGE_TYPE_METHOD_CALL:
379 case DBUS_MESSAGE_TYPE_SIGNAL:
380 printf (" serial=%u path=%s; interface=%s; member=%s\n",
381 dbus_message_get_serial (message),
382 dbus_message_get_path (message),
383 dbus_message_get_interface (message),
384 dbus_message_get_member (message));
387 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
388 printf (" reply_serial=%u\n",
389 dbus_message_get_reply_serial (message));
392 case DBUS_MESSAGE_TYPE_ERROR:
393 printf (" error_name=%s reply_serial=%u\n",
394 dbus_message_get_error_name (message),
395 dbus_message_get_reply_serial (message));
404 dbus_message_iter_init (message, &iter);
405 print_iter (&iter, literal, 1);