From 283a06bdf3bcde99a2aa867c6c8b5d543a85c560 Mon Sep 17 00:00:00 2001 From: huxiangyuan Date: Thu, 5 Jan 2023 09:06:23 +0800 Subject: [PATCH 1/5] Support Attach Agent for NoAPM Java Application Signed-off-by: huxiangyuan --- collector/docker/Dockerfile | 5 +- collector/docker/DockerfileLocalProbe | 5 +- collector/docker/build-asyncprofiler.sh | 24 +++++++ .../component/controller/profile_module.go | 38 +++++++++++ .../component/receiver/cgoreceiver/cgo_func.h | 2 + probe/src/cgo/cgo_func.cpp | 3 + probe/src/cgo/cgo_func.h | 2 + probe/src/cgo/kindling.cpp | 66 +++++++++++++++++++ probe/src/cgo/kindling.h | 6 ++ 9 files changed, 147 insertions(+), 4 deletions(-) create mode 100755 collector/docker/build-asyncprofiler.sh diff --git a/collector/docker/Dockerfile b/collector/docker/Dockerfile index 687e9ecb8..c201d8d60 100644 --- a/collector/docker/Dockerfile +++ b/collector/docker/Dockerfile @@ -5,8 +5,9 @@ RUN yum install -y qt5-qtbase-devel RUN yum install -y gdb COPY gdb_print.sh /app/ RUN curl https://k8s-bpf-probes-public.oss-cn-hangzhou.aliyuncs.com/kindling-falcolib-probe-v0.6.0.tar.gz -o kindling-falcolib-probe.tar.gz -RUN curl -O https://k8s-bpf-probes-public.oss-cn-hangzhou.aliyuncs.com/async-profiler-kindling-v1.0.1.tar.gz -RUN tar -zvxf async-profiler-kindling-v1.0.1.tar.gz + +COPY build-asyncprofiler.sh /app/ +RUN sh build-asyncprofiler.sh COPY libso/libkindling.so /lib64/ COPY libso/* /usr/lib64/ diff --git a/collector/docker/DockerfileLocalProbe b/collector/docker/DockerfileLocalProbe index 4d7228386..60bad09c8 100644 --- a/collector/docker/DockerfileLocalProbe +++ b/collector/docker/DockerfileLocalProbe @@ -5,8 +5,9 @@ RUN yum install -y qt5-qtbase-devel RUN yum install -y gdb COPY gdb_print.sh /app/ COPY kindling-falcolib-probe.tar.gz ./ -RUN curl -O https://k8s-bpf-probes-public.oss-cn-hangzhou.aliyuncs.com/async-profiler-kindling-v1.0.1.tar.gz -RUN tar -zvxf async-profiler-kindling-v1.0.1.tar.gz + +COPY build-asyncprofiler.sh /app/ +RUN sh build-asyncprofiler.sh COPY libso/libkindling.so /lib64/ COPY libso/* /usr/lib64/ diff --git a/collector/docker/build-asyncprofiler.sh b/collector/docker/build-asyncprofiler.sh new file mode 100755 index 000000000..d39d96772 --- /dev/null +++ b/collector/docker/build-asyncprofiler.sh @@ -0,0 +1,24 @@ +ASYNC_PROFILER=async-profiler-1.0.2-linux-x64.tar.gz +KINDLING_JAVA=kindling-java-1.0.2.tar.gz +APM_ALL=apm-all-3.1.0.jar + +SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_BIN")" > /dev/null 2>&1; pwd -P)" +if [ ! -d "$SCRIPT_DIR/async-profiler" ];then + curl -O https://k8s-bpf-probes-public.oss-cn-hangzhou.aliyuncs.com/$ASYNC_PROFILER + tar -zvxf $ASYNC_PROFILER + rm -f $ASYNC_PROFILER +fi + +if [ ! -d "$SCRIPT_DIR/async-profiler/agent/kindling-java" ];then + curl -O https://k8s-bpf-probes-public.oss-cn-hangzhou.aliyuncs.com/$KINDLING_JAVA + tar -zxvf $KINDLING_JAVA + mkdir -p $SCRIPT_DIR/async-profiler/agent + mv kindling-java $SCRIPT_DIR/async-profiler/agent/ + rm -f $KINDLING_JAVA +fi + +if [ ! -f "$SCRIPT_DIR/async-profiler/agent/apm-all/apm-all.jar" ];then + mkdir -p $SCRIPT_DIR/async-profiler/agent/apm-all + cd $SCRIPT_DIR/async-profiler/agent/apm-all + curl -o apm-all.jar https://k8s-bpf-probes-public.oss-cn-hangzhou.aliyuncs.com/$APM_ALL +fi \ No newline at end of file diff --git a/collector/pkg/component/controller/profile_module.go b/collector/pkg/component/controller/profile_module.go index 2cb2e60b3..b6ec4dc62 100644 --- a/collector/pkg/component/controller/profile_module.go +++ b/collector/pkg/component/controller/profile_module.go @@ -88,6 +88,22 @@ func (p *Profile) GetModuleKey() string { return p.Name() } +func startAttachAgent(pid int) error { + if C.startAttachAgent(C.int(pid)) == 0 { + return nil + } else { + return fmt.Errorf("start Attach Agent failed") + } +} + +func stopAttachAgent(pid int) error { + if C.stopAttachAgent(C.int(pid)) == 0 { + return nil + } else { + return fmt.Errorf("stop Attach Agent failed") + } +} + func startDebug(pid int, tid int) error { C.startProfileDebug(C.int(pid), C.int(tid)) return nil @@ -123,6 +139,28 @@ func (p *Profile) HandRequest(req *ControlRequest) *ControlResponse { Code: NoError, Msg: "stop success", } + case "start_attach_agent": + if err := startAttachAgent(req.Pid); err != nil { + return &ControlResponse{ + Code: StartWithError, + Msg: err.Error(), + } + } + return &ControlResponse{ + Code: NoError, + Msg: "start success", + } + case "stop_attach_agent": + if err := stopAttachAgent(req.Pid); err != nil { + return &ControlResponse{ + Code: StopWithError, + Msg: err.Error(), + } + } + return &ControlResponse{ + Code: NoError, + Msg: "stop success", + } case "status": var status string switch p.Status() { diff --git a/collector/pkg/component/receiver/cgoreceiver/cgo_func.h b/collector/pkg/component/receiver/cgoreceiver/cgo_func.h index a4ad75cf2..16de5f2c3 100644 --- a/collector/pkg/component/receiver/cgoreceiver/cgo_func.h +++ b/collector/pkg/component/receiver/cgoreceiver/cgo_func.h @@ -13,6 +13,8 @@ int getKindlingEvent(void **kindlingEvent); int subEventForGo(char* eventName, char* category, void *params); int startProfile(); int stopProfile(); +int startAttachAgent(int pid); +int stopAttachAgent(int pid); void startProfileDebug(int pid, int tid); void stopProfileDebug(); void getCaptureStatistics(); diff --git a/probe/src/cgo/cgo_func.cpp b/probe/src/cgo/cgo_func.cpp index 2432c7b16..59f8fc6f5 100644 --- a/probe/src/cgo/cgo_func.cpp +++ b/probe/src/cgo/cgo_func.cpp @@ -14,6 +14,9 @@ int getKindlingEvent(void** kindlingEvent) { return getEvent(kindlingEvent); } int startProfile() { return start_profile(); } int stopProfile() { return stop_profile(); } +int startAttachAgent(int pid) { return start_attach_agent(pid); } +int stopAttachAgent(int pid) { return stop_attach_agent(pid); } + void subEventForGo(char* eventName, char* category, void *params) { sub_event(eventName, category, (event_params_for_subscribe *)params); } void startProfileDebug(int pid, int tid) { start_profile_debug(pid, tid); } diff --git a/probe/src/cgo/cgo_func.h b/probe/src/cgo/cgo_func.h index f566df09b..4406ad445 100644 --- a/probe/src/cgo/cgo_func.h +++ b/probe/src/cgo/cgo_func.h @@ -13,6 +13,8 @@ int getKindlingEvent(void** kindlingEvent); void subEventForGo(char* eventName, char* category, void* params); int startProfile(); int stopProfile(); +int startAttachAgent(int pid); +int stopAttachAgent(int pid); void startProfileDebug(int pid, int tid); void stopProfileDebug(); void getCaptureStatistics(); diff --git a/probe/src/cgo/kindling.cpp b/probe/src/cgo/kindling.cpp index 36277b445..f9a1e18a9 100644 --- a/probe/src/cgo/kindling.cpp +++ b/probe/src/cgo/kindling.cpp @@ -918,6 +918,72 @@ void attach_pid(char* pid, bool is_new_start, bool is_attach, bool is_all_attach } } +void attach_agent(int64_t pid, bool is_attach) { + char result_buf[1024], command[1024]; + string attach_command_prefix; + if (is_attach) { + attach_command_prefix = "./async-profiler/jattach.sh start "; + } else { + attach_command_prefix = "./async-profiler/jattach.sh stop "; + } + attach_command_prefix.append(std::to_string(pid)); + strcpy(command, attach_command_prefix.c_str()); + + FILE* fp; + fp = popen(command, "r"); + if (NULL == fp) { + perror("popen execute failed!\n"); + return; + } + if (is_attach) { + cout << "------" + << " start attach agent for pid " << pid << "------" << endl; + } else { + cout << "------" + << " start detach agent for pid " << pid << "------" << endl; + } + + while (fgets(result_buf, sizeof(result_buf), fp) != NULL) { + if ('\n' == result_buf[strlen(result_buf) - 1]) { + result_buf[strlen(result_buf) - 1] = '\0'; + } + printf("%s\r\n", result_buf); + } + + int rc = pclose(fp); + if (-1 == rc) { + perror("close command fp failed!\n"); + exit(1); + } else { + printf("command:【%s】command process status:【%d】command return value:【%d】\r\n", command, + rc, WEXITSTATUS(rc)); + } + + if (is_attach) { + cout << "------end attach agent for pid " << pid << "------" << endl; + } else { + cout << "------end detach agent for pid " << pid << "------" << endl; + } +} + +int start_attach_agent(int64_t pid) { + if (!inspector) { + return -1; + } + attach_agent(pid, true); + + return 0; +} + +int stop_attach_agent(int64_t pid) { + if (!inspector) { + return -1; + } + attach_agent(pid, false); + + return 0; +} + int start_profile() { if (!inspector) { return -1; diff --git a/probe/src/cgo/kindling.h b/probe/src/cgo/kindling.h index 27e1aa825..32117b65d 100644 --- a/probe/src/cgo/kindling.h +++ b/probe/src/cgo/kindling.h @@ -42,6 +42,12 @@ void print_profile_debug_info(sinsp_evt* sevt); void attach_pid(char* pid, bool is_new_start, bool is_attach, bool is_all_attach, bool is_ps); +int start_attach_agent(int64_t pid); + +int stop_attach_agent(int64_t pid); + +void attach_agent(int64_t pid, bool is_attach); + void get_capture_statistics(); uint16_t get_protocol(scap_l4_proto proto); From cd448b6263a0df5408117e07674e8bce9d0c8188 Mon Sep 17 00:00:00 2001 From: huxiangyuan Date: Tue, 17 Jan 2023 15:25:59 +0800 Subject: [PATCH 2/5] Add error message for attach agent api Signed-off-by: huxiangyuan --- .../component/controller/profile_module.go | 28 ++++++++--------- .../component/receiver/cgoreceiver/cgo_func.h | 4 +-- probe/src/cgo/cgo_func.cpp | 4 +-- probe/src/cgo/cgo_func.h | 4 +-- probe/src/cgo/kindling.cpp | 30 ++++++++++--------- probe/src/cgo/kindling.h | 6 ++-- 6 files changed, 39 insertions(+), 37 deletions(-) diff --git a/collector/pkg/component/controller/profile_module.go b/collector/pkg/component/controller/profile_module.go index b6ec4dc62..0d088f952 100644 --- a/collector/pkg/component/controller/profile_module.go +++ b/collector/pkg/component/controller/profile_module.go @@ -88,20 +88,20 @@ func (p *Profile) GetModuleKey() string { return p.Name() } -func startAttachAgent(pid int) error { - if C.startAttachAgent(C.int(pid)) == 0 { - return nil - } else { - return fmt.Errorf("start Attach Agent failed") +func startAttachAgent(pid int) string { + errorMsg := C.GoString(C.startAttachAgent(C.int(pid))) + if len(errorMsg) > 0 { + return errorMsg } + return "" } -func stopAttachAgent(pid int) error { - if C.stopAttachAgent(C.int(pid)) == 0 { - return nil - } else { - return fmt.Errorf("stop Attach Agent failed") +func stopAttachAgent(pid int) string { + errorMsg := C.GoString(C.stopAttachAgent(C.int(pid))) + if len(errorMsg) > 0 { + return errorMsg } + return "" } func startDebug(pid int, tid int) error { @@ -140,10 +140,10 @@ func (p *Profile) HandRequest(req *ControlRequest) *ControlResponse { Msg: "stop success", } case "start_attach_agent": - if err := startAttachAgent(req.Pid); err != nil { + if errMsg := startAttachAgent(req.Pid); errMsg != "" { return &ControlResponse{ Code: StartWithError, - Msg: err.Error(), + Msg: errMsg, } } return &ControlResponse{ @@ -151,10 +151,10 @@ func (p *Profile) HandRequest(req *ControlRequest) *ControlResponse { Msg: "start success", } case "stop_attach_agent": - if err := stopAttachAgent(req.Pid); err != nil { + if errMsg := stopAttachAgent(req.Pid); errMsg != "" { return &ControlResponse{ Code: StopWithError, - Msg: err.Error(), + Msg: errMsg, } } return &ControlResponse{ diff --git a/collector/pkg/component/receiver/cgoreceiver/cgo_func.h b/collector/pkg/component/receiver/cgoreceiver/cgo_func.h index 16de5f2c3..ab3be155a 100644 --- a/collector/pkg/component/receiver/cgoreceiver/cgo_func.h +++ b/collector/pkg/component/receiver/cgoreceiver/cgo_func.h @@ -13,8 +13,8 @@ int getKindlingEvent(void **kindlingEvent); int subEventForGo(char* eventName, char* category, void *params); int startProfile(); int stopProfile(); -int startAttachAgent(int pid); -int stopAttachAgent(int pid); +char* startAttachAgent(int pid); +char* stopAttachAgent(int pid); void startProfileDebug(int pid, int tid); void stopProfileDebug(); void getCaptureStatistics(); diff --git a/probe/src/cgo/cgo_func.cpp b/probe/src/cgo/cgo_func.cpp index 59f8fc6f5..69f7f1123 100644 --- a/probe/src/cgo/cgo_func.cpp +++ b/probe/src/cgo/cgo_func.cpp @@ -14,8 +14,8 @@ int getKindlingEvent(void** kindlingEvent) { return getEvent(kindlingEvent); } int startProfile() { return start_profile(); } int stopProfile() { return stop_profile(); } -int startAttachAgent(int pid) { return start_attach_agent(pid); } -int stopAttachAgent(int pid) { return stop_attach_agent(pid); } +char* startAttachAgent(int pid) { return start_attach_agent(pid); } +char* stopAttachAgent(int pid) { return stop_attach_agent(pid); } void subEventForGo(char* eventName, char* category, void *params) { sub_event(eventName, category, (event_params_for_subscribe *)params); } void startProfileDebug(int pid, int tid) { start_profile_debug(pid, tid); } diff --git a/probe/src/cgo/cgo_func.h b/probe/src/cgo/cgo_func.h index 4406ad445..dfb6ded65 100644 --- a/probe/src/cgo/cgo_func.h +++ b/probe/src/cgo/cgo_func.h @@ -13,8 +13,8 @@ int getKindlingEvent(void** kindlingEvent); void subEventForGo(char* eventName, char* category, void* params); int startProfile(); int stopProfile(); -int startAttachAgent(int pid); -int stopAttachAgent(int pid); +char* startAttachAgent(int pid); +char* stopAttachAgent(int pid); void startProfileDebug(int pid, int tid); void stopProfileDebug(); void getCaptureStatistics(); diff --git a/probe/src/cgo/kindling.cpp b/probe/src/cgo/kindling.cpp index f9a1e18a9..e05b56a42 100644 --- a/probe/src/cgo/kindling.cpp +++ b/probe/src/cgo/kindling.cpp @@ -918,8 +918,8 @@ void attach_pid(char* pid, bool is_new_start, bool is_attach, bool is_all_attach } } -void attach_agent(int64_t pid, bool is_attach) { - char result_buf[1024], command[1024]; +char* attach_agent(int64_t pid, bool is_attach) { + char result_buf[1024], command[1024], error_message[1024]; string attach_command_prefix; if (is_attach) { attach_command_prefix = "./async-profiler/jattach.sh start "; @@ -933,7 +933,7 @@ void attach_agent(int64_t pid, bool is_attach) { fp = popen(command, "r"); if (NULL == fp) { perror("popen execute failed!\n"); - return; + return "popen execute failed"; } if (is_attach) { cout << "------" @@ -943,17 +943,22 @@ void attach_agent(int64_t pid, bool is_attach) { << " start detach agent for pid " << pid << "------" << endl; } + char* error_msg; while (fgets(result_buf, sizeof(result_buf), fp) != NULL) { if ('\n' == result_buf[strlen(result_buf) - 1]) { result_buf[strlen(result_buf) - 1] = '\0'; } + error_msg = strstr(result_buf, "[ERROR] "); + if (error_msg) { + strcpy(error_message, error_msg); + } printf("%s\r\n", result_buf); } int rc = pclose(fp); if (-1 == rc) { perror("close command fp failed!\n"); - exit(1); + return "close command fp failed"; } else { printf("command:【%s】command process status:【%d】command return value:【%d】\r\n", command, rc, WEXITSTATUS(rc)); @@ -964,24 +969,21 @@ void attach_agent(int64_t pid, bool is_attach) { } else { cout << "------end detach agent for pid " << pid << "------" << endl; } + return error_message; } -int start_attach_agent(int64_t pid) { +char* start_attach_agent(int64_t pid) { if (!inspector) { - return -1; + return "Please start profile first"; } - attach_agent(pid, true); - - return 0; + return attach_agent(pid, true); } -int stop_attach_agent(int64_t pid) { +char* stop_attach_agent(int64_t pid) { if (!inspector) { - return -1; + return "Please start profile first"; } - attach_agent(pid, false); - - return 0; + return attach_agent(pid, false); } int start_profile() { diff --git a/probe/src/cgo/kindling.h b/probe/src/cgo/kindling.h index 32117b65d..3dfe5c13a 100644 --- a/probe/src/cgo/kindling.h +++ b/probe/src/cgo/kindling.h @@ -42,11 +42,11 @@ void print_profile_debug_info(sinsp_evt* sevt); void attach_pid(char* pid, bool is_new_start, bool is_attach, bool is_all_attach, bool is_ps); -int start_attach_agent(int64_t pid); +char* start_attach_agent(int64_t pid); -int stop_attach_agent(int64_t pid); +char* stop_attach_agent(int64_t pid); -void attach_agent(int64_t pid, bool is_attach); +char* attach_agent(int64_t pid, bool is_attach); void get_capture_statistics(); From fd17cbe593a9f4eb537ac81b7f078951890766e2 Mon Sep 17 00:00:00 2001 From: huxiangyuan Date: Tue, 17 Jan 2023 15:34:00 +0800 Subject: [PATCH 3/5] Add ChangeLog Signed-off-by: huxiangyuan --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2082dd44..c01b70e4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ## Unreleased ### New features +- Support Attach Agent for NoAPM Java Application. ([#431](https://github.com/KindlingProject/kindling/pull/431)) ### Enhancements - When using the file writer in `cameraexporter`, we rotate files in chronological order now and rotate half of files one time. ([#420](https://github.com/KindlingProject/kindling/pull/420)) From 8f33102d7a3346ac0325d2d3c3eb584f800159aa Mon Sep 17 00:00:00 2001 From: huxiangyuan Date: Tue, 17 Jan 2023 16:59:02 +0800 Subject: [PATCH 4/5] Fix Check failure by stack-allocated memory Signed-off-by: huxiangyuan --- probe/src/cgo/kindling.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/probe/src/cgo/kindling.cpp b/probe/src/cgo/kindling.cpp index e05b56a42..fe99325cf 100644 --- a/probe/src/cgo/kindling.cpp +++ b/probe/src/cgo/kindling.cpp @@ -919,7 +919,7 @@ void attach_pid(char* pid, bool is_new_start, bool is_attach, bool is_all_attach } char* attach_agent(int64_t pid, bool is_attach) { - char result_buf[1024], command[1024], error_message[1024]; + char result_buf[1024], command[1024]; string attach_command_prefix; if (is_attach) { attach_command_prefix = "./async-profiler/jattach.sh start "; @@ -943,6 +943,7 @@ char* attach_agent(int64_t pid, bool is_attach) { << " start detach agent for pid " << pid << "------" << endl; } + string error_message = ""; char* error_msg; while (fgets(result_buf, sizeof(result_buf), fp) != NULL) { if ('\n' == result_buf[strlen(result_buf) - 1]) { @@ -950,7 +951,7 @@ char* attach_agent(int64_t pid, bool is_attach) { } error_msg = strstr(result_buf, "[ERROR] "); if (error_msg) { - strcpy(error_message, error_msg); + error_message.append(error_msg); } printf("%s\r\n", result_buf); } @@ -969,7 +970,7 @@ char* attach_agent(int64_t pid, bool is_attach) { } else { cout << "------end detach agent for pid " << pid << "------" << endl; } - return error_message; + return (char*) error_message.data(); } char* start_attach_agent(int64_t pid) { From 6667b3585142a75365fefec4803bef58e9b7cb05 Mon Sep 17 00:00:00 2001 From: huxiangyuan Date: Wed, 18 Jan 2023 10:35:09 +0800 Subject: [PATCH 5/5] Use malloc/free for char* in cgo function Signed-off-by: huxiangyuan --- .../component/controller/profile_module.go | 9 ++++-- probe/src/cgo/kindling.cpp | 28 ++++++++++++------- probe/src/cgo/kindling.h | 2 +- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/collector/pkg/component/controller/profile_module.go b/collector/pkg/component/controller/profile_module.go index 0d088f952..71d283346 100644 --- a/collector/pkg/component/controller/profile_module.go +++ b/collector/pkg/component/controller/profile_module.go @@ -14,6 +14,7 @@ import ( "errors" "fmt" "time" + "unsafe" "github.com/Kindling-project/kindling/collector/pkg/component" ) @@ -89,7 +90,9 @@ func (p *Profile) GetModuleKey() string { } func startAttachAgent(pid int) string { - errorMsg := C.GoString(C.startAttachAgent(C.int(pid))) + result := C.startAttachAgent(C.int(pid)) + errorMsg := C.GoString(result) + C.free(unsafe.Pointer(result)) if len(errorMsg) > 0 { return errorMsg } @@ -97,7 +100,9 @@ func startAttachAgent(pid int) string { } func stopAttachAgent(pid int) string { - errorMsg := C.GoString(C.stopAttachAgent(C.int(pid))) + result := C.stopAttachAgent(C.int(pid)) + errorMsg := C.GoString(result) + C.free(unsafe.Pointer(result)) if len(errorMsg) > 0 { return errorMsg } diff --git a/probe/src/cgo/kindling.cpp b/probe/src/cgo/kindling.cpp index fe99325cf..72ab5df44 100644 --- a/probe/src/cgo/kindling.cpp +++ b/probe/src/cgo/kindling.cpp @@ -918,7 +918,7 @@ void attach_pid(char* pid, bool is_new_start, bool is_attach, bool is_all_attach } } -char* attach_agent(int64_t pid, bool is_attach) { +void attach_agent(int64_t pid, char* error_message, bool is_attach) { char result_buf[1024], command[1024]; string attach_command_prefix; if (is_attach) { @@ -933,7 +933,8 @@ char* attach_agent(int64_t pid, bool is_attach) { fp = popen(command, "r"); if (NULL == fp) { perror("popen execute failed!\n"); - return "popen execute failed"; + strcpy(error_message, "popen execute failed"); + return; } if (is_attach) { cout << "------" @@ -943,7 +944,6 @@ char* attach_agent(int64_t pid, bool is_attach) { << " start detach agent for pid " << pid << "------" << endl; } - string error_message = ""; char* error_msg; while (fgets(result_buf, sizeof(result_buf), fp) != NULL) { if ('\n' == result_buf[strlen(result_buf) - 1]) { @@ -951,7 +951,7 @@ char* attach_agent(int64_t pid, bool is_attach) { } error_msg = strstr(result_buf, "[ERROR] "); if (error_msg) { - error_message.append(error_msg); + strcpy(error_message, error_msg); } printf("%s\r\n", result_buf); } @@ -959,7 +959,8 @@ char* attach_agent(int64_t pid, bool is_attach) { int rc = pclose(fp); if (-1 == rc) { perror("close command fp failed!\n"); - return "close command fp failed"; + strcpy(error_message, "close command fp failed"); + return; } else { printf("command:【%s】command process status:【%d】command return value:【%d】\r\n", command, rc, WEXITSTATUS(rc)); @@ -970,21 +971,28 @@ char* attach_agent(int64_t pid, bool is_attach) { } else { cout << "------end detach agent for pid " << pid << "------" << endl; } - return (char*) error_message.data(); } char* start_attach_agent(int64_t pid) { + char* error_message = (char*) malloc(1024 * sizeof(char)); + error_message[0] = '\0'; if (!inspector) { - return "Please start profile first"; + strcpy(error_message, "Please start profile first"); + } else { + attach_agent(pid, error_message, true); } - return attach_agent(pid, true); + return error_message; } char* stop_attach_agent(int64_t pid) { + char* error_message = (char*) malloc(1024 * sizeof(char)); + error_message[0] = '\0'; if (!inspector) { - return "Please start profile first"; + strcpy(error_message, "Please start profile first"); + } else { + attach_agent(pid, error_message, false); } - return attach_agent(pid, false); + return error_message; } int start_profile() { diff --git a/probe/src/cgo/kindling.h b/probe/src/cgo/kindling.h index 3dfe5c13a..6ca4fec37 100644 --- a/probe/src/cgo/kindling.h +++ b/probe/src/cgo/kindling.h @@ -46,7 +46,7 @@ char* start_attach_agent(int64_t pid); char* stop_attach_agent(int64_t pid); -char* attach_agent(int64_t pid, bool is_attach); +void attach_agent(int64_t pid, char* error_message, bool is_attach); void get_capture_statistics();