Skip to content

Instantly share code, notes, and snippets.

@asgrim
Last active April 14, 2021 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asgrim/2ffec166dfc4c1af9b21ae94f7100a8a to your computer and use it in GitHub Desktop.
Save asgrim/2ffec166dfc4c1af9b21ae94f7100a8a to your computer and use it in GitHub Desktop.
<?php
class Foo {
public function x() {
sleep(1);
}
}
add_monitoring();
$foo = new Foo();
$foo->x();
ZEND_NAMED_FUNCTION(my_custom_handler)
{
// Never make it into here...
original_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
PHP_FUNCTION(add_monitoring)
{
zend_function *original_function;
int handler_index;
zend_class_entry *ce;
// https://www.phpinternalsbook.com/php7/extensions_design/hooks.html#overwriting-an-internal-function
ce = zend_hash_str_find_ptr(EG(class_table), "foo", sizeof("foo") - 1);
original_function = zend_hash_str_find_ptr(&ce->function_table, "x", sizeof("x")-1);
original_handler = original_function->internal_function.handler;
original_function->internal_function.handler = my_custom_handler;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment