Debugging WINCACHE on Windows using Visual Studio – Important Breakpoints

Previous Post

In my last post I explained basic nuances of debugging WINCACHE using Visual Studio. Let’s move ahead. A general question while debugging any application is, “Can someone tell me what are the important points in the code where I should set breakpoints which will help me in understanding the code easily and better?”. I am going to answer this in the blog post today.

The most important file for WINCACHE is named “php_wincache.c” and so the corresponding header file which is named “php_wincache.h”. These files will help you understanding most of the core pieces including:

  • How WINCACHE INI directives work and is implemented?
  • How WINCACHE gets initialized?
  • What are the different external API which WINCACHE provides which can be called from PHP scripts and how are they implemented?
  • How WINCACHE cleans itself?

And to top it all, once you understand these things, you will know basic framework to write your own extension. Yes, that’s true. These two files will give you the basic idea of framework you will need to create your own extension.

Let’s look at the questions one  by one.

Implementation of WINCACHE directives

Take the directive ‘enablecli’ and see how it is implemented and this will apply to all other directives. You will need following things:

  • Define it as a valid INI directive

In order to create WINCACHE own INI directive, one will need to create a new entry between macros PHP_INI_BEGIN() and PHP_INI_END(). And one can see that in the file php_wincache.c ‘enablecli’ is defined within these macros. Look closely at the macro used STD_PHP_INI_BOOLEAN as the value of enablecli can be either 0 or 1 and all the arguments it takes. The first four arguments are worth mentioning here. They are in order of appearance directive name, directive value, directive’s right of getting modified and what function to call if the value gets changed. If you want to dig deeper all these macros are defined in file php_ini.h/zend_ini.h in the PHP source code.

  • Store the value somewhere

This has been defined in php_wincache.h within macro ZEND_BEGIN_MODULE_GLOBALS(wincache) and ZEND_END_MODULE_GLOBALS(wincache). This macro actually declares a structure (struct in C programming language) named zend_wincache_globals containing all the variables defined in between these two macros. Now go to source file php_wincache.c and one will find a line like ZEND_DECLARE_MODULE_GLOBALS(wincache). This is the macro which instantiates an object of the structure zend_wincache_globals as a true global if it is a non-thread-safe build or as a resource for this particular thread. However you should not pay too much attention to this. Just remember that the macro ZEND_DECLARE_MODULE_GLOBALS(wincache) instantiates the object of sructure zend_wincache_globals.

  • Use it where ever appropriate

This is pretty straight forward. Use the macro WCG(enablecli) to use it. Remember we just talked about creation of object of the struct zend_wincache_gloabls. This macro just points to corresponding enablecli variable within the object of this structure. Look in the code at all the different places it is used. Since this directive is used for enabling WINCACHE for PHP CLI, the value of this is being used as a check point to return from important WINCACHE function or continue.

  • Initializing the INI directives default value

PHP_MINIT_FUNCTION is the macro to look for in the file php_wncache.c. And BTW, didn’t this post was about setting important breakpoints. Yes set a breakpoint within this. This is the stepping stone for understanding how WINCACHE gets initialized properly. Within this, one can find another macro called ZEND_INIT_MODULE_GLOBALS. This sets up for WINCACHE, function which will do global initialization and termination. Global initialization function is where you initialize the WINCACHE INI directives with default value. For enablecli one can see it is ‘0’ meaning it is disabled by default.

This is it. You know now how to add a new WINCACHE INI directive.

I hope this will help you in debugging or adding new WINCACHE INI directive. But I promised to talk about all important break points. Well this is a series of blog posts and today you learnt setting one breakpoint and debugging WINCACHE directive. Believe me this one is the most important breakpoint to start debugging. So start debugging and I will explain more in next post. And for those who are not aware, WINCACHE sources can be found at http://pecl.php.net/package/WinCache.

Thanks for the patient reading and till we meet again good bye and ‘Shabba Khair’.

Don.

Next Post

No Comments