File 0236-erts-esock-Improve-debugging-during-unix-socket-open.patch of Package erlang

From a56538b401ed7a6df59f497b88fcf7360843fde8 Mon Sep 17 00:00:00 2001
From: Micael Karlberg <bmk@erlang.org>
Date: Fri, 6 Dec 2024 18:20:40 +0100
Subject: [PATCH 1/5] [erts|esock] Improve debugging during (unix) socket open

OTP-19386
---
 erts/emulator/nifs/common/socket_int.h       |  3 +
 erts/emulator/nifs/common/socket_util.c      | 94 ++++++++++++++++++++
 erts/emulator/nifs/common/socket_util.h      |  6 ++
 erts/emulator/nifs/unix/unix_socket_syncio.c | 47 ++++++----
 4 files changed, 131 insertions(+), 19 deletions(-)

diff --git a/erts/emulator/nifs/common/socket_int.h b/erts/emulator/nifs/common/socket_int.h
index ac0492202f..6f78107dc4 100644
--- a/erts/emulator/nifs/common/socket_int.h
+++ b/erts/emulator/nifs/common/socket_int.h
@@ -167,6 +167,9 @@ typedef int BOOLEAN_T;
 
 #define B2S(__B__) ((__B__) ? "true" : "false")
 
+#define DOM2STR(__D__)   esock_domain_to_string((__D__))
+#define PROTO2STR(__P__) esock_protocol_to_string((__P__))
+
 #define TYPE2STR(T) (((T) == SOCK_STREAM) ? "stream" :                  \
                      (((T) == SOCK_DGRAM) ? "dgram"  :                  \
                       (((T) == SOCK_RAW)  ? "raw"    :                  \
diff --git a/erts/emulator/nifs/common/socket_util.c b/erts/emulator/nifs/common/socket_util.c
index acd0c5f512..967c22508c 100644
--- a/erts/emulator/nifs/common/socket_util.c
+++ b/erts/emulator/nifs/common/socket_util.c
@@ -1871,6 +1871,100 @@ void esock_encode_domain(ErlNifEnv*    env,
 
 
 
+/*
+ * This is only intended for debugging.
+ * Transforms a 'domain' to a printable string.
+ */
+extern
+char* esock_domain_to_string(int domain)
+{
+    switch (domain) {
+    case AF_INET:
+        return "inet";
+        break;
+
+#if defined(HAVE_IN6) && defined(AF_INET6)
+    case AF_INET6:
+        return "inet6";
+        break;
+#endif
+
+#ifdef HAS_AF_LOCAL
+    case AF_LOCAL:
+        return "local";
+        break;
+#endif
+
+#ifdef AF_UNSPEC
+    case AF_UNSPEC:
+        return "unspec";
+        break;
+#endif
+
+    default:
+        return "undefined";
+    }
+}
+
+
+
+/*
+ * This is only intended for debugging.
+ * Transforms a 'protocol' to a printable string.
+ */
+extern
+char* esock_protocol_to_string(int protocol)
+{
+    switch (protocol) {
+#if defined(IPPROTO_IP)
+    case IPPROTO_IP:
+        return "ip";
+        break;
+#endif
+
+#if defined(IPPROTO_ICMP)
+    case IPPROTO_ICMP:
+        return "icmp";
+        break;
+#endif
+
+#if defined(IPPROTO_IGMP)
+    case IPPROTO_IGMP:
+        return "igmp";
+        break;
+#endif
+
+#if defined(IPPROTO_TCP)
+    case IPPROTO_TCP:
+        return "tcp";
+        break;
+#endif
+
+#if defined(IPPROTO_UDP)
+    case IPPROTO_UDP:
+        return "udp";
+        break;
+#endif
+
+#if defined(IPPROTO_SCTP)
+    case IPPROTO_SCTP:
+        return "sctp";
+        break;
+#endif
+
+#if defined(IPPROTO_RAW)
+    case IPPROTO_RAW:
+        return "raw";
+        break;
+#endif
+
+    default:
+        return "undefined";
+    }
+}
+
+
+
 /* +++ esock_decode_type +++
  *
  * Decode the Erlang form of the 'type' type, that is: 
diff --git a/erts/emulator/nifs/common/socket_util.h b/erts/emulator/nifs/common/socket_util.h
index 0f61f8ce2b..853183e140 100644
--- a/erts/emulator/nifs/common/socket_util.h
+++ b/erts/emulator/nifs/common/socket_util.h
@@ -189,6 +189,9 @@ extern BOOLEAN_T esock_decode_timeval(ErlNifEnv*      env,
                                       ERL_NIF_TERM    eTime,
                                       struct timeval* timeP);
 
+extern
+char* esock_domain_to_string(int domain);
+
 extern
 void esock_encode_domain(ErlNifEnv*    env,
                          int           domain,
@@ -198,6 +201,9 @@ int esock_decode_domain(ErlNifEnv*   env,
                         ERL_NIF_TERM eDomain,
                         int*         domain);
 
+extern
+char* esock_protocol_to_string(int domain);
+
 extern
 BOOLEAN_T esock_decode_type(ErlNifEnv*   env,
                             ERL_NIF_TERM eType,
diff --git a/erts/emulator/nifs/unix/unix_socket_syncio.c b/erts/emulator/nifs/unix/unix_socket_syncio.c
index e83aef62e9..4a952b8f06 100644
--- a/erts/emulator/nifs/unix/unix_socket_syncio.c
+++ b/erts/emulator/nifs/unix/unix_socket_syncio.c
@@ -832,7 +832,7 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv*       env,
     ESOCK_ASSERT( enif_self(env, &self) != NULL );
 
     SSDBG2( dbg,
-            ("UNIX-ESSIO", "essio_open2 -> entry with"
+            ("UNIX-ESSIO", "essio_open_with_fd -> entry with"
              "\r\n   fd:    %d"
              "\r\n   eopts: %T"
              "\r\n", fd, eopts) );
@@ -851,7 +851,7 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv*       env,
     if (! open_which_domain(fd, &domain)) {
         SSDBG2( dbg,
                 ("UNIX-ESSIO",
-                 "essio_open2 -> failed get domain from system\r\n") );
+                 "essio_open_with_fd -> failed get domain from system\r\n") );
 
         if (! open_get_domain(env, eopts, &domain)) {
             return esock_make_invalid(env, esock_atom_domain);
@@ -861,7 +861,7 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv*       env,
     if (! open_which_type(fd, &type)) {
         SSDBG2( dbg,
                 ("UNIX-ESSIO",
-                 "essio_open2 -> failed get type from system\r\n") );
+                 "essio_open_with_fd -> failed get type from system\r\n") );
 
         if (! open_get_type(env, eopts, &type))
             return esock_make_invalid(env, esock_atom_type);
@@ -870,12 +870,12 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv*       env,
     if (! esock_open_which_protocol(fd, &protocol)) {
         SSDBG2( dbg,
                 ("UNIX-ESSIO",
-                 "essio_open2 -> failed get protocol from system\r\n") );
+                 "essio_open_with_fd -> failed get protocol from system\r\n") );
 
         if (! open_get_protocol(env, eopts, &protocol)) {
             SSDBG2( dbg,
                     ("UNIX-ESSIO",
-                     "essio_open2 -> "
+                     "essio_open_with_fd -> "
                      "failed get protocol => try protocol 0\r\n") );
             protocol = 0;
         }
@@ -884,11 +884,14 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv*       env,
 
     SSDBG2( dbg,
             ("UNIX-ESSIO",
-             "essio_open2 -> "
-             "\r\n   domain:   %d"
-             "\r\n   type:     %d"
-             "\r\n   protocol: %d"
-             "\r\n", domain, type, protocol) );
+             "essio_open_with_fd -> "
+             "\r\n   domain:   %d (%s)"
+             "\r\n   type:     %d (%s)"
+             "\r\n   protocol: %d (%s)"
+             "\r\n",
+             domain,   DOM2STR(domain),
+             type,     TYPE2STR(type),
+             protocol, PROTO2STR(protocol)) );
 
 
     if (open_todup(env, eopts)) {
@@ -898,7 +901,7 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv*       env,
 
             SSDBG2( dbg,
                     ("UNIX-ESSIO",
-                     "essio_open2 -> dup failed: %d\r\n",
+                     "essio_open_with_fd -> dup failed: %d\r\n",
                      save_errno) );
 
             return esock_make_error_errno(env, save_errno);
@@ -929,10 +932,12 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv*       env,
         if (sock_peer(descP->sock,
                       (struct sockaddr*) &remote,
                       &addrLen) == 0) {
-            SSDBG2( dbg, ("UNIX-ESSIO", "essio_open2 -> connected\r\n") );
+            SSDBG2( dbg, ("UNIX-ESSIO",
+                          "essio_open_with_fd -> connected\r\n") );
             descP->writeState |= ESOCK_STATE_CONNECTED;
         } else {
-            SSDBG2( dbg, ("UNIX-ESSIO", "essio_open2 -> not connected\r\n") );
+            SSDBG2( dbg, ("UNIX-ESSIO",
+                          "essio_open_with_fd -> not connected\r\n") );
         }
     }
 
@@ -940,7 +945,7 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv*       env,
     sockRef = enif_make_resource(env, descP);
     enif_release_resource(descP);
 
-    ESOCK_ASSERT( MONP("essio_open2 -> ctrl",
+    ESOCK_ASSERT( MONP("essio_open_with_fd -> ctrl",
                        env, descP,
                        &descP->ctrlPid,
                        &descP->ctrlMon) == 0 );
@@ -955,7 +960,7 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv*       env,
     if (descP->useReg) esock_send_reg_add_msg(env, descP, sockRef);
 
     SSDBG2( dbg,
-            ("UNIX-ESSIO", "essio_open2 -> done: %T\r\n", sockRef) );
+            ("UNIX-ESSIO", "essio_open_with_fd -> done: %T\r\n", sockRef) );
 
     return esock_make_ok2(env, sockRef);
 }
@@ -1071,11 +1076,15 @@ ERL_NIF_TERM essio_open_plain(ErlNifEnv*       env,
 
     SSDBG2( dbg,
             ("UNIX-ESSIO", "essio_open4 -> entry with"
-             "\r\n   domain:   %d"
-             "\r\n   type:     %d"
-             "\r\n   protocol: %d"
+             "\r\n   domain:   %d (%s)"
+             "\r\n   type:     %d (%s)"
+             "\r\n   protocol: %d (%s)"
              "\r\n   eopts:    %T"
-             "\r\n", domain, type, protocol, eopts) );
+             "\r\n",
+             domain,   DOM2STR(domain),
+             type,     TYPE2STR(type),
+             protocol, PROTO2STR(protocol),
+             eopts) );
 
 
 #ifdef HAVE_SETNS
-- 
2.43.0

openSUSE Build Service is sponsored by