Systemtap supported data type (long,string,array,statistic), note don’t support numeric except long

1 minute read

背景

systemtap支持4种数据类型, 分别为long整型, 字符串, 数组, 统计类型.

数字只支持长整型, 不支持浮点.

例1 :

[root@db-172-16-3-39 ~]# cat test.stp  
function fun:long() %{  
  float a=1.9;  
  STAP_RETVALUE=a;  
%}  
probe begin {  
  printf("%d\n", fun())  
  exit()  
}  

输出为转换成长整型后的值, 1;

[root@db-172-16-3-39 ~]# stap -g test.stp   
1  

例2 :

printf("%d\n", fun()) 改成 printf("%f\n", fun()) 将报错.

[root@db-172-16-3-39 ~]# stap -g test.stp   
parse error: invalid or missing conversion specifier  
        saw: string '%f\n' at test.stp:6:10  
     source:   printf("%f\n", fun())  
                      ^  
1 parse error.  
Pass 1: parse failed.  Try again with another '--vp 1' option.  

因为输出格式只有%d,%s. 分别代表整型和字符串.

例3 :

改函数输出类型float.

[root@db-172-16-3-39 ~]# cat test.stp   
function fun:float() %{  
  float a=1.9;  
  STAP_RETVALUE=a;  
%}  
probe begin {  
  printf("%d\n", fun())  
  exit()  
}  

同样报错, 因为systemtap不支持这个类型.

[root@db-172-16-3-39 ~]# stap -g test.stp   
parse error: expected 'string' or 'long'  
        saw: identifier 'float' at test.stp:1:14  
     source: function fun:float() %{  
                          ^  
1 parse error.  
Pass 1: parse failed.  Try again with another '--vp 1' option.  

例4 :

接下来去掉内嵌C函数, 直接将1.9赋予本地变量.

[root@db-172-16-3-39 ~]# cat test.stp   
probe begin {  
  var = 1.9  
  printf("%d\n", var)  
  exit()  
}  

执行时报错, 因为.这个操作符是字符串连接符号. 而不是小数点.

[root@db-172-16-3-39 ~]# stap test.stp   
semantic error: type mismatch (string vs. long): identifier 'var' at test.stp:2:3  
        source:   var = 1.9  
                  ^  
  
semantic error: type was first inferred here (long): identifier 'var' at :3:18  
        source:   printf("%d\n", var)  
                                 ^  
  
semantic error: type mismatch (long vs. string): number '1' at :2:9  
        source:   var = 1.9  
                        ^  
  
semantic error: type mismatch (long vs. string): number '9' at :2:11  
        source:   var = 1.9  
                          ^  
  
Pass 2: analysis failed.  Try again with another '--vp 01' option.  

. 在systemtap中是字符串的连接符号.

例5 :

[root@db-172-16-3-39 ~]# vi test.stp   
probe begin {  
  var = 1.9  
  printf("%s\n", var)  
  exit()  
}  
[root@db-172-16-3-39 ~]# stap test.stp   
semantic error: type mismatch (long vs. string): number '1' at test.stp:2:9  
        source:   var = 1.9  
                        ^  
  
semantic error: type mismatch (long vs. string): number '9' at :2:11  
        source:   var = 1.9  
                          ^  
  
Pass 2: analysis failed.  Try again with another '--vp 01' option.  

例6 :

连接字符串时, 字符串必须使用”“双引号.

[root@db-172-16-3-39 ~]# vi test.stp   
probe begin {  
  var = "1"."9"  
  printf("%s\n", var)  
  exit()  
}  
[root@db-172-16-3-39 ~]# stap test.stp   
19  

var 在这里存储的是”19”字符串.

参考

1. https://sourceware.org/systemtap/langref/Language_elements.html

Flag Counter

digoal’s 大量PostgreSQL文章入口