bus: beautify bus_message_dump() output a bit

This commit is contained in:
Lennart Poettering 2013-11-12 00:12:08 +01:00
parent 7b0b392f7b
commit 2b5c5383e4
7 changed files with 50 additions and 273 deletions

View file

@ -2006,6 +2006,17 @@ libsystemd_bus_internal_la_CFLAGS = \
noinst_LTLIBRARIES += \
libsystemd-bus-internal.la
libsystemd_bus_dump_la_SOURCES = \
src/libsystemd-bus/bus-dump.c \
src/libsystemd-bus/bus-dump.h
libsystemd_bus_dump_la_CFLAGS = \
$(AM_CFLAGS)
$(CAP_CFLAGS)
noinst_LTLIBRARIES += \
libsystemd-bus-dump.la
tests += \
test-bus-marshal \
test-bus-signature \
@ -2032,13 +2043,17 @@ test_bus_marshal_LDADD = \
libsystemd-id128-internal.la \
libsystemd-daemon-internal.la \
libsystemd-shared.la \
libsystemd-bus-dump.la \
libsystemd-capability.la \
$(GLIB_LIBS) \
$(DBUS_LIBS)
$(DBUS_LIBS) \
$(CAP_LIBS)
test_bus_marshal_CFLAGS = \
$(AM_CFLAGS) \
$(GLIB_CFLAGS) \
$(DBUS_CFLAGS)
$(DBUS_CFLAGS) \
$(CAP_CFLAGS)
test_bus_signature_SOURCES = \
src/libsystemd-bus/test-bus-signature.c
@ -2078,13 +2093,17 @@ test_bus_objects_SOURCES = \
test_bus_objects_CFLAGS = \
$(AM_CFLAGS) \
$(CAP_CFLAGS) \
-pthread
test_bus_objects_LDADD = \
libsystemd-bus-internal.la \
libsystemd-id128-internal.la \
libsystemd-daemon-internal.la \
libsystemd-shared.la
libsystemd-shared.la \
libsystemd-bus-dump.la \
libsystemd-capability.la \
$(CAP_LIBS)
test_bus_match_SOURCES = \
src/libsystemd-bus/test-bus-match.c
@ -2102,7 +2121,14 @@ test_bus_kernel_LDADD = \
libsystemd-bus-internal.la \
libsystemd-id128-internal.la \
libsystemd-daemon-internal.la \
libsystemd-shared.la
libsystemd-shared.la \
libsystemd-bus-dump.la \
libsystemd-capability.la \
$(CAP_LIBS)
test_bus_kernel_CFLAGS = \
$(AM_CFLAGS) \
$(CAP_CFLAGS)
test_bus_kernel_bloom_SOURCES = \
src/libsystemd-bus/test-bus-kernel-bloom.c
@ -2136,7 +2162,14 @@ test_bus_zero_copy_LDADD = \
libsystemd-bus-internal.la \
libsystemd-id128-internal.la \
libsystemd-daemon-internal.la \
libsystemd-shared.la
libsystemd-shared.la \
libsystemd-bus-dump.la \
libsystemd-capability.la \
$(CAP_LIBS)
test_bus_zero_copy_CFLAGS = \
$(AM_CFLAGS) \
$(CAP_CFLAGS)
test_bus_introspect_SOURCES = \
src/libsystemd-bus/test-bus-introspect.c
@ -2161,7 +2194,14 @@ busctl_LDADD = \
libsystemd-bus-internal.la \
libsystemd-id128-internal.la \
libsystemd-daemon-internal.la \
libsystemd-shared.la
libsystemd-shared.la \
libsystemd-bus-dump.la \
libsystemd-capability.la \
$(CAP_LIBS)
busctl_CFLAGS = \
$(AM_CFLAGS) \
$(CAP_CFLAGS)
# ------------------------------------------------------------------------------
if ENABLE_GTK_DOC

View file

