搜索

此内容没有您所选择的语言版本。

1.4. Sample SystemTap scripts

download PDF
Following are some example scripts that illustrate the basic operation of SystemTap. For more examples, see the examples/small_demos/ directory in the source directory, the SystemTap wiki at http://sourceware.org/systemtap/wiki/HomePage, or the SystemTap War Stories at http://sourceware.org/systemtap/wiki/WarStories page.

1.4.1. Basic SystemTap syntax and control structures

The following code examples demonstrate SystemTap syntax and control structures.
global odds, evens

probe begin {
# "no" and "ne" are local integers
for (i = 0; i < 10; i++) {
if (i % 2) odds [no++] = i
else evens [ne++] = i
}

delete odds[2]
delete evens[3]
exit()
}

probe end {
foreach (x+ in odds)
printf ("odds[%d] = %d", x, odds[x])

foreach (x in evens-)
printf ("evens[%d] = %d", x, evens[x])
}
This prints:
odds[0] = 1
odds[1] = 3
odds[3] = 7
odds[4] = 9
evens[4] = 8
evens[2] = 4
evens[1] = 2
evens[0] = 0
Note that all variable types are inferred, and that all locals and globals are initialized.

1.4.2. Primes between 0 and 49

function isprime (x) {
if (x < 2) return 0
for (i = 2; i < x; i++) {
if (x % i == 0) return 0
if (i * i > x) break
}
return 1
}

probe begin {
for (i = 0; i < 50; i++)
if (isprime (i)) printf("%d\n", i)
exit()
}
This prints:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

1.4.3. Recursive functions

function fibonacci(i) {
if (i < 1) error ("bad number")
if (i == 1) return 1
if (i == 2) return 2
return fibonacci (i-1) + fibonacci (i-2)
}

probe begin {
printf ("11th fibonacci number: %d", fibonacci (11))
exit ()
}
This prints:
11th fibonacci number: 118
Any larger number input to the function may exceed the MAXACTION or MAXNESTING limits, which will be caught at run time and result in an error. For more about limits see Section Section 1.6, “Safety and security”.
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.