From bb78dcf76c2ef7d0ba297161dda5bcaa42f0f63e Mon Sep 17 00:00:00 2001
From: wo_cow <niuqianqian@huawei.com>
Date: Mon, 14 Nov 2022 16:51:59 +0800
Subject: [PATCH] fix pgsliprobe: 1. pgsliprobe reports error when gaussdb
 doesn't depend on libssl. 2. add a check to ensure skb->tstamp is not 0

---
 config/gala-gopher.conf                       |  6 ++++
 .../ebpf.probe/src/pgsliprobe/pgsliprobe.c    | 36 +++++++++++--------
 .../src/pgsliprobe/pgsliprobe_bpf.h           |  8 +++--
 3 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/config/gala-gopher.conf b/config/gala-gopher.conf
index 3c1848b..fad3214 100644
--- a/config/gala-gopher.conf
+++ b/config/gala-gopher.conf
@@ -205,5 +205,11 @@ extend_probes =
         start_check = "[ -z $(which java 2>/dev/null) ] && echo 0 || echo 1";
         check_type = "count";
         switch = "auto";
+    },
+    {
+        name = "stackprobe";
+        command = "/opt/gala-gopher/extend_probes/stackprobe";
+        param = "";
+        switch = "off";
     }
 );
diff --git a/src/probes/extends/ebpf.probe/src/pgsliprobe/pgsliprobe.c b/src/probes/extends/ebpf.probe/src/pgsliprobe/pgsliprobe.c
index 10508ca..9171985 100644
--- a/src/probes/extends/ebpf.probe/src/pgsliprobe/pgsliprobe.c
+++ b/src/probes/extends/ebpf.probe/src/pgsliprobe/pgsliprobe.c
@@ -63,6 +63,7 @@
 static volatile sig_atomic_t stop;
 static struct probe_params params = {.period = DEFAULT_PERIOD};
 static struct bpf_link_hash_t *head = NULL;
+static int noDependLibssl;
 
 enum pid_state_t {
     PID_NOEXIST,
@@ -241,7 +242,8 @@ static int get_elf_path(unsigned int pid, char elf_path[], int max_path_len)
     // 1. get elf_path
     (void)snprintf(cmd, COMMAND_LEN, PLDD_LIBSSL_COMMAND, pid);
     if (exec_cmd((const char *)cmd, openssl_path, PATH_LEN) < 0) {
-        fprintf(stderr, "pldd %u grep libssl failed\n", pid);
+        noDependLibssl = 1;
+        INFO("[DAEMON] GaussDB does not depend on libssl\n");
         return SLI_ERR;
     }
 
@@ -318,13 +320,16 @@ static int add_bpf_link_by_search_pids()
         // find_bpf_link and add_bpf_link will set bpf_link status
         if (!find_bpf_link(pid)) {
             if (add_bpf_link(pid) != SLI_OK) {
+                if (noDependLibssl) {
+                    goto out;
+                }
                 fprintf(stderr, "add_bpf_link of pid %u failed\n", pid);
             } else {
                 printf("add_bpf_link of pid %u success\n", pid);
             }
         }
     }
-
+out:
     (void)pclose(f);
     return ret;
 }
@@ -375,7 +380,6 @@ int main(int argc, char **argv)
 {
     int err, ret;
     FILE *fp = NULL;
-    int init = 0;
     struct bpf_link_hash_t *item, *tmp;
 
     err = args_parse(argc, argv, &params);
@@ -405,11 +409,26 @@ int main(int argc, char **argv)
         goto init_err;
     }
 
+    load_args(GET_MAP_FD(pgsli_kprobe, args_map), &params);
+    err = init_conn_mgt_process(GET_MAP_FD(pgsli_kprobe, output));
+    if (err != 0) {
+        fprintf(stderr, "Init connection management process failed.\n");
+        goto init_err;
+    }
+
     printf("pgsliprobe probe successfully started!\n");
 
     while (!stop) {
+        sleep(params.period);
+        if (noDependLibssl) {
+            continue;
+        }
+
         set_bpf_link_inactive();
         if (add_bpf_link_by_search_pids() != SLI_OK) {
+            if (noDependLibssl) {
+                continue;
+            }
             goto init_err;
         }
 
@@ -437,18 +456,7 @@ int main(int argc, char **argv)
                 item->v.pid_state = PID_ELF_ATTACHED;
             }
         }
-
         clear_invalid_bpf_link();
-        if (init == 0) {
-            load_args(GET_MAP_FD(pgsli_kprobe, args_map), &params);
-            err = init_conn_mgt_process(GET_MAP_FD(pgsli_kprobe, output));
-            if (err != 0) {
-                fprintf(stderr, "Init connection management process failed.\n");
-                goto init_err;
-            }
-            init = 1;
-        }
-        sleep(params.period);
     }
 
 init_err:
diff --git a/src/probes/extends/ebpf.probe/src/pgsliprobe/pgsliprobe_bpf.h b/src/probes/extends/ebpf.probe/src/pgsliprobe/pgsliprobe_bpf.h
index e5afad7..a2e85b2 100644
--- a/src/probes/extends/ebpf.probe/src/pgsliprobe/pgsliprobe_bpf.h
+++ b/src/probes/extends/ebpf.probe/src/pgsliprobe/pgsliprobe_bpf.h
@@ -223,9 +223,11 @@ static __always_inline void process_rdwr_msg(int fd, const char *buf, int count,
             return;
         }
         csd->req_cmd = cmd;
-#ifndef KERNEL_SUPPORT_TSTAMP
-        csd->start_ts_nsec = ts_nsec;
-#endif
+
+        if (csd->start_ts_nsec == 0) {
+            csd->start_ts_nsec = ts_nsec;
+        }
+
         csd->status = SAMP_READ_READY;
     } else {  // MSG_WRITE
         if (csd->status == SAMP_READ_READY) {
-- 
2.23.0

