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"
27 #include <sys/types.h>
29 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <netinet/ip.h>
34 #include <arpa/inet.h>
40 #include "tool-common.h"
42 #ifdef HAVE_INTTYPES_H
55 #ifndef HAVE_SOCKLEN_T
60 type_to_name (int message_type)
64 case DBUS_MESSAGE_TYPE_SIGNAL:
66 case DBUS_MESSAGE_TYPE_METHOD_CALL:
68 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
69 return "method return";
70 case DBUS_MESSAGE_TYPE_ERROR:
73 return "(unknown message type)";
83 printf (" "); /* INDENT spaces. */
87 print_hex (const unsigned char *bytes,
91 unsigned int i, columns;
93 printf ("array of bytes [\n");
97 /* Each byte takes 3 cells (two hexits, and a space), except the last one. */
98 columns = (80 - ((depth + 1) * INDENT)) / 3;
107 printf ("%02x", bytes[i]);
112 if (i % columns == 0)
129 #define DEFAULT_SIZE 100
132 print_ay (DBusMessageIter *iter, int depth)
134 /* True if every byte in the bytestring (so far) is printable
135 * ASCII, with one exception: the last byte is also allowed to be \0. */
136 dbus_bool_t all_ascii = TRUE;
137 const unsigned char *bytes;
141 dbus_message_iter_get_fixed_array (iter, &bytes, &len);
143 for (i = 0; i < len; i++)
145 if ((bytes[i] < 32 || bytes[i] > 126) &&
146 (i < len - 1 || bytes[i] != '\0'))
153 if (all_ascii && len > 0 && bytes[len - 1] == '\0')
155 printf ("array of bytes \"%s\" + \\0\n", bytes);
159 unsigned char *copy = dbus_malloc (len + 1);
162 tool_oom ("copying bytestring");
164 memcpy (copy, bytes, len);
166 printf ("array of bytes \"%s\"\n", copy);
171 print_hex (bytes, len, depth);
177 print_fd (int fd, int depth)
180 struct stat statbuf = {0,};
183 struct sockaddr_storage storage;
184 struct sockaddr_un un;
185 struct sockaddr_in ipv4;
186 struct sockaddr_in6 ipv6;
188 char hostip[INET6_ADDRSTRLEN];
189 socklen_t addrlen = sizeof (addr);
190 socklen_t peerlen = sizeof (peer);
193 /* Don't print the fd number: it is different in every process and since
194 * dbus-monitor closes the fd after reading it, the same number would be
195 * printed again and again.
197 printf ("file descriptor\n");
201 ret = fstat (fd, &statbuf);
206 printf ("inode: %d\n", (int) statbuf.st_ino);
210 if (S_ISREG(statbuf.st_mode))
212 if (S_ISDIR(statbuf.st_mode))
213 printf ("directory\n");
214 if (S_ISCHR(statbuf.st_mode))
216 if (S_ISBLK(statbuf.st_mode))
218 if (S_ISFIFO(statbuf.st_mode))
220 if (S_ISLNK(statbuf.st_mode))
222 if (S_ISSOCK(statbuf.st_mode))
225 /* If it's not a socket, getsockname will just return -1 with errno
228 memset (&addr, 0, sizeof (addr));
229 memset (&peer, 0, sizeof (peer));
231 if (getsockname(fd, &addr.sa, &addrlen))
234 has_peer = !getpeername(fd, &peer.sa, &peerlen);
237 printf ("address family: ");
238 switch (addr.sa.sa_family)
242 if (addr.un.sun_path[0] == '\0')
244 /* Abstract socket might not be zero-terminated and length is
245 * variable. Who designed this interface?
246 * Write the name in the same way as /proc/net/unix
247 * See manual page unix(7)
250 printf ("name @%.*s\n",
251 (int) (addrlen - sizeof (sa_family_t) - 1),
252 &(addr.un.sun_path[1]));
257 printf ("peer @%.*s\n",
258 (int) (addrlen - sizeof (sa_family_t) - 1),
259 &(addr.un.sun_path[1]));
265 printf ("name %s\n", addr.un.sun_path);
269 printf ("peer %s\n", peer.un.sun_path);
276 if (inet_ntop (AF_INET, &addr.ipv4.sin_addr, hostip, sizeof (hostip)))
279 printf ("name %s port %u\n", hostip, ntohs (addr.ipv4.sin_port));
281 if (has_peer && inet_ntop (AF_INET, &peer.ipv4.sin_addr, hostip, sizeof (hostip)))
284 printf ("peer %s port %u\n", hostip, ntohs (peer.ipv4.sin_port));
292 if (inet_ntop (AF_INET6, &addr.ipv6.sin6_addr, hostip, sizeof (hostip)))
295 printf ("name %s port %u\n", hostip, ntohs (addr.ipv6.sin6_port));
297 if (has_peer && inet_ntop (AF_INET6, &peer.ipv6.sin6_addr, hostip, sizeof (hostip)))
300 printf ("peer %s port %u\n", hostip, ntohs (peer.ipv6.sin6_port));
307 printf ("bluetooth\n");
312 printf ("unknown (%d)\n", addr.sa.sa_family);
319 print_iter (DBusMessageIter *iter, dbus_bool_t literal, int depth)
323 int type = dbus_message_iter_get_arg_type (iter);
325 if (type == DBUS_TYPE_INVALID)
332 case DBUS_TYPE_STRING:
335 dbus_message_iter_get_basic (iter, &val);
337 printf ("string \"");
344 case DBUS_TYPE_SIGNATURE:
347 dbus_message_iter_get_basic (iter, &val);
349 printf ("signature \"");
356 case DBUS_TYPE_OBJECT_PATH:
359 dbus_message_iter_get_basic (iter, &val);
361 printf ("object path \"");
368 case DBUS_TYPE_INT16:
371 dbus_message_iter_get_basic (iter, &val);
372 printf ("int16 %d\n", val);
376 case DBUS_TYPE_UINT16:
379 dbus_message_iter_get_basic (iter, &val);
380 printf ("uint16 %u\n", val);
384 case DBUS_TYPE_INT32:
387 dbus_message_iter_get_basic (iter, &val);
388 printf ("int32 %d\n", val);
392 case DBUS_TYPE_UINT32:
395 dbus_message_iter_get_basic (iter, &val);
396 printf ("uint32 %u\n", val);
400 case DBUS_TYPE_INT64:
403 dbus_message_iter_get_basic (iter, &val);
404 printf ("int64 %" PRId64 "\n", val);
408 case DBUS_TYPE_UINT64:
411 dbus_message_iter_get_basic (iter, &val);
412 printf ("uint64 %" PRIu64 "\n", val);
416 case DBUS_TYPE_DOUBLE:
419 dbus_message_iter_get_basic (iter, &val);
420 printf ("double %g\n", val);
427 dbus_message_iter_get_basic (iter, &val);
428 printf ("byte %d\n", val);
432 case DBUS_TYPE_BOOLEAN:
435 dbus_message_iter_get_basic (iter, &val);
436 printf ("boolean %s\n", val ? "true" : "false");
440 case DBUS_TYPE_VARIANT:
442 DBusMessageIter subiter;
444 dbus_message_iter_recurse (iter, &subiter);
447 print_iter (&subiter, literal, depth+1);
450 case DBUS_TYPE_ARRAY:
453 DBusMessageIter subiter;
455 dbus_message_iter_recurse (iter, &subiter);
457 current_type = dbus_message_iter_get_arg_type (&subiter);
459 if (current_type == DBUS_TYPE_BYTE)
461 print_ay (&subiter, depth);
466 while (current_type != DBUS_TYPE_INVALID)
468 print_iter (&subiter, literal, depth+1);
470 dbus_message_iter_next (&subiter);
471 current_type = dbus_message_iter_get_arg_type (&subiter);
473 if (current_type != DBUS_TYPE_INVALID)
480 case DBUS_TYPE_DICT_ENTRY:
482 DBusMessageIter subiter;
484 dbus_message_iter_recurse (iter, &subiter);
486 printf("dict entry(\n");
487 print_iter (&subiter, literal, depth+1);
488 dbus_message_iter_next (&subiter);
489 print_iter (&subiter, literal, depth+1);
495 case DBUS_TYPE_STRUCT:
498 DBusMessageIter subiter;
500 dbus_message_iter_recurse (iter, &subiter);
502 printf("struct {\n");
503 while ((current_type = dbus_message_iter_get_arg_type (&subiter)) != DBUS_TYPE_INVALID)
505 print_iter (&subiter, literal, depth+1);
506 dbus_message_iter_next (&subiter);
507 if (dbus_message_iter_get_arg_type (&subiter) != DBUS_TYPE_INVALID)
516 case DBUS_TYPE_UNIX_FD:
519 dbus_message_iter_get_basic (iter, &fd);
521 print_fd (fd, depth+1);
523 /* dbus_message_iter_get_basic() duplicated the fd, we need to
524 * close it after use. The original fd will be closed when the
525 * DBusMessage is released.
534 printf (" (dbus-monitor too dumb to decipher arg type '%c')\n", type);
537 } while (dbus_message_iter_next (iter));
541 print_message (DBusMessage *message, dbus_bool_t literal, long sec, long usec)
543 DBusMessageIter iter;
545 const char *destination;
548 message_type = dbus_message_get_type (message);
549 sender = dbus_message_get_sender (message);
550 destination = dbus_message_get_destination (message);
554 if (sec != 0 || usec != 0)
556 printf ("%s time=%ld.%06ld sender=%s -> destination=%s",
557 type_to_name (message_type), sec, usec,
558 sender ? sender : "(null sender)",
559 destination ? destination : "(null destination)");
563 printf ("%s sender=%s -> destination=%s",
564 type_to_name (message_type),
565 sender ? sender : "(null sender)",
566 destination ? destination : "(null destination)");
569 switch (message_type)
571 case DBUS_MESSAGE_TYPE_METHOD_CALL:
572 case DBUS_MESSAGE_TYPE_SIGNAL:
573 printf (" serial=%u path=%s; interface=%s; member=%s\n",
574 dbus_message_get_serial (message),
575 dbus_message_get_path (message),
576 dbus_message_get_interface (message),
577 dbus_message_get_member (message));
580 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
581 printf (" serial=%u reply_serial=%u\n",
582 dbus_message_get_serial (message),
583 dbus_message_get_reply_serial (message));
586 case DBUS_MESSAGE_TYPE_ERROR:
587 printf (" error_name=%s reply_serial=%u\n",
588 dbus_message_get_error_name (message),
589 dbus_message_get_reply_serial (message));
598 dbus_message_iter_init (message, &iter);
599 print_iter (&iter, literal, 1);