КатегорииLinksUnix Tutorial
Personal Development Ruslan Valiev Solaris Performance Team Damien Farnham Fintan Ryan Nicky Veitch Niall Mullen Sean McGrath DTrace Bryan Cantrill Brendan Gregg ZFS Tim Foster General Ben Rockwood Learning Solaris 10 Privacy policy |
Tuesday, December 6. 2005DTrace: observing PHP
I've finally got some spare time to download a PEAR module for DTrace. What a great module! I've found a little problem with it, wrote a letter to Wez Furlong, and he immediately changed the source code to make it work even better. Thanks a lot, Wez! :)
So, here it is - my first PHP Dtrace script: php-scripts-snoop.d CODE: #!/usr/sbin/dtrace -s #pragma D option quiet :dtrace.so::function-entry / copyinstr(arg0) == "" / { printf("%d -> %-30s\n", pid, copyinstr(arg1)); } :dtrace.so::function-return / copyinstr(arg0) == "" / { printf("%d <- %-30s\n", pid, copyinstr(arg1)); } So go ahead, download the script, do chmod a+x for it, and after you start it in a terminal of your web-server, you can run PHP scripts from this server, and our DTrace script will be reporting us whenever each PHP script stars or finishes, specifying which Apache process is responsible for this: CODE: bash-3.00# ./php-scripts-snoop.d 204934 -> /export/www/pma/index.php 204934 <- /export/www/pma/index.php 204934 -> /export/www/pma/css/phpmyadmin.css.php 204934 <- /export/www/pma/css/phpmyadmin.css.php 204936 -> /export/www/pma/left.php 204934 -> /export/www/pma/queryframe.php 204936 <- /export/www/pma/left.php 204936 -> /export/www/pma/main.php 204934 <- /export/www/pma/queryframe.php 204934 -> /export/www/pma/css/phpmyadmin.css.php 204934 <- /export/www/pma/css/phpmyadmin.css.php 204934 -> /export/www/pma/css/phpmyadmin.css.php 204936 -> /export/www/pma/css/phpmyadmin.css.php 204934 <- /export/www/pma/css/phpmyadmin.css.php 204934 -> /export/www/1.php 204934 <- /export/www/1.php 204936 <- /export/www/pma/css/phpmyadmin.css.php pS: yes, starting of today I've decided to provide download links for each of my scripts - should be more convenient for you now. This means that sooner or later I'll update all the archive blog entries to have download links for respective scripts. Wednesday, May 18. 2005
DTrace: command line parameters in ... Posted by Gleb Reys
in DTrace at
20:05
Comments (0) Trackbacks (0) Defined tags for this entry: dtrace
DTrace: command line parameters in scripting
When you're writing a DTrace script, often you'd like to pass some parameter in command line. Such an easy task might be really hard unless you read a docs.sun.com section about DTrace scripting, particularly a section about comman line parameters macros.
As with sh or bash, you can access command line parameters via $0..$9 macroses. But there's a catch. Values of these macroses should match a certain definition - a number, an identifier or a string. If there isn't a match here, you'll get an error. Let's start with a simple script. CODE: #!/usr/sbin/dtrace -s #pragma D option quiet BEGIN { printf("Your parameter: %d\n", $1); } If you pass a number as a command line parameter, there will be no error, and you'll get this message: Your parameter: 123 But if you pass a string instead of number, here's the error you'll get: bash-3.00# ./args.d a1b2c3 dtrace: failed to compile script ./args.d: line 9: failed to resolve a1b2c3: Unknown variable name This happens because DTrace tries to find an identifier "a1b2c3". Why does this happen? Because of the way DTrace tries to recognize the type of a command line parameter. The parameter doesn't look like a number, but to make DTrace accept the value as a string, you have to specify it in quotes. Without them, DTrace accepts we've passed an identifier name. And since there isn't one with such a name, we get an error. To prove this theory, you simply have to create a variable with the same name in your script: CODE: #!/usr/sbin/dtrace -s #pragma D option quiet BEGIN { a1b2c3=1979; printf("Your parameter: %d\n", $1); } There you go - it works just like I said: Your parameter: 1979 And if your parameter is definitely a string, you have to change your script (the line with printf, so that it has %s for a string, and not %d for an integer, otherwise you'll get another error)... CODE: #!/usr/sbin/dtrace -s #pragma D option quiet BEGIN { a1b2c3=1979; printf("Your parameter: %s\n", $1); } ... and also put your parameter in quotes, like this: bash-3.00# ./args.d '"a1b2c3"' Your parameter: a1b2c3 As you can see, we're getting a string we passed, and not the variable with the same name, which was left in the script intentionally to demonstrate it's not used anymore. And finally, one more thing. If you want all your command line parameters to be treated as strings by default and even leave the quotes around parameters out, all you have to do is to access the macroses for your parameters with a double $ sign, and then the parameter type definition will be skipped: CODE: #!/usr/sbin/dtrace -s #pragma D option quiet BEGIN { a1b2c3=1979; printf("Your parameter: %s\n", $$1); } And here's how you'd access such a script with the same parameter we've used before: bash-3.00# ./args.d a1b2c3 Your parameter: a1b2c3 That's it for today! Good luck with your DTrace scripts! Tuesday, March 1. 2005
Tips and tricks from DTrace authors Posted by Gleb Reys
in DTrace at
13:38
Comments (0) Trackbacks (0) Tips and tricks from DTrace authors
Yesterday Bryan Cantrill (one of DTrace authors) provided a link to the presentation he along with two other DTrace authors gave recently on advanced DTrace scripting. There are lots of useful tips, tricks and gotchas explained, which might be useful for both newbies and some seasoned DTrace users.
As it's states one the very first slides of the presentation, some of the things presented are quite simple - most of the tips you'd know if you read the documentation carefully, but the tips given are very useful. I've learned lots of new things, so I strongly recommend anyone interested in DTrace to find some time and read it as well. Here is the presentation in a PDF file. Saturday, February 26. 2005
DTrace: pid provider Posted by Gleb Reys
in DTrace at
23:52
Comments (0) Trackbacks (0) Defined tags for this entry: dtrace
DTrace: pid provider
Quite ofter we'd like to know what happens to a particular process on a system, and if we're using DTrace for our investigation, we'd probably use the $target variable (I've already spoked about it in the past) or simply specify the process identifier (pid) in some predicates of our probes, thus pointing to DTrace that we're after only a certain process. $target variable still can and should be used when creating our probes with pid provider.
For DTrace beginners it's quite a common and good practice. But there is a better way: using the pid provider. As it's explained in the official DTrace guide, pid provider isn't just a provider, but in fact is a whole class of providers. And, as the name suggests, it provides us with probes related to a certain process. For instance, pid737 provider allows us to trace everything happening within a process with pid of 737. What's really cool about this provide - it's not only easier to scope our interest, but it also gets much easier to see how where certain probes originate from (what libraries provide certain functions used by our process, read on - I'll explain it a bit later). Such a way of looking at a certain process, in contrast to using predicates like /pid == $target/, is more correct. Instead of enabling all the specified probes by default, and then using predicates for each probe firing for each process on the system only to see whether we're interested in a given process, DTrace won't even look at other processes - and pid737 will make sure that all the probes specified will be enabled and possibly fired only for the specified process. We already know how to get the list of all the probes known to DTrace. I've explained it a while ago. The thing you need to remember about the pid provider is this: after you've used pid provider in some probes for a process, dtrace -l will start reporting you these probes at the end of all the available probes. They've been dynamically allocated just for you. If the process in question dies or finishes, all the dynamically allocated probes for its pid provider will disappear. One more thing I really like about pid provider - modules used in probes are in fact libraries used for the execution of the process you're looking at. This gives you a wonderful opportunity of observing only what's been done in your process by functions of a particular library. Very useful. Right, enough of theory - here's an example. First, we create a simple bit of "Hello, world!" code in C: CODE: void main() { printf("Hello, world!\n"); } After you compile it, let's get going with out DTrace script: CODE: #!/usr/sbin/dtrace -s #pragma D option quiet BEGIN { printf("Target pid: %d\n", $target); } pid$target:::entry { printf("%s:%s:%s:%s\n", probeprov, probemod, probefunc, probename); } Save the script, make it executable, and start it, specifying our hello as a command to start: CODE: $ ./pid-global.d -c ./hello | more Target pid: 1059 Hello, world! pid1059:LM1`ld.so.1:call_array:entry pid1059:LM1`ld.so.1:call_init:entry pid1059:LM1`ld.so.1:leave:entry pid1059:LM1`ld.so.1:fmap_setup:entry pid1059:LM1`ld.so.1:munmap:entry pid1059:LM1`ld.so.1:rt_bind_clear:entry pid1059:LM1`ld.so.1:_rt_bind_clear:entry pid1059:LM1`ld.so.1:rt_mutex_unlock:entry pid1059:LM1`ld.so.1:_rt_null:entry pid1059:LM1`ld.so.1:rt_bind_clear:entry pid1059:LM1`ld.so.1:_rt_bind_clear:entry pid1059:libc.so.1:libc_init:entry pid1059:libc.so.1:__amd64id:entry pid1059:libc.so.1:atexit:entry pid1059:libc.so.1:lmalloc:entry pid1059:libc.so.1:getbucketnum:entry ... You see this list of probes? I won't give you the full list - it's going to be almost a 1000 lines, but I hope you get the idea. It's also a perfect opportunity to see how easy it's going to be for you to track probes of a particular library. Back to our example, it's obvious what I'm going to do now: I'd like to get our printf caught with a probe. CODE: #!/usr/sbin/dtrace -s #pragma D option quiet BEGIN { printf("Target pid: %d\n", $target); } pid$target::printf:entry { printf("%s:%s:%s:%s\n", probeprov, probemod, probefunc, probename); printf("Argument: %s\n", copyinstr(arg0)); } If we start tihs script, here's what you'll get: CODE: $ ./pid-printf.d -c ./hello
Target pid: 1201 Hello, world! pid1201:libc.so.1:printf:entry Argument: Hello, world! Tuesday, January 25. 2005
DTrace open sourced Posted by Gleb Reys
in DTrace at
19:49
Comments (0) Trackbacks (0) Defined tags for this entry: dtrace, opensolaris
DTrace open sourced
At last, OpenSolaris.Org is up!
As it was expected, it's not possible to download OS sources just yet. Instead, to prove that the whole OpenSolaris idea is serious, Sun revealed DTrace sources. Bryan Cantrill has posted a very interesting entry in his blog today, I strongly recommend you read it: Solaris 10 Revealed. |





