Home php教程 PHP开发 PHP kernel analysis (4)-do_cli

PHP kernel analysis (4)-do_cli

Dec 19, 2016 am 11:12 AM

Abstract: The php version read here is PHP-7.1.0 RC3, and the platform for reading the code is linux# main has added comments to the remaining code and posted them all (this is the simplified main function, with some irrelevant information removed Important code segment): int main(int argc, char *argv) { ... sapi_modu ...

The php version read here is PHP-7.1.0 RC3, and the platform for reading the code is linux

# main

Added comments to the rest of the code and posted it all (this is the simplified main function, with some irrelevant code segments removed):

01    int main(int argc, char *argv[])    
02    {    
03        ...    
04        sapi_module_struct *sapi_module = &cli_sapi_module;    
05    
06        argv = save_ps_args(argc, argv); //这里获取一次当前执行进程的参数,环境变量等。为的是对特定平台,修正下argv变量以供后续使用。    
07    
08        cli_sapi_module.additional_functions = additional_functions; // cli模式特有的函数    
09    
10         ...    
11    
12    
13    #ifdef ZTS    
14        tsrm_startup(1, 1, 0, NULL);    
15        (void)ts_resource(0);    
16        ZEND_TSRMLS_CACHE_UPDATE();    
17    #endif    
18    
19        zend_signal_startup();  // 设置信号,把一些需要反应的信号位设置为0    
20    
21        // 获取参数,做一些对应的初始化行为,或者一些简单的操作,比如help    
22        while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {    
23            switch (c) { // 这里的c是代表返回的字符串的ascii码值    
24                case 'c':    
25                    ...    
26                case 'n':    
27                    ini_ignore = 1; // 不使用ini文件,通过代码或者其他指定ini值    
28                    break;    
29                case 'd': { // 配置ini的key,val值在命令行中,下面的行为都是修改ini_entries这个变量    
30                    ...    
31                }    
32                case 'h': /* help & quit */    
33                case '?':    
34                    php_cli_usage(argv[0]);    
35                    goto out;    
36                case 'i': case 'v': case 'm':    
37                    sapi_module = &cli_sapi_module;    
38                    goto exit_loop;    
39                case 'e': /* enable extended info output */    
40                    use_extended_info = 1;    
41                    break;    
42            }    
43        }    
44    exit_loop:    
45    
46        sapi_module->ini_defaults = sapi_cli_ini_defaults; // 设置初始化的ini值    
47        sapi_module->php_ini_path_override = ini_path_override; //设置重写后的ini_path地址,如果是php -c的话,这个就为非null    
48        sapi_module->phpinfo_as_text = 1; // 打开打印phpinfo的开关,需要的时候可以把phpinfo打印出来    
49        sapi_module->php_ini_ignore_cwd = 1; // 不在当前路径寻找php.ini    
50        sapi_startup(sapi_module); // sapi初始化行为,比如初始化全局变量SG    
51        sapi_started = 1; // 标记,表示已经调用了startup,关闭的时候需要调用shundown    
52        ...    
53    
54        // 开始调用sapi的startup方法,对cli模式,实际上是调用php_cli_startup方法    
55        if (sapi_module->startup(sapi_module) == FAILURE) {    
56            exit_status = 1;    
57            goto out;    
58        }    
59        module_started = 1; // 标记位,标记已经调用了module的startup方法    
60    
61        ...    
62    
63        zend_first_try {    
64                exit_status = do_cli(argc, argv);  // 这个是实际上调用的内容    
65        } zend_end_try();    
66    out:  // 这个代码段已经是要退出了    
67        if (ini_path_override) {    
68            free(ini_path_override);    
69        }    
70        if (ini_entries) {    
71            free(ini_entries);    
72        }    
73        if (module_started) {    
74            php_module_shutdown();    
75        }    
76        if (sapi_started) {    
77            sapi_shutdown();    
78        }    
79    #ifdef ZTS    
80        tsrm_shutdown();    
81    #endif    
82    
83        cleanup_ps_args(argv);    
84        exit(exit_status);    
85    }
Copy after login

In fact, it is very simple to look at the pseudo code:

1    tsrm_startup(1, 1, 0, NULL);  // TSM启动    
2    zend_signal_startup();  // 信号设置    
3    sapi_startup(sapi_module);  // SAPI启动    
4    sapi_module->startup(sapi_module); // 当前模块的startup    
5    do_cli(argc, argv); // 做实际的行为    
6    php_module_shutdown();  // 当前模块的shutdown    
7    sapi_shutdown(); // SAPI关闭    
8    tsrm_shutdown(); // TSM关闭
Copy after login