@ -4205,272 +4205,6 @@ _public_ int sd_bus_message_set_destination(sd_bus_message *m, const char *desti
return message_append_field_string(m, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
}
int bus_message_dump(sd_bus_message *m, FILE *f, bool with_header) {
const char *u = NULL, *uu = NULL, *s = NULL;
char **cmdline = NULL;
unsigned level = 1;
int r;
uid_t owner, audit_loginuid;
uint32_t audit_sessionid;
assert(m);
if (!f)
f = stdout;
if (with_header) {
fprintf(f,
"Message %p\n"
"\tn_ref=%u\n"
"\tendian=%c\n"
"\ttype=%i\n"
"\tflags=%u\n"
"\tversion=%u\n"
"\tserial=%u\n"
"\tfields_size=%u\n"
"\tbody_size=%u\n"
"\tpath=%s\n"
"\tinterface=%s\n"
"\tmember=%s\n"
"\tdestination=%s\n"
"\tsender=%s\n"
"\tsignature=%s\n"
"\treply_serial=%u\n"
"\tsealed=%s\n"
"\tn_body_parts=%u\n",
m,
m->n_ref,
m->header->endian,
m->header->type,
m->header->flags,
m->header->version,
BUS_MESSAGE_SERIAL(m),
BUS_MESSAGE_FIELDS_SIZE(m),
BUS_MESSAGE_BODY_SIZE(m),
strna(m->path),
strna(m->interface),
strna(m->member),
strna(m->destination),
strna(m->sender),
strna(m->root_container.signature),
m->reply_serial,
yes_no(m->sealed),
m->n_body_parts);
if (sd_bus_error_is_set(&m->error))
fprintf(f,
"\terror.name=%s\n"
"\terror.message=%s\n",
strna(m->error.name),
strna(m->error.message));
if (m->pid != 0)
fprintf(f, "\tpid=%lu\n", (unsigned long) m->pid);
if (m->tid != 0)
fprintf(f, "\ttid=%lu\n", (unsigned long) m->tid);
if (m->uid_valid)
fprintf(f, "\tuid=%lu\n", (unsigned long) m->uid);
if (m->gid_valid)
fprintf(f, "\tgid=%lu\n", (unsigned long) m->gid);
if (m->pid_starttime != 0)
fprintf(f, "\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime);
if (m->monotonic != 0)
fprintf(f, "\tmonotonic=%llu\n", (unsigned long long) m->monotonic);
if (m->realtime != 0)
fprintf(f, "\trealtime=%llu\n", (unsigned long long) m->realtime);
if (m->exe)
fprintf(f, "\texe=[%s]\n", m->exe);
if (m->comm)
fprintf(f, "\tcomm=[%s]\n", m->comm);
if (m->tid_comm)
fprintf(f, "\ttid_comm=[%s]\n", m->tid_comm);
if (m->label)
fprintf(f, "\tlabel=[%s]\n", m->label);
if (m->cgroup)
fprintf(f, "\tcgroup=[%s]\n", m->cgroup);
sd_bus_message_get_unit(m, &u);
if (u)
fprintf(f, "\tunit=[%s]\n", u);
sd_bus_message_get_user_unit(m, &uu);
if (uu)
fprintf(f, "\tuser_unit=[%s]\n", uu);
sd_bus_message_get_session(m, &s);
if (s)
fprintf(f, "\tsession=[%s]\n", s);
if (sd_bus_message_get_owner_uid(m, &owner) >= 0)
fprintf(f, "\towner_uid=%lu\n", (unsigned long) owner);
if (sd_bus_message_get_audit_loginuid(m, &audit_loginuid) >= 0)
fprintf(f, "\taudit_loginuid=%lu\n", (unsigned long) audit_loginuid);
if (sd_bus_message_get_audit_sessionid(m, &audit_sessionid) >= 0)
fprintf(f, "\taudit_sessionid=%lu\n", (unsigned long) audit_sessionid);
r = sd_bus_message_has_effective_cap(m, 5);
if (r >= 0)
fprintf(f, "\tCAP_KILL=%s\n", yes_no(r));
if (sd_bus_message_get_cmdline(m, &cmdline) >= 0) {
char **c;
fputs("\tcmdline=[", f);
STRV_FOREACH(c, cmdline) {
if (c != cmdline)
fputc(' ', f);
fputs(*c, f);
}
fputs("]\n", f);
}
}
r = sd_bus_message_rewind(m, true);
if (r < 0) {
log_error("Failed to rewind: %s", strerror(-r));
return r;
}
fprintf(f, "MESSAGE \"%s\" {\n", strempty(m->root_container.signature));
for(;;) {
_cleanup_free_ char *prefix = NULL;
const char *contents = NULL;
char type;
union {
uint8_t u8;
uint16_t u16;
int16_t s16;
uint32_t u32;
int32_t s32;
uint64_t u64;
int64_t s64;
double d64;
const char *string;
int i;
} basic;
r = sd_bus_message_peek_type(m, &type, &contents);
if (r < 0) {
log_error("Failed to peek type: %s", strerror(-r));
return r;
}
if (r == 0) {
if (level <= 1)
break;
r = sd_bus_message_exit_container(m);
if (r < 0) {
log_error("Failed to exit container: %s", strerror(-r));
return r;
}
level--;
prefix = strrep("\t", level);
if (!prefix)
return log_oom();
fprintf(f, "%s};\n", prefix);
continue;
}
prefix = strrep("\t", level);
if (!prefix)
return log_oom();
if (bus_type_is_container(type) > 0) {
r = sd_bus_message_enter_container(m, type, contents);
if (r < 0) {
log_error("Failed to enter container: %s", strerror(-r));
return r;
}
if (type == SD_BUS_TYPE_ARRAY)
fprintf(f, "%sARRAY \"%s\" {\n", prefix, contents);
else if (type == SD_BUS_TYPE_VARIANT)
fprintf(f, "%sVARIANT \"%s\" {\n", prefix, contents);
else if (type == SD_BUS_TYPE_STRUCT)
fprintf(f, "%sSTRUCT \"%s\" {\n", prefix, contents);
else if (type == SD_BUS_TYPE_DICT_ENTRY)
fprintf(f, "%sDICT_ENTRY \"%s\" {\n", prefix, contents);
level ++;
continue;
}
r = sd_bus_message_read_basic(m, type, &basic);
if (r < 0) {
log_error("Failed to get basic: %s", strerror(-r));
return r;
}
assert(r > 0);
switch (type) {
case SD_BUS_TYPE_BYTE:
fprintf(f, "%sBYTE %u;\n", prefix, basic.u8);
break;
case SD_BUS_TYPE_BOOLEAN:
fprintf(f, "%sBOOLEAN %s;\n", prefix, yes_no(basic.i));
break;
case SD_BUS_TYPE_INT16:
fprintf(f, "%sINT16 %i;\n", prefix, basic.s16);
break;
case SD_BUS_TYPE_UINT16:
fprintf(f, "%sUINT16 %u;\n", prefix, basic.u16);
break;
case SD_BUS_TYPE_INT32:
fprintf(f, "%sINT32 %i;\n", prefix, basic.s32);
break;
case SD_BUS_TYPE_UINT32:
fprintf(f, "%sUINT32 %u;\n", prefix, basic.u32);
break;
case SD_BUS_TYPE_INT64:
fprintf(f, "%sINT64 %lli;\n", prefix, (long long) basic.s64);
break;
case SD_BUS_TYPE_UINT64:
fprintf(f, "%sUINT64 %llu;\n", prefix, (unsigned long long) basic.u64);
break;
case SD_BUS_TYPE_DOUBLE:
fprintf(f, "%sDOUBLE %g;\n", prefix, basic.d64);
break;
case SD_BUS_TYPE_STRING:
fprintf(f, "%sSTRING \"%s\";\n", prefix, basic.string);
break;
case SD_BUS_TYPE_OBJECT_PATH:
fprintf(f, "%sOBJECT_PATH \"%s\";\n", prefix, basic.string);
break;
case SD_BUS_TYPE_SIGNATURE:
fprintf(f, "%sSIGNATURE \"%s\";\n", prefix, basic.string);
break;
case SD_BUS_TYPE_UNIX_FD:
fprintf(f, "%sUNIX_FD %i;\n", prefix, basic.i);
break;
default:
assert_not_reached("Unknown basic type.");
}
}
fprintf(f, "};\n");
return 0;
}
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
size_t total;
void *p, *e;

View file

@ -190,7 +190,6 @@ static inline void* BUS_MESSAGE_FIELDS(sd_bus_message *m) {
}
int bus_message_seal(sd_bus_message *m, uint64_t serial);
int bus_message_dump(sd_bus_message *m, FILE *f, bool with_header);
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
int bus_message_read_strv_extend(sd_bus_message *m, char ***l);

View file

@ -31,6 +31,7 @@
#include "bus-message.h"
#include "bus-internal.h"
#include "bus-util.h"
#include "bus-dump.h"
static bool arg_no_pager = false;
static char *arg_address = NULL;

View file

@ -29,6 +29,7 @@
#include "bus-error.h"
#include "bus-kernel.h"
#include "bus-util.h"
#include "bus-dump.h"
int main(int argc, char *argv[]) {
_cleanup_close_ int bus_ref = -1;

View file

@ -37,6 +37,7 @@
#include "sd-bus.h"
#include "bus-message.h"
#include "bus-util.h"
#include "bus-dump.h"
int main(int argc, char *argv[]) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *copy = NULL;

View file

@ -30,6 +30,7 @@
#include "bus-message.h"
#include "bus-error.h"
#include "bus-kernel.h"
#include "bus-dump.h"
#define FIRST_ARRAY 17
#define SECOND_ARRAY 33