Merge pull request #26217 from bluca/dump_mem

Add D-Bus method and analyze command to dump malloc_info()
This commit is contained in:
Luca Boccassi
2023-02-24 10:31:21 +00:00
committed by GitHub
11 changed files with 177 additions and 15 deletions
+16
View File
@@ -139,6 +139,12 @@
<arg choice="plain">security</arg>
<arg choice="plain" rep="repeat"><replaceable>UNIT</replaceable></arg>
</cmdsynopsis>
<cmdsynopsis>
<command>systemd-analyze</command>
<arg choice="opt" rep="repeat">OPTIONS</arg>
<arg choice="plain">malloc</arg>
<arg choice="opt" rep="repeat"><replaceable>D-BUS SERVICE</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>
<refsect1>
@@ -271,6 +277,16 @@ Timestamp units-load-finish: Thu 2019-03-14 23:28:07 CET
</example>
</refsect2>
<refsect2>
<title><command>systemd-analyze malloc [<replaceable>D-Bus service</replaceable>…]</command></title>
<para>This command can be used to request the output of the internal memory state (as returned by
<citerefentry><refentrytitle>malloc_info</refentrytitle><manvolnum>3</manvolnum></citerefentry>) of
a D-Bus service implementing this pattern. If no service is specified, the command will be sent to
<filename>org.freedesktop.systemd1</filename> (the system or user service manager). The output format
is subject to change without notice and should not be parsed by applications.</para>
</refsect2>
<refsect2>
<title><command>systemd-analyze plot</command></title>
-15
View File
@@ -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;
+63
View File
@@ -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;
}
+5
View File
@@ -0,0 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
int verb_malloc(int argc, char *argv[], void *userdata);
+20
View File
@@ -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 },
{}
};
+2
View File
@@ -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);
+1
View File
@@ -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',
+8
View File
@@ -737,6 +737,10 @@ static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void
return 0;
}
r = bus_register_malloc_status(bus, "org.freedesktop.systemd1");
if (r < 0)
log_warning_errno(r, "Failed to register MemoryAllocation1, ignoring: %m");
r = set_ensure_put(&m->private_buses, NULL, bus);
if (r == -ENOMEM) {
log_oom();
@@ -798,6 +802,10 @@ static int bus_setup_api(Manager *m, sd_bus *bus) {
if (r < 0)
return log_error_errno(r, "Failed to request name: %m");
r = bus_register_malloc_status(bus, "org.freedesktop.systemd1");
if (r < 0)
log_warning_errno(r, "Failed to register MemoryAllocation1, ignoring: %m");
log_debug("Successfully connected to API bus.");
return 0;
+58
View File
@@ -18,6 +18,8 @@
#include "bus-internal.h"
#include "bus-label.h"
#include "bus-util.h"
#include "data-fd-util.h"
#include "fd-util.h"
#include "path-util.h"
#include "socket-util.h"
#include "stdio-util.h"
@@ -578,6 +580,62 @@ int bus_reply_pair_array(sd_bus_message *m, char **l) {
return sd_bus_send(NULL, reply, NULL);
}
static int method_dump_memory_state_by_fd(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
_cleanup_free_ char *dump = NULL; /* keep this above dump_file, so that it's freed after */
_cleanup_fclose_ FILE *dump_file = NULL;
_cleanup_close_ int fd = -EBADF;
size_t dump_size;
int r;
assert(message);
dump_file = open_memstream(&dump, &dump_size);
if (!dump_file)
return -ENOMEM;
r = RET_NERRNO(malloc_info(/* options= */ 0, dump_file));
if (r < 0)
return r;
dump_file = safe_fclose(dump_file);
fd = acquire_data_fd(dump, dump_size, 0);
if (fd < 0)
return fd;
r = sd_bus_reply_method_return(message, "h", fd);
if (r < 0)
return r;
return 1; /* Stop further processing */
}
/* The default install callback will fail and disconnect the bus if it cannot register the match, but this
* is only a debug method, we definitely don't want to fail in case there's some permission issue. */
static int dummy_install_callback(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
return 1;
}
int bus_register_malloc_status(sd_bus *bus, const char *destination) {
const char *match;
int r;
assert(bus);
assert(!isempty(destination));
match = strjoina("type='method_call',"
"interface='org.freedesktop.MemoryAllocation1',"
"path='/org/freedesktop/MemoryAllocation1',"
"destination='", destination, "',",
"member='GetMallocInfo'");
r = sd_bus_add_match_async(bus, NULL, match, method_dump_memory_state_by_fd, dummy_install_callback, NULL);
if (r < 0)
return log_debug_errno(r, "Failed to subscribe to GetMallocInfo() calls on MemoryAllocation1 interface: %m");
return 0;
}
static void bus_message_unref_wrapper(void *m) {
sd_bus_message_unref(m);
}
+3
View File
@@ -63,4 +63,7 @@ static inline int bus_open_system_watch_bind(sd_bus **ret) {
int bus_reply_pair_array(sd_bus_message *m, char **l);
/* Listen to GetMallocInfo() calls to 'destination' and return malloc_info() via FD */
int bus_register_malloc_status(sd_bus *bus, const char *destination);
extern const struct hash_ops bus_message_hash_ops;
+1
View File
@@ -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