Okay, actually After looking around, the most important function in it is do_cli.

php parameters

In do_cli you will see that there are many branches according to different parameters. Here you need to understand what these parameters are used for.


Parameter
Function
Example

PHP kernel analysis (4)-do_cli

do_cli

We remove the redundant code from the entire function of do_cli function, leaving only the key code as follows:

001    static int do_cli(int argc, char **argv)    
002    {    
003        ...    
004    
005        zend_try {    
006    
007            // 这里处理了 i-输出phpinfo内容/ v-输出php版本 / m-输出扩展信息    
008            while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {    
009                switch (c) {    
010    
011                case 'i': // 输出phpinfo内容    
012                    ...    
013                    php_print_info(0xFFFFFFFF);    
014                    ...    
015                    goto out;    
016    
017                case 'v': // 输出php版本信息    
018                    ...    
019                        get_zend_version()    
020                    ...    
021                    goto out;    
022    
023                case 'm': // 列出所有模块    
024                    ...    
025                    print_extensions();    
026                    ...    
027                    goto out;    
028    
029                default:    
030                    break;    
031                }    
032            }    
033    
034            ...    
035    
036            // 下面的代码做了几个事情:    
037            // 1 根据参数设置了behavior参数    
038            // 2 有执行文件的就将文件存在script_file    
039            while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {    
040                switch (c) {    
041    
042                case 'a': // php的交互模式    
043                    ...    
044                    interactive=1;    
045                    ...    
046                    break;    
047    
048                case 'C': // 不要把cwd目录变成脚本所在的目录。这个默认就是cwd是当前执行路径,所以这里什么都不做。    
049                    break;    
050    
051                case &#39;F&#39;: // php -F <file> 进入交互模式,每执行一行就执行一次<file>文件    
052                    ...    
053                    behavior=PHP_MODE_PROCESS_STDIN;    
054                    script_file = php_optarg;    
055                    break;    
056    
057                case &#39;f&#39;: // php -f <file> 解析并执行文件    
058                    ...    
059                    script_file = php_optarg;    
060                    break;    
061    
062                case &#39;l&#39;:  // 检查文件的语法是否有错误    
063                    ...    
064                    behavior=PHP_MODE_LINT;    
065                    break;    
066    
067                case &#39;q&#39;: // 安静模式,默认也是安静模式    
068                    break;    
069    
070                case &#39;r&#39;: // 从命令行直接执行脚本    
071                    ...    
072                    behavior=PHP_MODE_CLI_DIRECT;    
073                    exec_direct=php_optarg;    
074                    break;    
075    
076                case &#39;R&#39;: // 每行输入的时候执行一次code脚本,比如 php -R &#39;echo 12;&#39;    
077                    ...    
078                    behavior=PHP_MODE_PROCESS_STDIN;    
079                    exec_run=php_optarg;    
080                    break;    
081    
082                case &#39;B&#39;: // 在每次输入开始之前执行一次code脚本    
083                    ...    
084                    behavior=PHP_MODE_PROCESS_STDIN;    
085                    exec_begin=php_optarg;    
086                    break;    
087    
088                case &#39;E&#39;: // 在每次输入结束之后执行一次code脚本, 上面的 RBE可以参考一个例子:find conf.d | php -B &#39;$l=0;&#39; -R &#39;$l += count(@file($argn));&#39; -E &#39;echo "Total Lines: $l\n";&#39;    
089                    ...    
090                    behavior=PHP_MODE_PROCESS_STDIN;    
091                    exec_end=php_optarg;    
092                    break;    
093    
094                case &#39;s&#39;: // 使用html高亮方式显示代码,这个或许在一些代码显示的时候需要用到    
095                    ...    
096                    behavior=PHP_MODE_HIGHLIGHT;    
097                    break;    
098    
099                case &#39;w&#39;:  // php <file> -w 能把<file>中的评论和多余的空格去掉    
100                    ...    
101                    behavior=PHP_MODE_STRIP;    
102                    break;    
103    
104                case &#39;z&#39;: // 加载外部扩展    
105                    zend_load_extension(php_optarg);    
106                    break;    
107                case &#39;H&#39;: // 隐藏所有参数    
108                    hide_argv = 1;    
109                    break;    
110                case 10: // 显示function定义    
111                    behavior=PHP_MODE_REFLECTION_FUNCTION;    
112                    reflection_what = php_optarg;    
113                    break;    
114                case 11: // 显示class定义    
115                    behavior=PHP_MODE_REFLECTION_CLASS;    
116                    reflection_what = php_optarg;    
117                    break;    
118                case 12: // 显示扩展定义,注意这里是php扩展    
119                    behavior=PHP_MODE_REFLECTION_EXTENSION;    
120                    reflection_what = php_optarg;    
121                    break;    
122                case 13: // 显示zend扩展定义, 比如xdebug    
123                    behavior=PHP_MODE_REFLECTION_ZEND_EXTENSION;    
124                    reflection_what = php_optarg;    
125                    break;    
126                case 14: // 显示扩展的对应配置    
127                    behavior=PHP_MODE_REFLECTION_EXT_INFO;    
128                    reflection_what = php_optarg;    
129                    break;    
130                case 15: // 显示ini配置    
131                    behavior = PHP_MODE_SHOW_INI_CONFIG;    
132                    break;    
133                default:    
134                    break;    
135                }    
136            }    
137    
138            ...    
139    
140            // 初始化request之后,执行了request_startup    
141            if (php_request_startup()==FAILURE) {    
142                ...    
143                goto err;    
144            }    
145            ...    
146    
147            zend_is_auto_global_str(ZEND_STRL("_SERVER"));    
148    
149            // 根据不同的行为做不同的具体操作,这个是核心方法    
150            switch (behavior) {    
151            case PHP_MODE_STANDARD:  // 标准,就是执行一个脚本文件    
152                ...    
153                    php_execute_script(&file_handle);    
154                ...    
155                break;    
156            case PHP_MODE_LINT: // 只检查文件有没有语法错误    
157                exit_status = php_lint_script(&file_handle);    
158                ...    
159                break;    
160            case PHP_MODE_STRIP:    
161                ...    
162                    zend_strip();    
163                ...    
164                break;    
165            case PHP_MODE_HIGHLIGHT:    
166                ...    
167                php_get_highlight_struct(&syntax_highlighter_ini);    
168                zend_highlight(&syntax_highlighter_ini);    
169                goto out;    
170                break;    
171            case PHP_MODE_CLI_DIRECT:    
172                ...    
173                if (zend_eval_string_ex(exec_direct, NULL, "Command line code", 1) == FAILURE) {    
174                    exit_status=254;    
175                }    
176                break;    
177    
178            case PHP_MODE_PROCESS_STDIN:    
179                    ...    
180                    zend_eval_string_ex(exec_end, NULL, "Command line end code", 1)    
181                    ...    
182                    break;    
183            case PHP_MODE_REFLECTION_FUNCTION:    
184            case PHP_MODE_REFLECTION_CLASS:    
185            case PHP_MODE_REFLECTION_EXTENSION:    
186            case PHP_MODE_REFLECTION_ZEND_EXTENSION:    
187                ...    
188                ZVAL_STRING(&arg, reflection_what);    
189                object_init_ex(&ref, pce);    
190                ...    
191                zend_call_method_with_1_params(&ref, pce, &pce->constructor, "__construct", NULL, &arg);    
192                ...    
193                break;    
194            case PHP_MODE_REFLECTION_EXT_INFO:    
195                ...    
196                if ((module = zend_hash_str_find_ptr(&module_registry, lcname, len)) == NULL) {    
197                    ...    
198                        display_ini_entries(NULL);    
199                    ...    
200                }    
201                ...    
202                break;    
203            case PHP_MODE_SHOW_INI_CONFIG:    
204                ...    
205                break;    
206            }    
207        } zend_end_try();    
208    
209    out:    
210        ...    
211    err:    
212        ...    
213    }</file></file></file></file></file>
Copy after login

The entire 200 lines of code is easy to understand, the entire It is wrapped in a zend_try...zend_catch. Did a few steps:

Process the -i, -m, -v parameters

Set behavior, script_file and other variables for other parameters

Do different behaviors based on behavior

Back to our initial plan, we want to understand Things:

Our search is based on the -r parameter configuration.


It actually calls

zend_eval_string_ex(exec_direct, NULL, "Command line code", 1)
Copy after login

The exec_direct here is the echo 12 string

The above is the content of php kernel analysis (4)-do_cli. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

See all articles