| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732  | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html > <head><title>Components of a SystemTap script</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="generator" content="TeX4ht (https://tug.org/tex4ht/)"> <meta name="originator" content="TeX4ht (https://tug.org/tex4ht/)"> <!-- html,2 --> <meta name="src" content="langref.tex"> <link rel="stylesheet" type="text/css" href="langref.css"> </head><body > <!--l. 331--><div class="crosslinks"><p class="noindent">[<a href="langrefse4.html" >next</a>] [<a href="langrefse2.html" >prev</a>] [<a href="langrefse2.html#taillangrefse2.html" >prev-tail</a>] [<a href="#taillangrefse3.html">tail</a>] [<a href="langref.html#langrefse3.html" >up</a>] </p></div> <h3 class="sectionHead"><span class="titlemark">3 </span> <a id="x5-150003"></a>Components of a SystemTap script</h3> <!--l. 333--><p class="noindent" >The main construct in the scripting language identifies probes. Probes associate abstract events with a statement block, or probe handler, that is to be executed when any of those events occur. <!--l. 337--><p class="noindent" >The following example shows how to trace entry and exit from a function using two probes. <!--l. 340--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 340--><p class="indent" > <pre class="verbatim" id="verbatim-7"> probe kernel.function("sys_mkdir").call { log ("enter") } probe kernel.function("sys_mkdir").return { log ("exit") } </pre> <!--l. 344--><p class="nopar" ></dd></dl> <!--l. 347--><p class="noindent" >To list the probe-able functions in the kernel, use the listing option (<span class="cmtt-10">-l</span>). For example: <!--l. 350--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 350--><p class="indent" > <pre class="verbatim" id="verbatim-8"> $ stap -l ’kernel.function("*")’ | sort </pre> <!--l. 353--><p class="nopar" ></dd></dl> <!--l. 356--><p class="noindent" > <h4 class="subsectionHead"><span class="titlemark">3.1 </span> <a id="x5-160003.1"></a>Probe definitions</h4> <!--l. 358--><p class="noindent" >The general syntax is as follows. <!--l. 360--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 360--><p class="indent" > <pre class="verbatim" id="verbatim-9"> probe PROBEPOINT [, PROBEPOINT] { [STMT ...] } </pre> <!--l. 363--><p class="nopar" ></dd></dl> <!--l. 365--><p class="noindent" >Events are specified in a special syntax called <span class="cmti-10">probe points</span>. There are several varieties of probe points defined by the translator, and tapset scripts may define others using aliases. The provided probe points are listed in the <span class="cmtt-10">stapprobes(3)</span>, <span class="cmtt-10">tapset::*(3stap)</span>, and <span class="cmtt-10">probe::*(3stap) </span>man pages. The STMT statement block is executed whenever <img src="cmr10-10.png" alt="i" class="10x-x-10" />any of the named PROBEPOINT events occurs. <!--l. 372--><p class="noindent" >The probe handler is interpreted relative to the context of each event. For events associated with kernel code, this context may include variables defined in the source code at that location. These <span class="cmti-10">target variables</span><a id="dx5-16001"></a> (or “context variables”) are presented to the script as variables whose names are prefixed with a dollar sign (<span class="tcrm-1000">$</span>). They may be accessed only if the compiler used to compile the kernel preserved them, despite optimization. This is the same constraint imposed by a debugger when working with optimized code. Other events may have very little context. <!--l. 382--><p class="noindent" > <h4 class="subsectionHead"><span class="titlemark">3.2 </span> <a id="x5-170003.2"></a>Probe aliases</h4> <a id="dx5-17001"></a> <!--l. 384--><p class="noindent" >The general syntax is as follows. <!--l. 386--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 386--><p class="indent" > <pre class="verbatim" id="verbatim-10"> probe <alias> = <probepoint> { <prologue_stmts> } probe <alias> += <probepoint> { <epilogue_stmts> } </pre> <!--l. 390--><p class="nopar" ></dd></dl> <!--l. 393--><p class="noindent" >New probe points may be defined using <span class="cmti-10">aliases</span>. A probe point alias looks similar to probe definitions, but instead of activating a probe at the given point, it defines a new probe point name as an alias to an existing one. New probe aliases may refer to one or more existing probe aliases. Multiple aliases may share the same underlying probe points. The following is an example. <!--l. 400--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 400--><p class="indent" > <pre class="verbatim" id="verbatim-11"> probe socket.sendmsg = kernel.function ("sock_sendmsg") { ... } probe socket.do_write = kernel.function ("do_sock_write") { ... } probe socket.send = socket.sendmsg, socket.do_write { ... } </pre> <!--l. 405--><p class="nopar" ></dd></dl> <!--l. 408--><p class="noindent" >There are two types of aliases, the prologue style and the epilogue style which are identified by the equal sign (<span class="cmtt-10">=</span>) and ”<span class="cmtt-10">+=</span>” respectively. <!--l. 412--><p class="noindent" >A probe that uses a probe point alias will create an actual probe, with the handler of the alias <span class="cmti-10">pre-pended</span>. <!--l. 415--><p class="noindent" >This pre-pending behavior serves several purposes. It allows the alias definition to pre-process the context of the probe before passing control to the handler specified by the user. This has several possible uses, demonstrated as follows. <!--l. 419--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 419--><p class="indent" > <pre class="verbatim" id="verbatim-12"> # Skip probe unless given condition is met: if ($flag1 != $flag2) next # Supply values describing probes: name = "foo" # Extract the target variable to a plain local variable: var = $var </pre> <!--l. 429--><p class="nopar" ></dd></dl> <!--l. 432--><p class="noindent" > <h5 class="subsubsectionHead"><span class="titlemark">3.2.1 </span> <a id="x5-180003.2.1"></a>Prologue-style aliases (=)</h5> <a id="dx5-18001"></a> <a id="dx5-18002"></a> <!--l. 435--><p class="noindent" >For a prologue style alias, the statement block that follows an alias definition is implicitly added as a prologue to any probe that refers to the alias. The following is an example. <!--l. 439--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 439--><p class="indent" > <pre class="verbatim" id="verbatim-13"> # Defines a new probe point syscall.read, which expands to # kernel.function("sys_read"), with the given statement as # a prologue. # probe syscall.read = kernel.function("sys_read") {     fildes = $fd } </pre> <!--l. 448--><p class="nopar" ></dd></dl> <!--l. 451--><p class="noindent" > <h5 class="subsubsectionHead"><span class="titlemark">3.2.2 </span> <a id="x5-190003.2.2"></a>Epilogue-style aliases (+=)</h5> <a id="dx5-19001"></a> <a id="dx5-19002"></a> <!--l. 454--><p class="noindent" >The statement block that follows an alias definition is implicitly added as an epilogue to any probe that refers to the alias. It is not useful to define new variables there (since no subsequent code will see them), but rather the code can take action based upon variables set by the prologue or by the user code. The following is an example: <!--l. 460--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 460--><p class="indent" > <pre class="verbatim" id="verbatim-14"> # Defines a new probe point with the given statement as an # epilogue. # probe syscall.read += kernel.function("sys_read") {     if (traceme) println ("tracing me") } </pre> <!--l. 468--><p class="nopar" ></dd></dl> <!--l. 471--><p class="noindent" > <h5 class="subsubsectionHead"><span class="titlemark">3.2.3 </span> <a id="x5-200003.2.3"></a>Probe alias usage</h5> <!--l. 473--><p class="noindent" >A probe alias is used the same way as any built-in probe type, by naming it: <!--l. 476--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 476--><p class="indent" > <pre class="verbatim" id="verbatim-15"> probe syscall.read {     printf("reading fd=%d\n", fildes) } </pre> <!--l. 481--><p class="nopar" ></dd></dl> <!--l. 484--><p class="noindent" > <h5 class="subsubsectionHead"><span class="titlemark">3.2.4 </span> <a id="x5-210003.2.4"></a>Alias suffixes</h5> <!--l. 486--><p class="noindent" >It is possible to include a suffix with a probe alias invocation. If only the initial part of a probe point matches an alias, the remainder is treated as a suffix and attached to the underlying probe point(s) when the alias is expanded. For example: <!--l. 491--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 491--><p class="indent" > <pre class="verbatim" id="verbatim-16"> /* Define an alias: */ probe sendrecv = tcp.sendmsg, tcp.recvmsg { ... } /* Use the alias in its basic form: */ probe sendrecv { ... } /* Use the alias with an additional suffix: */ probe sendrecv.return { ... } </pre> <!--l. 501--><p class="nopar" ></dd></dl> <!--l. 504--><p class="noindent" >Here, the second use of the probe alias is equivalent to writing <span class="obeylines-h"><span class="verb"><span class="cmtt-10">probe</span><span class="cmtt-10"> tcp.sendmsg.return,</span><span class="cmtt-10"> tcp.recvmsg.return</span></span></span>. <!--l. 506--><p class="noindent" >As another example, the probe points <span class="obeylines-h"><span class="verb"><span class="cmtt-10">tcp.sendmsg.return</span></span></span> and <span class="obeylines-h"><span class="verb"><span class="cmtt-10">tcp.recvmsg.return</span></span></span> are actually defined as aliases in the tapset <span class="obeylines-h"><span class="verb"><span class="cmtt-10">tcp.stp</span></span></span>. They expand to a probe point of the form <span class="obeylines-h"><span class="verb"><span class="cmtt-10">kernel.function("...").return</span></span></span>, so they can also be suffixed: <!--l. 508--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 508--><p class="indent" > <pre class="verbatim" id="verbatim-17"> probe tcp.sendmsg.return.maxactive(10) {     printf("returning from sending %d bytes\n", size) } </pre> <!--l. 513--><p class="nopar" ></dd></dl> <!--l. 516--><p class="noindent" >Here, the probe point expands to <span class="obeylines-h"><span class="verb"><span class="cmtt-10">kernel.function("tcp_sendmsg").return.maxactive(10)</span></span></span>. <!--l. 519--><p class="noindent" > <h5 class="subsubsectionHead"><span class="titlemark">3.2.5 </span> <a id="x5-220003.2.5"></a>Alias suffixes and wildcards</h5> <!--l. 521--><p class="noindent" >When expanding wildcards, SystemTap generally avoids considering alias suffixes in the expansion. The exception is when a wildcard element is encountered that does not have any ordinary expansions. Consider the following example: <!--l. 526--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 526--><p class="indent" > <pre class="verbatim" id="verbatim-18"> probe some_unrelated_probe = ... { ... } probe myprobe = syscall.read { ... } probe myprobe.test = some_unrelated_probe { ... } probe myprobe.* { ... } probe myprobe.ret* { ... } </pre> <!--l. 537--><p class="nopar" ></dd></dl> <!--l. 540--><p class="noindent" >Here, <span class="obeylines-h"><span class="verb"><span class="cmtt-10">return</span></span></span> would be a valid suffix for <span class="obeylines-h"><span class="verb"><span class="cmtt-10">myprobe</span></span></span>. The wildcard <span class="obeylines-h"><span class="verb"><span class="cmtt-10">myprobe.*</span></span></span> matches the ordinary alias <span class="obeylines-h"><span class="verb"><span class="cmtt-10">myprobe.test</span></span></span>, and hence the suffix expansion <span class="obeylines-h"><span class="verb"><span class="cmtt-10">myprobe.return</span></span></span> is not included. Conversely, <span class="obeylines-h"><span class="verb"><span class="cmtt-10">myprobe.ret*</span></span></span> does not match any ordinary aliases, so the suffix <span class="obeylines-h"><span class="verb"><span class="cmtt-10">myprobe.return</span></span></span> is included as an expansion. <!--l. 547--><p class="noindent" > <h4 class="subsectionHead"><span class="titlemark">3.3 </span> <a id="x5-230003.3"></a>Variables</h4> <a id="dx5-23001"></a> <!--l. 549--><p class="noindent" >Identifiers for variables and functions are alphanumeric sequences, and may include the underscore (_) and the dollar sign (<span class="tcrm-1000">$</span>) characters. They may not start with a plain digit. Each variable is by default local to the probe or function statement block where it is mentioned, and therefore its scope and lifetime is limited to a particular probe or function invocation. Scalar variables are implicitly typed as either string or integer. Associative arrays also have a string or integer value, and a tuple of strings or integers serves as a key. Arrays must be declared as global. Local arrays<a id="dx5-23002"></a> are not allowed. <!--l. 559--><p class="noindent" >The translator performs <span class="cmti-10">type inference </span>on all identifiers, including array indexes and function parameters. Inconsistent type-related use of identifiers results in an error. <!--l. 563--><p class="noindent" >Variables may be declared global. Global variables are shared among all probes and remain instantiated as long as the SystemTap session. There is one namespace for all global variables, regardless of the script file in which they are found. Because of possible concurrency limits, such as multiple probe handlers, each global variable used by a probe is automatically read- or write-locked while the handler is running. A global declaration may be written at the outermost level anywhere in a script file, not just within a block of code. Global variables which are written but never read will be displayed automatically at session shutdown. The following declaration marks <span class="cmtt-10">var1 </span>and <span class="cmtt-10">var2 </span>as global. The translator will infer a value type for each, and if the variable is used as an array, its key types. <!--l. 576--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 576--><p class="indent" > <pre class="verbatim" id="verbatim-19"> global var1[=<value>], var2[=<value>] </pre> <!--l. 579--><p class="nopar" ></dd></dl> <!--l. 582--><p class="noindent" >The scope of a global variable may be limited to a tapset or user script file using private keyword. The global keyword is optional when defining a private global variable. Following declaration marks var1 and var2 private globals. <!--l. 587--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 587--><p class="indent" > <pre class="verbatim" id="verbatim-20"> private global var1[=<value>] private var2[=<value>] </pre> <!--l. 591--><p class="nopar" ></dd></dl> <!--l. 594--><p class="noindent" > <h5 class="subsubsectionHead"><span class="titlemark">3.3.1 </span> <a id="x5-240003.3.1"></a>Unused variables</h5> <a id="dx5-24001"></a> <!--l. 597--><p class="noindent" >The SystemTap translator removes unused variables. Global variable that are never written or read are discarded. Every local variables where the variable is only written but never read are also discarded. This optimization prunes unused variables defined in the probe aliases, but never used in the probe handler. If desired, this optimization can disabled with the <span class="cmtt-10">-u </span>option. <!--l. 604--><p class="noindent" > <h4 class="subsectionHead"><span class="titlemark">3.4 </span> <a id="x5-250003.4"></a>Auxiliary functions</h4> <a id="dx5-25001"></a> <!--l. 606--><p class="noindent" >General syntax: <!--l. 608--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 608--><p class="indent" > <pre class="verbatim" id="verbatim-21"> function <name>[:<type>] ( <arg1>[:<type>], ... )[:<priority>] { <stmts> } </pre> <!--l. 611--><p class="nopar" ></dd></dl> <!--l. 613--><p class="noindent" >SystemTap scripts may define subroutines to factor out common work. Functions may take any number of scalar arguments, and must return a single scalar value. Scalars in this context are integers or strings. For more information on scalars, see Section <a href="#x5-230003.3">3.3<!--tex4ht:ref: sub:Variables --></a> and Section <a href="langrefse5.html#x7-640005.2">5.2<!--tex4ht:ref: sub:Data-types --></a><span class="cmtt-10">. </span>The following is an example function declaration. <!--l. 619--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 619--><p class="indent" > <pre class="verbatim" id="verbatim-22"> function thisfn (arg1, arg2) {     return arg1 + arg2 } </pre> <!--l. 624--><p class="nopar" ></dd></dl> <!--l. 627--><p class="noindent" >Note the general absence of type declarations, which are inferred by the translator. If desired, a function definition may include explicit type declarations for its return value, its arguments, or both. This is helpful for embedded-C functions. In the following example, the type inference engine need only infer the type of arg2, a string. <!--l. 633--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 633--><p class="indent" > <pre class="verbatim" id="verbatim-23"> function thatfn:string(arg1:long, arg2) {     return sprintf("%d%s", arg1, arg2) } </pre> <!--l. 638--><p class="nopar" ></dd></dl> <!--l. 641--><p class="noindent" >Functions may call others or themselves recursively, up to a fixed nesting limit. See Section <a href="langrefse1.html#x3-110001.6">1.6<!--tex4ht:ref: sub:SystemTap-safety --></a>. <!--l. 644--><p class="noindent" >Functions may be marked private using the private keyword to limit their scope to the tapset or user script file they are defined in. An example definition of a private function follows: <!--l. 648--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 648--><p class="indent" > <pre class="verbatim" id="verbatim-24"> private function three:long () { return 3 } </pre> <!--l. 651--><p class="nopar" ></dd></dl> <!--l. 654--><p class="noindent" >Functions terminating without reaching an explicit return statement will return an implicit 0 or <span class="obeylines-h"><span class="verb"><span class="cmtt-10">""</span></span></span>, determined by type inference. <!--l. 657--><p class="noindent" >Functions may be overloaded during both runtime and compile time. <!--l. 659--><p class="noindent" >Runtime overloading allows the executed function to be selected while the module is running based on runtime conditions and is achieved using the ”next” statement in script functions and <span class="cmtt-10">STAP</span><span class="cmtt-10">_NEXT </span>macro for embedded-C functions. For example, <!--l. 664--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 664--><p class="indent" > <pre class="verbatim" id="verbatim-25"> function f() { if (condition) next; print("first function") } function f() %{ STAP_NEXT; print("second function") %} function f() { print("third function") } </pre> <!--l. 669--><p class="nopar" ></dd></dl> <!--l. 672--><p class="noindent" >During a functioncall f(), the execution will transfer to the third function if condition evaluates to true and print ”third function”. Note that the second function is unconditionally nexted. <!--l. 676--><p class="noindent" >Parameter overloading allows the function to be executed to be selected at compile time based on the number of arguments provided to the functioncall. For example, <!--l. 680--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 680--><p class="indent" > <pre class="verbatim" id="verbatim-26"> function g() { print("first function") } function g(x) { print("second function") } g() -> "first function" g(1) -> "second function" </pre> <!--l. 686--><p class="nopar" ></dd></dl> <!--l. 689--><p class="noindent" >Note that runtime overloading does not occur in the above example, as exactly one function will be resolved for the functioncall. The use of a next statement inside a function while no more overloads remain will trigger a runtime exception Runtime overloading will only occur if the functions have the same arity, functions with the same name but different number of parameters are completely unrelated. <!--l. 696--><p class="noindent" >Execution order is determined by a priority value which may be specified. If no explicit priority is specified, user script functions are given a higher priority than library functions. User script functions and library functions are assigned a default priority value of 0 and 1 respectively. Functions with the same priority are executed in declaration order. For example, <!--l. 702--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 702--><p class="indent" > <pre class="verbatim" id="verbatim-27"> function f():3 { if (condition) next; print("first function") } function f():1 { if (condition) next; print("second function") } function f():2 { print("third function") } </pre> <!--l. 707--><p class="nopar" ></dd></dl> <!--l. 710--><p class="noindent" > <h4 class="subsectionHead"><span class="titlemark">3.5 </span> <a id="x5-260003.5"></a>Embedded C</h4> <a id="dx5-26001"></a> <!--l. 712--><p class="noindent" >SystemTap supports a <span class="cmti-10">guru</span><a id="dx5-26002"></a> <span class="cmti-10">mode </span>where script safety features such as code and data memory reference protection are removed. Guru mode is set by passing the <span class="cmbx-10">-g </span>option to the stap command. When in guru mode, the translator accepts C code enclosed between “%<span class="cmsy-10">{</span>” and “%<span class="cmsy-10">}</span>” markers in the top level of the script file. The embedded C code is transcribed verbatim, without analysis, in sequence, into the top level of the generated C code. Thus, guru mode may be useful for adding #include instructions at the top level of the generated module, or providing auxiliary definitions for use by other embedded code. <!--l. 723--><p class="noindent" >When in guru mode, embedded C code blocks are also allowed as the body of a SystemTap function (as described in Section <a href="#x5-270003.6">3.6<!--tex4ht:ref: sub:Embedded-C-Functions --></a>), and in place of any SystemTap expression. In the latter case, the code block must contain a valid expression according to C syntax. <!--l. 729--><p class="noindent" >Here is an example of the various permitted methods of embedded C code inclusion: <!--l. 731--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 731--><p class="indent" > <pre class="verbatim" id="verbatim-28"> %{ #include <linux/in.h> #include <linux/ip.h> %} /* <-- top level */ /* Reads the char value stored at a given address: */ function __read_char:long(addr:long) %{ /* pure */          STAP_RETURN(kderef(sizeof(char), STAP_ARG_addr));          CATCH_DEREF_FAULT (); %} /* <-- function body */ /* Determines whether an IP packet is TCP, based on the iphdr: */ function is_tcp_packet:long(iphdr) {          protocol = @cast(iphdr, "iphdr")->protocol          return (protocol == %{ IPPROTO_TCP %}) /* <-- expression */ } </pre> <!--l. 749--><p class="nopar" ></dd></dl> <!--l. 752--><p class="noindent" > <h4 class="subsectionHead"><span class="titlemark">3.6 </span> <a id="x5-270003.6"></a>Embedded C functions</h4> <!--l. 754--><p class="noindent" >General syntax: <!--l. 756--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 756--><p class="indent" > <pre class="verbatim" id="verbatim-29"> function <name>:<type> ( <arg1>:<type>, ... )[:<priority>] %{ <C_stmts> %} </pre> <!--l. 759--><p class="nopar" ></dd></dl> <!--l. 761--><p class="noindent" >Embedded C code is permitted in a function body. In that case, the script language body is replaced entirely by a piece of C code enclosed between “%<span class="cmsy-10">{</span>” and “%<span class="cmsy-10">}</span>” markers. The enclosed code may do anything reasonable and safe as allowed by the C parser. <!--l. 768--><p class="noindent" >There are a number of undocumented but complex safety constraints on concurrency, resource consumption and runtime limits that are applied to code written in the SystemTap language. These constraints are not applied to embedded C code, so use embedded C code with extreme caution. Be especially careful when dereferencing pointers. Use the kread() macro to dereference any pointers that could potentially be invalid or dangerous. If you are unsure, err on the side of caution and use kread(). The kread() macro is one of the safety mechanisms used in code generated by embedded C. It protects against pointer accesses that could crash the system. <!--l. 778--><p class="noindent" >For example, to access the pointer chain <span class="cmtt-10">name = skb->dev->name </span>in embedded C, use the following code. <!--l. 781--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 781--><p class="indent" > <pre class="verbatim" id="verbatim-30"> struct net_device *dev; char *name; dev = kread(&(skb->dev)); name = kread(&(dev->name)); </pre> <!--l. 787--><p class="nopar" ></dd></dl> <!--l. 790--><p class="noindent" >The memory locations reserved for input and output values are provided to a function using macros named <span class="cmtt-10">STAP</span><span class="cmtt-10">_ARG</span><span class="cmtt-10">_foo</span><a id="dx5-27001"></a> (for arguments named <span class="cmtt-10">foo</span>) and <span class="cmtt-10">STAP</span><span class="cmtt-10">_RETVALUE</span><a id="dx5-27002"></a>. Errors may be signalled with <span class="cmtt-10">STAP</span><span class="cmtt-10">_ERROR</span>. Output may be written with <span class="cmtt-10">STAP</span><span class="cmtt-10">_PRINTF</span>. The function may return early with <span class="cmtt-10">STAP</span><span class="cmtt-10">_RETURN</span>. Here are some examples: <!--l. 797--><p class="noindent" > <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 797--><p class="indent" > <pre class="verbatim" id="verbatim-31"> function integer_ops:long (val) %{   STAP_PRINTF("%d\n", STAP_ARG_val);   STAP_RETVALUE = STAP_ARG_val + 1;   if (STAP_RETVALUE == 4)       STAP_ERROR("wrong guess: %d", (int) STAP_RETVALUE);   if (STAP_RETVALUE == 3)       STAP_RETURN(0);   STAP_RETVALUE ++; %} function string_ops:string (val) %{   strlcpy (STAP_RETVALUE, STAP_ARG_val, MAXSTRINGLEN);   strlcat (STAP_RETVALUE, "one", MAXSTRINGLEN);   if (strcmp (STAP_RETVALUE, "three-two-one"))       STAP_RETURN("parameter should be three-two-"); %} function no_ops () %{     STAP_RETURN(); /* function inferred with no return value */ %} </pre> <!--l. 817--><p class="nopar" ></dd></dl> <!--l. 820--><p class="noindent" >The function argument and return value types should be stated if the translator cannot infer them from usage. The translator does not analyze the embedded C code within the function. <!--l. 824--><p class="noindent" >You should examine C code generated for ordinary script language functions to write compatible embedded-C. Usually, all SystemTap functions and probes run with interrupts disabled, thus you cannot call functions that might sleep within the embedded C. <!--l. 829--><p class="noindent" > <h4 class="subsectionHead"><span class="titlemark">3.7 </span> <a id="x5-280003.7"></a>Embedded C pragma comments</h4> <!--l. 831--><p class="noindent" >Embedded C blocks may contain various markers to assert optimization and safety properties. <ul class="itemize1"> <li class="itemize"> <!--l. 835--><p class="noindent" ><span class="obeylines-h"><span class="verb"><span class="cmtt-10">/*</span><span class="cmtt-10"> pure</span><span class="cmtt-10"> */</span></span></span> means that the C code has no side effects and may be elided entirely if its value is not used by script code. </li> <li class="itemize"> <!--l. 837--><p class="noindent" ><span class="obeylines-h"><span class="verb"><span class="cmtt-10">/*</span><span class="cmtt-10"> stable</span><span class="cmtt-10"> */</span></span></span> means that the C code always has the same value (in any given probe handler invocation), so repeated calls may be automatically replaced by memoized values. Such functions must take no parameters, and also be <span class="obeylines-h"><span class="verb"><span class="cmtt-10">/*</span><span class="cmtt-10"> pure</span><span class="cmtt-10"> */</span></span></span>. </li> <li class="itemize"> <!--l. 841--><p class="noindent" ><span class="obeylines-h"><span class="verb"><span class="cmtt-10">/*</span><span class="cmtt-10"> unprivileged</span><span class="cmtt-10"> */</span></span></span> means that the C code is so safe that even unprivileged users are permitted to use it. (This is useful, in particular, to define an embedded-C function inside a tapset that may be used by unprivileged code.) </li> <li class="itemize"> <!--l. 845--><p class="noindent" ><span class="obeylines-h"><span class="verb"><span class="cmtt-10">/*</span><span class="cmtt-10"> myproc-unprivileged</span><span class="cmtt-10"> */</span></span></span> means that the C code is so safe that even unprivileged users are permitted to use it, provided that the target of the current probe is within the user’s own process. </li> <li class="itemize"> <!--l. 849--><p class="noindent" ><span class="obeylines-h"><span class="verb"><span class="cmtt-10">/*</span><span class="cmtt-10"> guru</span><span class="cmtt-10"> */</span></span></span> means that the C code is so unsafe that a systemtap user must specify <span class="obeylines-h"><span class="verb"><span class="cmtt-10">-g</span></span></span> (guru mode) to use this, even if the C code is being exported from a tapset. </li> <li class="itemize"> <!--l. 852--><p class="noindent" ><span class="obeylines-h"><span class="verb"><span class="cmtt-10">/*</span><span class="cmtt-10"> unmangled</span><span class="cmtt-10"> */</span></span></span>, used in an embedded-C function, means that the legacy (pre-1.8) argument access syntax should be made available inside the function. Hence, in addition to <span class="obeylines-h"><span class="verb"><span class="cmtt-10">STAP_ARG_foo</span></span></span> and <span class="obeylines-h"><span class="verb"><span class="cmtt-10">STAP_RETVALUE</span></span></span> one can use <span class="obeylines-h"><span class="verb"><span class="cmtt-10">THIS->foo</span></span></span> and <span class="obeylines-h"><span class="verb"><span class="cmtt-10">THIS->__retvalue</span></span></span> respectively inside the function. This is useful for quickly migrating code written for SystemTap version 1.7 and earlier. </li> <li class="itemize"> <!--l. 859--><p class="noindent" ><span class="obeylines-h"><span class="verb"><span class="cmtt-10">/*</span><span class="cmtt-10"> unmodified-fnargs</span><span class="cmtt-10"> */</span></span></span> in an embedded-C function, means that the function arguments are not modified inside the function body. </li> <li class="itemize"> <!--l. 861--><p class="noindent" ><span class="obeylines-h"><span class="verb"><span class="cmtt-10">/*</span><span class="cmtt-10"> string</span><span class="cmtt-10"> */</span></span></span> in embedded-C expressions only, means that the expression has <span class="obeylines-h"><span class="verb"><span class="cmtt-10">const</span><span class="cmtt-10"> char</span><span class="cmtt-10"> *</span></span></span> type and should be treated as a string value, instead of the default long numeric.</li></ul> <!--l. 866--><p class="noindent" > <h4 class="subsectionHead"><span class="titlemark">3.8 </span> <a id="x5-290003.8"></a>Accessing script level global variables</h4> <!--l. 868--><p class="noindent" >Script level global variables may be accessed in embedded-C functions and blocks. To read or write the global variable <span class="cmbx-10">var</span>, the <span class="obeylines-h"><span class="verb"><span class="cmtt-10">/*</span><span class="cmtt-10"> pragma:read:var</span><span class="cmtt-10"> */</span></span></span> or <span class="obeylines-h"><span class="verb"><span class="cmtt-10">/*</span><span class="cmtt-10"> pragma:write:var</span><span class="cmtt-10"> */</span></span></span> marker must be first placed in the embedded-C function or block. This provides the macros <span class="obeylines-h"><span class="verb"><span class="cmtt-10">STAP_GLOBAL_GET_*</span></span></span> and <span class="obeylines-h"><span class="verb"><span class="cmtt-10">STAP_GLOBAL_SET_*</span></span></span> macros to allow reading and writing, respectively. For example: <dl class="list1"><dt class="list"> </dt><dd class="list"> <!--l. 874--><p class="indent" > <pre class="verbatim" id="verbatim-32"> global var global var2[100] function increment() %{     /* pragma:read:var */ /* pragma:write:var */     /* pragma:read:var2 */ /* pragma:write:var2 */     STAP_GLOBAL_SET_var(STAP_GLOBAL_GET_var()+1); //var++     STAP_GLOBAL_SET_var2(1, 1, STAP_GLOBAL_GET_var2(1, 1)+1); //var2[1,1]++ %} </pre> <!--l. 884--><p class="nopar" ></dd></dl> <!--l. 886--><p class="noindent" >Variables may be read and set in both embedded-C functions and expressions. Strings returned from embedded-C code are decayed to pointers. Variables must also be assigned at script level to allow for type inference. Map assignment does not return the value written, so chaining does not work. <!--l. 891--><div class="crosslinks"><p class="noindent">[<a href="langrefse4.html" >next</a>] [<a href="langrefse2.html" >prev</a>] [<a href="langrefse2.html#taillangrefse2.html" >prev-tail</a>] [<a href="langrefse3.html" >front</a>] [<a href="langref.html#langrefse3.html" >up</a>] </p></div> <!--l. 891--><p class="noindent" ><a id="taillangrefse3.html"></a> </body></html> 
 |