diff --git a/man/systemd-analyze.xml b/man/systemd-analyze.xml index f81dcc324ae..8ceeccb37a0 100644 --- a/man/systemd-analyze.xml +++ b/man/systemd-analyze.xml @@ -139,6 +139,12 @@ security UNIT + + systemd-analyze + OPTIONS + malloc + D-BUS SERVICE + @@ -271,6 +277,16 @@ Timestamp units-load-finish: Thu 2019-03-14 23:28:07 CET + + <command>systemd-analyze malloc [<replaceable>D-Bus service</replaceable>…]</command> + + This command can be used to request the output of the internal memory state (as returned by + malloc_info3) of + a D-Bus service implementing this pattern. If no service is specified, the command will be sent to + org.freedesktop.systemd1 (the system or user service manager). The output format + is subject to change without notice and should not be parsed by applications. + + <command>systemd-analyze plot</command> diff --git a/src/analyze/analyze-dump.c b/src/analyze/analyze-dump.c index 2e838c906f3..2642582903b 100644 --- a/src/analyze/analyze-dump.c +++ b/src/analyze/analyze-dump.c @@ -29,21 +29,6 @@ static int dump_fallback(sd_bus *bus) { return 0; } -static int dump_fd_reply(sd_bus_message *message) { - int fd, r; - - r = sd_bus_message_read(message, "h", &fd); - if (r < 0) - return bus_log_parse_error(r); - - fflush(stdout); - r = copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0); - if (r < 0) - return r; - - return 1; /* Success */ -} - static int dump(sd_bus *bus) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; diff --git a/src/analyze/analyze-malloc.c b/src/analyze/analyze-malloc.c new file mode 100644 index 00000000000..5e6ff5bb962 --- /dev/null +++ b/src/analyze/analyze-malloc.c @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "sd-bus.h" + +#include "analyze-malloc.h" +#include "analyze.h" +#include "bus-error.h" +#include "bus-internal.h" + +static int dump_malloc_info(sd_bus *bus, char *service) { + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + int r; + + assert(bus); + assert(service); + + r = sd_bus_call_method(bus, + service, + "/org/freedesktop/MemoryAllocation1", + "org.freedesktop.MemoryAllocation1", + "GetMallocInfo", + &error, + &reply, + NULL); + if (r < 0) + return log_error_errno(r, "Failed to call GetMallocInfo on '%s': %s", service, bus_error_message(&error, r)); + + return dump_fd_reply(reply); +} + +int verb_malloc(int argc, char *argv[], void *userdata) { + _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; + char **services = STRV_MAKE("org.freedesktop.systemd1"); + int r; + + if (!strv_isempty(strv_skip(argv, 1))) { + services = strv_skip(argv, 1); + STRV_FOREACH(service, services) + if (!service_name_is_valid(*service)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "D-Bus service name '%s' is not valid.", *service); + } + + r = acquire_bus(&bus, NULL); + if (r < 0) + return bus_log_connect_error(r, arg_transport); + + r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD); + if (r < 0) + return log_error_errno(r, "Unable to determine if bus connection supports fd passing: %m"); + if (r == 0) + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Unable to receive FDs over D-Bus."); + + pager_open(arg_pager_flags); + + STRV_FOREACH(service, services) { + r = dump_malloc_info(bus, *service); + if (r < 0) + return r; + } + + return EXIT_SUCCESS; +} diff --git a/src/analyze/analyze-malloc.h b/src/analyze/analyze-malloc.h new file mode 100644 index 00000000000..d3feabd757e --- /dev/null +++ b/src/analyze/analyze-malloc.h @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#pragma once + +int verb_malloc(int argc, char *argv[], void *userdata); diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index aaa7e4b706a..d7688c1a85b 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -25,6 +25,7 @@ #include "analyze-filesystems.h" #include "analyze-inspect-elf.h" #include "analyze-log-control.h" +#include "analyze-malloc.h" #include "analyze-plot.h" #include "analyze-security.h" #include "analyze-service-watchdogs.h" @@ -166,6 +167,23 @@ void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timesp "Use 'systemd-analyze timespan \"%s\"' instead?", p); } +int dump_fd_reply(sd_bus_message *message) { + int fd, r; + + assert(message); + + r = sd_bus_message_read(message, "h", &fd); + if (r < 0) + return bus_log_parse_error(r); + + fflush(stdout); + r = copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0); + if (r < 0) + return r; + + return 1; /* Success */ +} + static int help(int argc, char *argv[], void *userdata) { _cleanup_free_ char *link = NULL, *dot_link = NULL; int r; @@ -211,6 +229,7 @@ static int help(int argc, char *argv[], void *userdata) { " timespan SPAN... Validate a time span\n" " security [UNIT...] Analyze security of unit\n" " inspect-elf FILE... Parse and print ELF package metadata\n" + " malloc [D-BUS SERVICE...] Dump malloc stats of a D-Bus service\n" "\nOptions:\n" " --recursive-errors=MODE Control which units are verified\n" " --offline=BOOL Perform a security review on unit file(s)\n" @@ -601,6 +620,7 @@ static int run(int argc, char *argv[]) { { "timespan", 2, VERB_ANY, 0, verb_timespan }, { "security", VERB_ANY, VERB_ANY, 0, verb_security }, { "inspect-elf", 2, VERB_ANY, 0, verb_elf_inspection }, + { "malloc", VERB_ANY, VERB_ANY, 0, verb_malloc }, {} }; diff --git a/src/analyze/analyze.h b/src/analyze/analyze.h index e4af7b47e00..35b1cddb38f 100644 --- a/src/analyze/analyze.h +++ b/src/analyze/analyze.h @@ -44,3 +44,5 @@ int acquire_bus(sd_bus **bus, bool *use_full_bus); int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv); void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan); + +int dump_fd_reply(sd_bus_message *message); diff --git a/src/analyze/meson.build b/src/analyze/meson.build index 6493cb2e995..ac40600a6de 100644 --- a/src/analyze/meson.build +++ b/src/analyze/meson.build @@ -14,6 +14,7 @@ systemd_analyze_sources = files( 'analyze-filesystems.c', 'analyze-inspect-elf.c', 'analyze-log-control.c', + 'analyze-malloc.c', 'analyze-plot.c', 'analyze-security.c', 'analyze-service-watchdogs.c', diff --git a/test/units/testsuite-65.sh b/test/units/testsuite-65.sh index 4093c5a2a78..edaf6671071 100755 --- a/test/units/testsuite-65.sh +++ b/test/units/testsuite-65.sh @@ -56,6 +56,7 @@ systemd-analyze dump "*" >/dev/null systemd-analyze dump "*.socket" >/dev/null systemd-analyze dump "*.socket" "*.service" aaaaaaa ... >/dev/null systemd-analyze dump systemd-journald.service >/dev/null +systemd-analyze malloc >/dev/null (! systemd-analyze dump "") # unit-files systemd-analyze unit-files >/dev/null