 Second talk here in the Henry Thomas room for today and our last talk before we break for lunch Let me introduce our next speaker. He is a PHP Internals expert author of X debug and an open street map and mapping enthusiast He currently works at MongoDB on the PHP and HH HVM drivers for MongoDB. Please welcome Derek Revans My name is always a tricky one to get right All right. Well, I don't have to really talk much about myself not right now because the introduction has been provided Just one words. I wrote a tool called XD bug. You might be familiar with that Some of the talk will be about that if you have more questions about it I'm happy to I'm happy to take questions all during the presentation. Just raise your hands Somebody is hopefully going to run to you with a microphone in that case So yes, I'm Derek and Dutch. I'm also British Salon a long story can do it later And I like maps and I like beer. I also like whiskey, but there's no icon in a uni code for whiskey And if you have questions or running commentary, then you stutter like All right. So today's agenda is we're going to talk about past present and future Looking at how people tended to debug or rather not debug How are we currently doing it? So I'm showing some things that most people end up doing it right now and then we're going to look at Yeah, what's in there in the future? Hopefully some good things all right, so pretty printing variables you have I Repeat-reverable you dump it out and if you how many of you still do this daily? Yeah, I mean it's still the quick way of doing it right you run Vardamp and then something shows up on your screen If you do it with just PHP You're going to have to use pre-tag around that so that it renders nicely in HTML or If you have xdbook installed you get slightly more interesting output like like here, so it will show you different data types in different colors it will Show you here When you have a reference back to your object It also allows you to limit the length of strings being sent Which is kind of handy if in case you have like a file loaded in a variable somewhere and you're dumping this out You don't want a megabyte of rubbish on your screen, right? So you get to limit that and there's lots of different settings in there, but xdbook is not the only tool that does that You need to remember to click outside of the frame All right, so a few settings Any time you do Vardamp in xdbook in the latest version You also get the file name and line number where you have rendered Vardamp, so that is new and You get to format how this URL looks like By setting making this setting which is xdbook dot file underscore link underscore format If you do that then it gets rendered whenever you whenever xdbooks output a file name and line number You get to click on it and it can open directly into your ID Which is great quite handy Lots of other things you get to limit there like how many How much data you show for a single variable how many nested array levels you get deep How many array elements or object properties you get and things like that But if you don't want it you can turn it off which is also handy in some cases like when you are having a Hx request and Jason comes back. You don't want php to come back there with html Because the browser are not going to be able to understand that so xdbooks Vardamp output doesn't look the nicest because I don't really know where to Where I can use certain html or CSS styles because I never know where in which context things get executed So helpers at combat frameworks are installable with other ways like through composer because the developer more Actively determines where the Vardamp is happening They can allow for more interesting CSS and JavaScript styles to go with that They found the light button finally and so this is a tool called Krumo Krumo You can install with composer by default. It doesn't really show anything So you need to click on those links and then it opens up Just like you'd expect it does do some interesting things as well Like if it detects that you have a timestamp Actually that light is not good because the contrast of the slides goes really bad here Somebody turn the light back off again, but I can hardly see it myself here Right so in the bottom it says protect the timestamp integer and then it has the timestamp integer and Krumo detects that is a timestamp and then will show you the time that goes with it Which is says it's October 2nd 2014 Other tools like the one on the next slides Browses is a tool called kint. Just do something very similar. I don't think it looks not nearly as nice I'll show you what it does further down It sees here that if you have a color string it also renders that color to go with it Interestingly the color is the same as the background of the slides, so that isn't particularly useful here But it doesn't do that But this however doesn't do is the timestamp So there's many variants of these tools, but none of them do all of those nice things So I would just hope they would get together and just sort it out at some point. Anyway Something in XD book, which is great if you have somebody else's code base and you don't know what it is doing Sometimes you get like notes and warnings and errors and There are being silenced with the at sign, right? So you get a whole lot of codes And it just suddenly stops working. You don't know why so XD books screen operator or its setting Allows you to disable the at operator That means that if you use any a code then it really doesn't do anything. Which is great story for myself many many years ago as where we were debugging an issue where The code was suddenly would work fine on the interweb server But it would stop and do nothing on a command line and we figured out that Yeah, we couldn't really quite figured out We tracked it down after several hours to be the at sign in front of my school connect But my school connect couldn't find a host or the my school servers down or something It would always throw a warning and then return false We check in the code for false to make sure that well, we handle the error cases, right? But what it doesn't cover is the my school connect function not existing Because that throws a fator and it instantly aborts. I have no idea why so we couldn't handle it Stream operator got written that afternoon or the code for this All right, something else I'll be looking at is some tracing and tracing is basically a way again If you get code that you've never seen before getting a good overview of what happens in there There is different types of traces that you can make an XD book you can make textual traces You can make parsable traces and HTML traces It is probably better to show that than to have a slide for it. So live demo time. Let's hope Ah, I've done this many times Of course the moment I say that's the the cable falls out of the product All right, so what I have done in my browser I have installed a browser extension I think it's called the easiest XD book There was an easy XD book and an easier XD book before that There's similar things also for For all the browsers for Chrome and for Safari itself, but just the Firefox one in here There's it gives you three icons. It's like enable debug with XD books You'll go to in a moment and there's also one for enabling tracing And we when we click that and reload the slide and let's hope I've given off offerings to the demo gods I should get a file. I will make this a bit bigger in a second So this file is just been created when opened as it takes some time Yeah, it seems to be the correct time whoo-hoo, sorry it shows you every function called as be made when Doing my slide request through the browser It shows you the time index in the first column It shows you the amount of memory and use in the second column and then with the different nested levels You get to see which function calls which function on which line which which arguments you can either Have things like they even shows you the return values of things like it shows here that SPL outload register returns true and With the arrows you can see which variables are being set and assigned if you go through this line by line You get to instantly understand what has happened in your code This format is very difficult for all the things to parse But there's a different format that makes it all tap separated It also adds the memory usage and time index for leaving the function as well And it's pretty trivial to write a tool or a quick script To go through this as a very simple profiling tool. You can easily infer How much memory of specific function is using or how much time they've been taken and things like that And it's 102 line script. It comes part of the XD book Download if you download it and you don't get it if you do it with Pekko install, but if you download the tarball You will see that All right, that's tracing it's a bit bigger again some new features in XD book 2 3 Are all requested features by people so The first one is halt level. You know that Whenever you get a warning or an error notice and sometimes you get those in like a for loop and in the for loop You're getting this error thrown a hundred thousand times because you have so many elements to loop over It would be sometimes be really handy that the moment you get a notice or a warning that it stops the execution of the script And that is what you get to configure with XD book dot halt underscore level And you give it all the notice types and warning types that you want XD book to hold your script on when you sit that set that It will then abort your script, which is really handy in some cases Similarly some naughty code in libraries that you don't write yourself most of the time They like messing with the error reporting and display error settings, right? I'm sure some of you have had that happen in the past I know I have and That is really annoying, especially if it is in a library that you don't really control yourself So another feature in XD book 2 3 is now These force error reporting and force display errors, so you can set these in PHP I and I or in an HD access file and The moment you set it you cannot override it in your script not with any set Not with other ways of doing it So it is really that value and there's no way you can change it Which means that libraries can't mess mess up and make your development experience worse Yeah, now this stuff there. Okay, and another thing is that? Looking at what's happened in your code after this happened is really nice thing to have like with a trace valve You can see everything that has happened, but it's even better if you can inspect your script Well, this actually running and With that XD book has a feature called live debugging Sometimes you see to refer to as remote debugging, but remote isn't really important here Basically what that means is your script runs you get to set breakpoints You get to look at what happens all the different variables as they are being set and changed There's many different IDs for that just a show of hands here who use a speech piece storm Who doesn't? Okay, so what what do the others use? Sublime Atom Koda, yeah, Vim. Yeah, I Think all of these except for maybe atom doesn't have an exebook plug-in Even if you're using e-max, that's all for that too You can say it's fine submit a feature request to the atom developers And From what I get our peachy storm is probably the most widely used one that also has one of the better debugging integrations Well worded if you ask me and I'm I don't work for that point So I'll be showing that instead of just having a slide because slides are boring and Life demos are fun. All right. Well, I say that's and then my doesn't that on so peachy storm on a slide But we'll show you that install I have two versions apparently It's the name of my machine. I can't help it Like I said, I like maps beer and whiskey, right? All my machines are named after Sources of alcohol except for the firewall. That's called water because it doesn't do anything useful Anyway So yeah, I I have to admit I know absolutely nothing about WordPress. So we'll discover that as we go along. This is a bit on a smaller side So I don't use peachy storm. So I've no idea how this actually works to make the font larger Hence people How do I do that? Anyway? few oh It does now. I'm wondering does the debugger work in this mode and we're going to find it out by the way soon All right, so I'm going to turn off the trace all that is important that you turn off the tracing because if you don't every single request is going to be traced making that your Temp directory is going to be full before you notice it Okay, so again this browse extension. I clicked as a icon Request, okay, of course Who is curious? I need to turn on the listening mode in the ID so it run. Oh Interesting. This is called down. Oh, that works actually pretty. Oh, this is awesome. I Need to click the start listening for PHP debug connections And the moment I do that it should listen to every request that comes in So I reload the slide and it says who I found WordPress, but I don't know where it is Apparently, I hadn't selected a project yet. So I got to select a project and then It works. Oh, yeah, even the debugger works. This is great. Thanks. I learned something here So I go to the debugger console is also interesting but not having the time for that today So in debugger, they didn't make the phones of this bigger. How annoying I Shall be report a feature request Right, so I thought I'd be debugging WordPress, but I reloaded the slides. So I'm debugging my presentation on set It doesn't matter. It doesn't matter, but you what you can do In the bottom here, you see where your current line of code is and if you step into the function by stepping You actually start executing the code. So step into or f7. I'm going to Compose this require vendor outload stuff and as I just do some magic But you see see here in the bottom Left that I can still go back to the previous stack frame as all so you get to switch and see the data for all of your Function calls at the moment There are no variable sets. So that's not particularly interesting. Well compose it. Yeah, skip over that Get loader. It does some things. I will skip out of this because it's not particularly interesting at the moment So step out steps out of your current function call back into the color function I'll do the ones more. Okay. This is a more interesting one where You can see here is that I know it's very difficult to even on my screen. It has two dots which means static So because this is a static function You can still see this as it was sort of a object You also get it if it is a object you get this because that is how you would refer to an object for statics You get the two Colons and that is something that I've picked you get to click that and Then in there. Well, you can see all the variables that are being set. So in this case, there's a variable called instance list Which doesn't do anything. It has nothing in there yet. So doing some more scaping stepping because I now set a New element in an array with the name with keys default because that comes from here I Can now expand this and it should and it shows me the Contents of that variable or that array element and you can even go deeper in that so that you see all the properties of that one and so on and so on until Okay, this is going to continue endlessly because that's a recursive variable So you can click those things until you went out of memory basically so don't do that So there's lots and lots of stuff in here if you want you get to change the values of all these variables and properties as well It's a really really handy way to figure out what goes on other things that are kind of cool in here is that You get to set breakpoints By clicking in the border So breakpoint is basically means that if XD book hits this bit of code stop the execution of your script So you don't have to go through all of it yourself by stepping I'm actually not sure whether this line of code is being executed. So we'll find out So I click resume program. That means continue executing from where we currently are and that indeed it stopped a little bit later In line 316. I still get to skip back to the previous one and back here Can can basically see everything that goes on Now, let me go back out of presentation modes, which I will be using many more times in the future And go back to the slides. I need to turn off the debugger. Otherwise, I'll be debugging the rest of the presentation All right, so PHP storm if you have never used this before, please have a look at this Did it turn it actually off? Yeah, I didn't stop the debugger and if I don't do that then Yeah, there we go There's a whole other things you can set breakpoint breakpoints on as well You can set them on exceptions when they're being set you can set breakpoints on warning or notice So to get a breakpoint in your IDE every time a warning or an exception happens You get to see the values of constants now I don't think PHP storm shows that yet, but I'm sure it will so do so in the future So yeah, there's lots of nice things in there and that has already been released quite some time ago To activate this debugger you need to make basically three settings or I rather you only have to do one which is remote underscore enable enable this thing what is handy is to also make the setting remote connect back which means that XD book will look at the HTTP headers to find out which IP address made the browser requests and Then connect back to that IP address Which is really handy if you are in an organization where you have a shared development machine So that you don't have to tell the debugger a specific IP address But use the IP address of the machine that made a request which is really handy to have Called remote connect back if you want to do debug unit tests You can do the same thing the way how you activate it there is by setting an environment variable The value of the IDE key doesn't particularly matter unless your net beans Because they want to be special. I don't know in a browser I would rather activate it with one of my browser extensions. So there's the easiest XD book for Firefox There's XD book helper for Chrome and XD book tockler for Safari There's also one for opera, but opera doesn't really exist anymore. So I've left it off the slide as far as I know There's nothing for IE Right, so let's have a quick look at some other things I Spoken about the primitive debugging with Vardem. I showed you some live debugging. I showed you some tracing XD book also helps PHP units with code coverage anytime you do unit tests with code coverage Code coverage is something that tells you which lines of your code have been run for each test being executed and In order for PHP units to do that it uses a functionality in XD bug to collect that data all right All thing this functionality XD book is very complicated because PHP's internals are really quite complicated So whenever I ask functionality to that bit I use a different extension called VLD that Tells me how PHP's internal structures are going on and it is a tool that will dump you a Operate which is PHP's internal structures for the main scripts functions class methods and It also does things like that code analysis. So it goes through your code and figures out which lines of code cannot be executed for example variable assignment after a return statement or code that has been run after a An exception has been thrown by the throw keywords and things like that And it also because it follows all of the parts through your code Every if statement both yes and no It follows for loops. It gets quite a good understanding of what happens in your code So let's have a look at the if then else method here or function here It has two states or two variables really a and B and the function doesn't do anything particularly useful it either act it Can echo both a is hit and B is hit depending on which arguments you put into the function If you would run that with those two statements here You will see that if you do code coverage You have covered every single line because it has covered this if statement that happens in whenever a is true or false Echo a is hit only happens when a is true and similarly for B hits which only echoes when B is true But by calling those two functions, we've covered every single line there But we haven't covered every single path through the function So even though the code coverage is saying that you've covered a hundred percent of the lines That doesn't mean you have tested your function for a hundred percent because you're missing the through through case and the false false case The author of PHP units Sebastian Bergman. He's been bugging me for years to add something to XD bug to Figure out which parts are being executed in your script and that was not particularly easy to write Just a quick look of what VLD outputs it dumps this whole bunch of outputs here We can go over that some other time Happy to explain there and in the bottom it tells you all the different branches. So you remember if then else had Five branches in there, right the first one until the first if statement then the if statement both through and false And then the second if statement also through and false. So there's five branches But there's four parts through it seeing that from those slide is difficult. So I've made it you an image for it instead So on the on the left hand side I there's both a graph for if then else but also for loopy which I'll skipping for this this version of the presentation and it tells you exactly the different branches by the by the blocks by the rectangles and then the different Options you can take from each block Which one gets executed next so if a is through it will follow the block that start with OP 5 to 7 and if B is hit it will follow the block with OP 10 to 12 So we now know which parts are possible after doing the analysis I Have to admit if you have a very complex function with hundreds of if statements. This is not going to work particularly well Every time you do an if statement You double the amount of possible parts You run out of memory doing the analysis if there's too many of them, which is not a good reason to keep your functions nice and short Anyway, when I have the parts I can now figure out which ones are actually executed and Xz work helps you with that. So PHP unit uses internally something called PHP code coverage Which you can also use independently without PHP unit, which is handy to do and that's what I've done in this slide So I'm requiring Composer to do the to pull in code coverage I started up and Have two test cases the first one here if then else with true and false and in second case with If then else falls and true to emulate the cases from a few slides ago And when you then show this in code coverage you get this right you get a hundred percent everything is green Everything is happy, but that's not really quite a case Because we know we haven't covered true true and false false this is very xd books branch coverage comes into play, which is a new feature in two three and Yeah, it needs support in PHP code coverage to render this in some way and although Sebastian has been bugging me for years to implement it I've now been bugging him for a year and a half to get it in PHP code covered and I suppose I know that it's still not been done. I Don't know what to say then in any case You get to output from xd book those different branches and parts as well now because it collects that information and then you need a way to render this and that is not trivial because With four possible parts. It's quite easy to do right You have a different color for each different parts if it's a solid line It means it's been followed if it's a dashed line. It is not been followed before that is doable we Try to imagine what happens if you have a hundred parts, right? It makes it really difficult to then figure out what actually happens So somebody needs to come up with a clever way of showing which parts have been followed and which ones haven't been and then give you some useful matrix on Not trivial to them All right Done with that bit. Let's look at some time travel Not into the future. Sadly. We're only going to go in the past Before I start with that Remember the things that I've shown you early. I've shown you tracing that shows you Everything that has happened during your scripts. I've also shown you Life debugging where you get to live DB a running script One thing you cannot do in live debugging is stepping backwards You can't step backwards is because of several things some internal PHP things like if you have entered a for each loop You cannot get out of that without Without memory leaks in PHP itself If you have executed an insert statement into a database You cannot an insert it if you do update a value you can't cannot an update a value in a column You can't do that, right? But be really handy that if you could combine the trace files Together with PHP's live debugging which means that you get to create a trace of your whole execution of your script and Then later Give this file to somebody else and run a live debugging session over that and because at that moment I've already recorded every single state during the execution I can do a step back and figure out what was the state one statement earlier Which is kind of handy it actually would allow you to if you have a hosted Now if you produce a product that other people install on their own servers They're having issues with that you can also enable the tracing give me the trace file Which will also include the contents of every source file and Things like that and then a sport team or you probably going to be the sport team will be able to Play back and forwards to the execution of that particular instance To get to figure out exactly what goes on what caused the issue because you have everything in there that you need after the fact So that is something called way back and I've been working on that for some time It is going to work as a not our tracing module like the ones I've shown you earlier By a different format number doesn't really matter which one of this and it will act as a DBGP client just like XD book is To Play back those recorded traces as it was PHP running at that same time It will require some additional functionality in IDEs Like PHP store needs to have an extra button that allows you to step back or slider That allows you to go forwards and backwards through the whole time frame and It's quite a lot of work still to be done here. I started working with and I was happily going along and then PHP 7 happens and PHP 7 although for users it is very few changes if you write extensions it is a pain a massive pain really But yeah, that is progress, right? All right So that means that way back is now going to be way in the future because I need to go back in it Actually, I have to rewrite it as well because PHP 7 is so different It compiles though, which is always handy all the test cases pass And but yeah PHP 7 meant like rewriting XD book everything had to be read on that touches internal structure Which is about 60% of what XD book really does That took a lot of time it took nearly a year to do that But it works now all the test cases pass and I released it about a month and a half ago so it still has a few bucks in there, but It's good enough for people to use and if you find a bug, please let me know and I'll help you to go fix it So XD book 2 4 came out. It also has another tool in there again requested by Sebastian for PHP units. He wanted a way to blacklist certain functions from being used Because some functions are easy apparently I don't know. I don't know what is was it what he was going to get at there And that is something called function monitoring which will collect every time a specific function has been Executed or called By recording the phone number No, the file name and line number Where this function call is being made from and it looks a little bit like this? so we start the function monitor which start function monitor you give it an array of functions or Method names or static method calls and things like that You can even give it closures as long as you give it a correct line and phone numbers And then every time that function is being called it will get added to an internal list Which you get to retrieve by calling XD but get monitored functions and then you get a result like this So you get a function name the file name and the line number where this one was called from and then if you are a PHP unit developer, then you can Easily tell you all you're using this function in this line and you really shouldn't be doing that that kind of things All right, so I'm skipping that slide because that's the wrong event for that Yeah, that's about what I have to talk about if there's any questions. I'm happy to hear about them right now There's one right in the back. Thank you, Derek. Let's give a round of applause this first We don't have a lot of time for questions. We have about five minutes. So just a couple. Yeah, then we have lunch Which I will also be attending so you can ask me more questions that later. There was some right in the back Hello With the function monitoring, how would it work with? class methods on etc You can specify class name arrow method name As a string So instead of using the string for the function name What you can use is class name arrow method name or class name double colon static method name So you get to specify those two. I think I might at some point extend this to having like You can put in a callable in there as well to to register those but I Haven't done it yet. Okay. Thank you. You're welcome And you mentioned you've actually had to rewrite for PHP 7. Yeah, do you is there any support for HHVM? Oh That's an interesting question. So HHVM has implemented some of X debug They have implemented some of the functionality and they have implemented live debugging The last time I tried that it would crash all over the place But yeah, they have support for support between quotes All right, anything else Nothing. Oh, yeah, right now That was pointing at you It's not really a question, but I think it's possibly the only person that said I was using atom Oh, right. There is one called PHP debug, but I've not tried it I've just installed it, but I've not actually run it yet. All right. There's one there. Okay. Good luck I'm happy to hear the results of this experiment All right, so question if you have no questions, I'll ask questions So how many of you have use XD book before today? How many hadn't I should probably ask are you going to now? Yes, that's what I like to see. All right Slides are going to be online here. There aren't quite there yet The session has also been recorded I Wasn't structured that I probably should string string record the demos so that I can put them in the Recordings I'll have to spend some time on that in the next few days. I guess if you have any further questions feel free to email me. I am Usually pretty good replying to email but not all the time and if I don't reply just send me another email I'll get to you at some point Yeah, if there's everything thanks very much and joy the rest of the conference and lunch