search
HomeBackend DevelopmentPHP ProblemHow to customize the life cycle of PHP extension (3)

接着上篇来讲php生命周期。

php_request_startup

这个阶段和php_module_startup差不多,都是初始化工作,比php_module_startup简单很多,可以自己看下,重点来看下执行阶段

php_execute_script

用gdb看看调用栈,gdb ./php  

php_execute_script打断点,执行,在看下调用栈,

b php_execute_script
(gdb) r test.php
bt
#0  php_execute_script (primary_file=0x7fffffffe240)
    at /www/test/php/php-7.4.3/main/main.c:2541
#1  0x00000000008bbd85 in do_cli (argc=2, argv=0x1425af0)
    at /www/test/php/php-7.4.3/sapi/cli/php_cli.c:961
#2  0x00000000008bcd2d in main (argc=2, argv=0x1425af0)
    at /www/test/php/php-7.4.3/sapi/cli/php_cli.c:1356

在调用栈可以清楚看到执行流程,现在到/cli/php_cli.c文件看看做了哪些事情,

int c;
    zend_file_handle file_handle;
    int behavior = PHP_MODE_STANDARD;
    char *reflection_what = NULL;
    volatile int request_started = 0;
    volatile int exit_status = 0;
    char *php_optarg = NULL, *orig_optarg = NULL;
    int php_optind = 1, orig_optind = 1;
    char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL;
    char *arg_free=NULL, **arg_excp=&arg_free;
    char *script_file=NULL, *translated_path = NULL;
    int interactive=0;
    int lineno = 0;
    const char *param_error=NULL;
    int hide_argv = 0;

    zend_try {

        CG(in_compilation) = 0; /* not initialized but needed for several options */

        while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
            switch (c) {

初始化变量,解析命令到/main/main.c文件看看真正的执行阶段

PHPAPI int php_execute_script(zend_file_handle *primary_file)
{
    zend_file_handle *prepend_file_p, *append_file_p;
    zend_file_handle prepend_file = {{0}, NULL, NULL, 0, 0}, append_file = {{0}, NULL, NULL, 0, 0};
#if HAVE_BROKEN_GETCWD
    volatile int old_cwd_fd = -1;
#else
    char *old_cwd;
    ALLOCA_FLAG(use_heap)
#endif
    int retval = 0;

加载要执行的php文件,通过zend_compile_file进行词法分析 语法分析,生成AST,编译成op_array,也就是指令集,
我们看下指令集,

b zend_execute
c
bt
(gdb) p *op_array
$1 = {type = 2 '\002', arg_flags = "\000\000", fn_flags = 37748736, 
  function_name = 0x0, scope = 0x0, prototype = 0x0, num_args = 0, 
  required_num_args = 0, arg_info = 0x0, cache_size = 16, last_var = 2, 
  T = 4, last = 13, opcodes = 0x7ffff5e8b000, run_time_cache__ptr = 0x0, 
  static_variables_ptr__ptr = 0x7ffff5e78358, static_variables = 0x0, 
  vars = 0x7ffff5e790f0, refcount = 0x7ffff5e85000, last_live_range = 0, 
  last_try_catch = 0, live_range = 0x0, try_catch_array = 0x0, 
  filename = 0x7ffff5e583c0, line_start = 1, line_end = 12, 
  doc_comment = 0x0, last_literal = 5, literals = 0x7ffff5e8b1a0, 
  reserved = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}

然后放到zend虚拟机上执行zend_execute(op_array, retval);这这里可以看到真正执行的是这个zend_execute_ex(execute_data);,它是一个函数指针,我们可以更换它,知道这个,那么我们在写扩展时可以重新写个zend_execute_ex函数替换php默认的,我们自己的就可以做很多事情,像拦截php函数,做性能监控。

请求关闭阶段php_request_shutdown,模块关闭阶段php_module_shutdown这两个阶段主要是做变量销毁,现在我们知道了一个自定义扩展在生命周期里的怎么执行的。

推荐学习:《PHP视频教程

The above is the detailed content of How to customize the life cycle of PHP extension (3). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)