You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2023/01/18 01:17:23 UTC

[skywalking-php] branch master updated: Hack dtor for mysqli. (#45)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-php.git


The following commit(s) were added to refs/heads/master by this push:
     new 9d76bc3  Hack dtor for mysqli. (#45)
9d76bc3 is described below

commit 9d76bc3282faa54ceee27f1e1020dd12340112f3
Author: jmjoy <jm...@apache.org>
AuthorDate: Wed Jan 18 09:17:18 2023 +0800

    Hack dtor for mysqli. (#45)
---
 src/plugin/plugin_mysqli.rs | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/plugin/plugin_mysqli.rs b/src/plugin/plugin_mysqli.rs
index d47f1a0..9be5d8b 100644
--- a/src/plugin/plugin_mysqli.rs
+++ b/src/plugin/plugin_mysqli.rs
@@ -22,19 +22,24 @@ use crate::{
 use anyhow::Context;
 use dashmap::DashMap;
 use once_cell::sync::Lazy;
+use phper::{objects::ZObj, sys};
 use skywalking::{skywalking_proto::v3::SpanLayer, trace::span::Span};
 use tracing::debug;
 
 static MYSQL_MAP: Lazy<DashMap<u32, MySQLInfo>> = Lazy::new(Default::default);
 
+static DTOR_MAP: Lazy<DashMap<u32, sys::zend_object_dtor_obj_t>> = Lazy::new(Default::default);
+
 #[derive(Default, Clone)]
 pub struct MySQLImprovedPlugin;
 
 impl Plugin for MySQLImprovedPlugin {
+    #[inline]
     fn class_names(&self) -> Option<&'static [&'static str]> {
         Some(&["mysqli"])
     }
 
+    #[inline]
     fn function_name_prefix(&self) -> Option<&'static str> {
         None
     }
@@ -58,6 +63,8 @@ impl MySQLImprovedPlugin {
             Box::new(|_, execute_data| {
                 let this = get_this_mut(execute_data)?;
                 let handle = this.handle();
+                hack_dtor(this, Some(mysqli_dtor));
+
                 let mut info: MySQLInfo = MySQLInfo {
                     hostname: "127.0.0.1".to_string(),
                     port: 3306,
@@ -98,7 +105,7 @@ impl MySQLImprovedPlugin {
                 let this = get_this_mut(execute_data)?;
                 let handle = this.handle();
 
-                debug!(handle, function_name, "call mysql method");
+                debug!(handle, function_name, "call mysqli method");
 
                 let mut span = with_info(handle, |info| {
                     create_mysqli_exit_span(request_id, "mysqli", &function_name, info)
@@ -145,3 +152,27 @@ struct MySQLInfo {
     hostname: String,
     port: i64,
 }
+
+fn hack_dtor(this: &mut ZObj, new_dtor: sys::zend_object_dtor_obj_t) {
+    let handle = this.handle();
+
+    unsafe {
+        let ori_dtor = (*(*this.as_mut_ptr()).handlers).dtor_obj;
+        DTOR_MAP.insert(handle, ori_dtor);
+        (*((*this.as_mut_ptr()).handlers as *mut sys::zend_object_handlers)).dtor_obj = new_dtor;
+    }
+}
+
+unsafe extern "C" fn mysqli_dtor(object: *mut sys::zend_object) {
+    debug!("call mysqli dtor");
+    dtor(object);
+}
+
+unsafe extern "C" fn dtor(object: *mut sys::zend_object) {
+    let handle = ZObj::from_ptr(object).handle();
+
+    MYSQL_MAP.remove(&handle);
+    if let Some((_, Some(dtor))) = DTOR_MAP.remove(&handle) {
+        dtor(object);
+    }
+}