Skip to content

Commit a729837

Browse files
committed
stap doc
1 parent 1169044 commit a729837

31 files changed

+6066
-1
lines changed

201309/20130912_02.md

Lines changed: 439 additions & 0 deletions
Large diffs are not rendered by default.

201309/20130912_03.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
## systemtap local & global variables
2+
3+
### 作者
4+
digoal
5+
6+
### 日期
7+
2013-09-12
8+
9+
### 标签
10+
PostgreSQL , Linux , systemtap , stap , dtrace , probe
11+
12+
----
13+
14+
## 背景
15+
前几天写过一篇blog, 关于systemtap优化模式下, 对有写, 没读, 或者没写没读的全局变量和本地变量做消除处理.
16+
17+
本文将讲一讲本地变量和全局变量的使用.
18+
19+
首先是变量的命名规范.
20+
21+
变量名可以包含数字,字母_,$符号. 但是必须以字母或_开头, 区分大小写.
22+
23+
24+
Identifiers for variables and functions are alphanumeric sequences, and may include the underscore (_) and the dollar sign ($) 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 are not allowed.
25+
26+
The translator performs type inference on all identifiers, including array indexes and function parameters. Inconsistent type-related use of identifiers results in an error.
27+
28+
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 var1 and var2 as global. The translator will infer a value type for each, and if the variable is used as an array, its key types.
29+
30+
```
31+
global var1[=<value>], var2[=<value>]
32+
```
33+
34+
$开头的变量为target variable, 或者context变量. 参考 :
35+
36+
http://blog.163.com/digoal@126/blog/static/16387704020138113455697/
37+
38+
39+
所以不适合用做本地变量或者全局变量名. 来看个例子 :
40+
41+
```
42+
[root@db-172-16-3-39 ~]# cat test.stp
43+
probe begin {
44+
$abc=222
45+
printf("%d\n", $abc)
46+
exit()
47+
}
48+
49+
[root@db-172-16-3-39 ~]# stap --vp 5 test.stp
50+
Parsed kernel "/lib/modules/2.6.18-348.12.1.el5/build/.config", containing 1977 tuples
51+
Parsed kernel /lib/modules/2.6.18-348.12.1.el5/build/Module.symvers, which contained 3546 vmlinux exports
52+
Searched: " /usr/share/systemtap/tapset/x86_64/*.stp ", found: 4, processed: 4
53+
Searched: " /usr/share/systemtap/tapset/*.stp ", found: 81, processed: 81
54+
Pass 1: parsed user script and 85 library script(s) using 146804virt/23708res/3008shr/21400data kb, in 160usr/10sys/173real ms.
55+
semantic error: unresolved target-symbol expression: identifier '$abc' at test.stp:2:3
56+
source: $abc=222
57+
^
58+
59+
semantic error: unresolved target-symbol expression: identifier '$abc' at :3:18
60+
source: printf("%d\n", $abc)
61+
^
62+
Pass 2: analysis failed. Try again with another '--vp 01' option.
63+
```
64+
65+
本地变量不需要加锁, 如果是多核CPU的情况下, 事件并发触发的话, 每个核管自己的本地变量, 互不干涉.
66+
67+
接下来要说的是全局变量, 全局变量写的时候是要加锁的, 所以有加锁超时的说法, 参见 :
68+
69+
```
70+
systemtap SAFETY AND SECURITY
71+
```
72+
73+
http://blog.163.com/digoal@126/blog/static/163877040201381021752228/
74+
75+
同时全局变量的作用范围, 不仅仅局限在本地脚本, 而是整个stap的检索范围中.
76+
77+
例如 :
78+
79+
在/tmp/p.stp中定义一个全局变量
80+
81+
```
82+
[root@db-172-16-3-39 ~]# cat /tmp/p.stp
83+
global a="abc"
84+
```
85+
86+
在test.stp中调用这个全局变量.
87+
88+
```
89+
[root@db-172-16-3-39 ~]# cat test.stp
90+
probe begin {
91+
_a$$vars=123
92+
B=123
93+
printf("%s, %d, %d, %d\n", a, _a$$vars, B, b)
94+
exit()
95+
}
96+
```
97+
98+
使用-I 加载自定义脚本库. 我们看到在tmp/p.stp中定义的a这个全局变量被打印出来了.
99+
100+
```
101+
[root@db-172-16-3-39 ~]# stap -I /tmp --vp 5 test.stp
102+
Parsed kernel "/lib/modules/2.6.18-348.12.1.el5/build/.config", containing 1977 tuples
103+
Parsed kernel /lib/modules/2.6.18-348.12.1.el5/build/Module.symvers, which contained 3546 vmlinux exports
104+
Searched: " /usr/share/systemtap/tapset/x86_64/*.stp ", found: 4, processed: 4
105+
Searched: " /usr/share/systemtap/tapset/*.stp ", found: 81, processed: 81
106+
Searched: " /tmp/*.stp ", found: 2, processed: 2
107+
Pass 1: parsed user script and 87 library script(s) using 146920virt/23716res/3012shr/21516data kb, in 160usr/20sys/173real ms.
108+
WARNING: never-assigned local variable 'b' (alternatives: _a$$vars B a): identifier 'b' at test.stp:4:46
109+
source: printf("%s, %d, %d, %d\n", a, _a$$vars, B, b)
110+
^
111+
abc, 123, 123, 0
112+
```
113+
114+
最后, 数组必须定义为全局变量.
115+
116+
## 参考
117+
1\. systemtap optimized for variables
118+
119+
http://blog.163.com/digoal@126/blog/static/16387704020138109459201/
120+
121+
2\. https://sourceware.org/systemtap/langref/Components_SystemTap_script.html
122+
123+
3\. systemtap probe point's "context variables" or "target variables"
124+
125+
http://blog.163.com/digoal@126/blog/static/16387704020138113455697/
126+
127+
4\. systemtap SAFETY AND SECURITY
128+
129+
http://blog.163.com/digoal@126/blog/static/163877040201381021752228/
130+
131+
132+
<a rel="nofollow" href="http://info.flagcounter.com/h9V1" ><img src="http://s03.flagcounter.com/count/h9V1/bg_FFFFFF/txt_000000/border_CCCCCC/columns_2/maxflags_12/viewers_0/labels_0/pageviews_0/flags_0/" alt="Flag Counter" border="0" ></a>
133+

0 commit comments

Comments
 (0)