tizen: Add well-known-names -> unique names on graph
authorDawid Kuczma <d.kuczma@partner.samsung.com>
Tue, 5 Dec 2017 08:31:16 +0000 (09:31 +0100)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Wed, 29 May 2024 22:07:10 +0000 (00:07 +0200)
Usage:
--well-known-names=true

Change-Id: Ife01bd8d1a2d6acc4a45c6595bcc52c15d523742

src/busctl/busctl.c
src/libsystemd/sd-bus/bus-dump.c
src/libsystemd/sd-bus/bus-dump.h

index fab3a55..a49805f 100644 (file)
@@ -69,6 +69,7 @@ static int arg_receiver_pid = 0;
 static bool arg_pid = false;
 static bool arg_dot = false;
 static bool monitor_run_condi = true;
+static bool arg_well_known_names;
 
 STATIC_DESTRUCTOR_REGISTER(arg_matches, strv_freep);
 
@@ -1191,19 +1192,26 @@ static int introspect(int argc, char **argv, void *userdata) {
         return 0;
 }
 
-static int message_dump(sd_bus_message *m, FILE *f) {
+static int message_dump(sd_bus_message *m, FILE *f, Hashmap *hashmap_wkn, sd_bus *bus) {
         return sd_bus_message_dump(m, f, SD_BUS_MESSAGE_DUMP_WITH_HEADER);
 }
 
-static int message_pcap(sd_bus_message *m, FILE *f) {
+static int message_pcap(sd_bus_message *m, FILE *f, Hashmap *hashmap_wkn, sd_bus *bus) {
         return bus_message_pcap_frame(m, arg_snaplen, f);
 }
 
-static int message_dot(sd_bus_message *m, FILE *f) {
+static int message_dot(sd_bus_message *m, FILE *f, Hashmap *hashmap_wkn, sd_bus *bus) {
+
+        if (arg_well_known_names){
+
+                dot_dump_unique_name(bus, sd_bus_message_get_sender(m), hashmap_wkn, f, m);
+                dot_dump_unique_name(bus, sd_bus_message_get_destination(m), hashmap_wkn, f, m);
+        }
+
         return bus_message_dot_dump(m, f);
 }
 
-static bool check_pid(sd_bus *bus, Hashmap *hashmap_pids, const char *name, int compare_pid, sd_bus_message *m) {
+static bool check_pid(sd_bus *bus, Hashmap *hashmap_pids, const char *name, int compare_pid) {
         pid_t pid;
         sd_bus_creds *creds = NULL;
         int r;
@@ -1367,7 +1375,7 @@ static int prepare_connection(sd_bus *bus, char *argv[]) {
         return 0;
 }
 
-static int monitor(int argc, char *argv[], int (*dump)(sd_bus_message *m, FILE *f)) {
+static int monitor(int argc, char *argv[], int (*dump)(sd_bus_message *m, FILE *f, Hashmap *hashmap_wkn, sd_bus *bus)) {
         int r;
         bool receiver_pid_match;
         bool sender_pid_match;
@@ -1391,7 +1399,6 @@ static int monitor(int argc, char *argv[], int (*dump)(sd_bus_message *m, FILE *
 
         while (monitor_run_condi) {
                 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
-
                 receiver_pid_match = true;
                 sender_pid_match = true;
 
@@ -1402,8 +1409,7 @@ static int monitor(int argc, char *argv[], int (*dump)(sd_bus_message *m, FILE *
                                                         bus,
                                                         hashmap_pids,
                                                         sd_bus_message_get_sender(m),
-                                                        arg_sender_pid,
-                                                        m);
+                                                        arg_sender_pid);
                         }
 
                         if (arg_receiver_pid != 0) {
@@ -1411,12 +1417,11 @@ static int monitor(int argc, char *argv[], int (*dump)(sd_bus_message *m, FILE *
                                                         bus,
                                                         hashmap_pids,
                                                         sd_bus_message_get_destination(m),
-                                                        arg_receiver_pid,
-                                                        m);
+                                                        arg_receiver_pid);
                         }
 
                         if ((!arg_pid && receiver_pid_match && sender_pid_match) || (arg_pid && (receiver_pid_match || sender_pid_match))) {
-                                dump(m, stdout);
+                                dump(m, stdout, hashmap_names, bus);
                         }
 
                         fflush(stdout);
@@ -2442,6 +2447,8 @@ static int help(void) {
                "                          Only show message with sender pid equals SENDER_PID\n"
                "     --receiver-pid=RECEIVER_PID\n"
                "                          Only show message with receiver pid equals RECEIVER_PID\n"
+               "     --well-known-names=BOOL \n"
+               "                          Show well know names connected to unique names on graph\n"
                "\nSee the %s for details.\n",
                program_invocation_short_name,
                ansi_highlight(),
@@ -2505,6 +2512,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_PID,
                 ARG_SENDER_PID,
                 ARG_RECEIVER_PID,
+                ARG_WELL_KNOWN_NAMES,
         };
 
         static const struct option options[] = {
@@ -2539,6 +2547,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "pid",                             required_argument, NULL, ARG_PID},
                 { "sender-pid",                      required_argument, NULL, ARG_SENDER_PID},
                 { "receiver-pid",                    required_argument, NULL, ARG_RECEIVER_PID},
+                { "well-known-names",                required_argument, NULL, ARG_WELL_KNOWN_NAMES},
                 {},
         };
 
@@ -2714,6 +2723,10 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_destination = optarg;
                         break;
 
+                case ARG_WELL_KNOWN_NAMES:
+                        arg_well_known_names = parse_boolean(optarg);
+                        break;
+
                 case '?':
                         return -EINVAL;
 
index ab2a48c..0b60871 100644 (file)
@@ -94,6 +94,75 @@ int bus_message_dot_dump(sd_bus_message *m, FILE *f) {
         return 0;
 }
 
+void dot_dump_unique_name(sd_bus *bus, char *name, Hashmap *hashmap_wkn, FILE *f, sd_bus_message *m) {
+        int r;
+        bool update = false;
+        char *obtained;
+        const char *uname;
+        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;
+        sd_bus_creds *creds = NULL;
+
+        if (m->member && !strcmp(m->member, "NameOwnerChanged")) {
+
+                r = sd_bus_message_peek_type(m, &type, &contents);
+
+                if (r > 0 && bus_type_is_container(type) <= 0) {
+                        if (r >= 0)
+                                r = sd_bus_message_read_basic(m, type, &basic);
+
+                        if (r >= 0) {
+                                char *free_name = hashmap_get(hashmap_wkn, basic.string);
+                                free(free_name);
+                                update = true;
+                        }
+                }
+        }
+
+        if (!f)
+                f = stdout;
+
+        if (!name)
+                return;
+
+        if (name[0] == ':')
+                return;
+
+        obtained = hashmap_get(hashmap_wkn, name);
+
+        if (obtained && !update)
+                return;
+
+        r = sd_bus_get_name_creds(bus, name, SD_BUS_CREDS_UNIQUE_NAME, &creds);
+
+        if (r >= 0)
+                r = sd_bus_creds_get_unique_name(creds, &uname);
+
+        if (r < 0)
+                return;
+
+        fprintf(f, "\t\"%s\"->\"%s\" [ color = blue, penwidth = 4];\n", name, uname);
+
+        if (update)
+                hashmap_update(hashmap_wkn, name, strdup(uname));
+        else
+                hashmap_put(hashmap_wkn, strdup(name), strdup(uname));
+
+}
+
+
 _public_ int sd_bus_message_dump(sd_bus_message *m, FILE *f, uint64_t flags) {
         unsigned level = 1;
         int r;
index 7359d72..9828710 100644 (file)
@@ -3,10 +3,12 @@
 
 #include <stdbool.h>
 #include <stdio.h>
+#include <hashmap.h>
 
 #include "sd-bus.h"
 
 int bus_message_dot_dump(sd_bus_message *m, FILE *f);
+void dot_dump_unique_name(sd_bus *bus, char *name, Hashmap *hashmap_wkn, FILE *f, sd_bus_message *m);
 
 int bus_creds_dump(sd_bus_creds *c, FILE *f, bool terse);