Debian Codegraph @

src/analyze/analyze-malloc.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "analyze.h"
6 #include "analyze-malloc.h"
7 #include "bus-error.h"
8 #include "bus-internal.h"
9 #include "bus-message-util.h"
10 #include "bus-util.h"
11 #include "log.h"
12 #include "pager.h"
13 #include "strv.h"
14
15 static int dump_malloc_info(sd_bus *bus, char *service) {
16 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
17 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
18 int r;
19
20 assert(bus);
21 assert(service);
22
23 r = sd_bus_call_method(bus,
24 service,
25 "/org/freedesktop/MemoryAllocation1",
26 "org.freedesktop.MemoryAllocation1",
27 "GetMallocInfo",
28 &error,
29 &reply,
30 NULL);
31 if (r < 0)
32 return log_error_errno(r, "Failed to call GetMallocInfo on '%s': %s", service, bus_error_message(&error, r));
33
34 return bus_message_dump_fd(reply);
35 }
36
37 int verb_malloc(int argc, char *argv[], uintptr_t _data, void *userdata) {
38 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
39 char **services = STRV_MAKE("org.freedesktop.systemd1");
40 int r;
41
42 char **args = strv_skip(argv, 1);
43 if (args) {
44 STRV_FOREACH(service, args)
45 if (!service_name_is_valid(*service))
46 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "D-Bus service name '%s' is not valid.", *service);
47 services = args;
48 }
49
50 r = acquire_bus(&bus, NULL);
51 if (r < 0)
52 return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
53
54 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
55 if (r < 0)
56 return log_error_errno(r, "Unable to determine if bus connection supports fd passing: %m");
57 if (r == 0)
58 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Unable to receive FDs over D-Bus.");
59
60 pager_open(arg_pager_flags);
61
62 STRV_FOREACH(service, services) {
63 r = dump_malloc_info(bus, *service);
64 if (r < 0)
65 return r;
66 }
67
68 return 0;
69 }