File: ns_simple.cpp

package info (click to toggle)
pcp 7.1.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 252,748 kB
  • sloc: ansic: 1,483,656; sh: 182,366; xml: 160,462; cpp: 83,813; python: 24,980; perl: 18,327; yacc: 6,877; lex: 2,864; makefile: 2,738; awk: 165; fortran: 60; java: 52
file content (164 lines) | stat: -rw-r--r-- 6,097 bytes parent folder | download
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
// SPDX-License-Identifier: BSD-2-Clause
#include <cstdio>
#include "common.h"
#include "../usdt.h"

/* Alright, USDT and USDT semaphores have global visibility and naming, but
 * C++ has its own quirks, so we'll test various cases with USDTs in global
 * and namespaced context.
 */

USDT_DEFINE_SEMA(global_exp_sema); /* definition */

USDT_DECLARE_SEMA(ns_exp_sema); /* declaration */
USDT_DECLARE_SEMA(nested_ns_exp_sema); /* declaration */

__weak __optimize void global_func(int x)
{
	USDT(test, global_no_sema);
	USDT_WITH_SEMA(test, global_imp_sema, x);

	printf("global: global_imp_sema is %s.\n", USDT_IS_ACTIVE(test, global_imp_sema) ? "ACTIVE" : "INACTIVE");

	USDT_WITH_EXPLICIT_SEMA(global_exp_sema, test, global_exp_sema_main, x);
	USDT_WITH_EXPLICIT_SEMA(ns_exp_sema, test, ns_exp_sema_sub_global, x);
	USDT_WITH_EXPLICIT_SEMA(nested_ns_exp_sema, test, nested_ns_exp_sema_sub_global, x);

	printf("global: global_exp_sema is %s.\n", USDT_SEMA_IS_ACTIVE(global_exp_sema) ? "ACTIVE" : "INACTIVE");
	printf("global: ns_exp_sema is %s.\n", USDT_SEMA_IS_ACTIVE(ns_exp_sema) ? "ACTIVE" : "INACTIVE");
	printf("global: nested_ns_exp_sema is %s.\n", USDT_SEMA_IS_ACTIVE(nested_ns_exp_sema) ? "ACTIVE" : "INACTIVE");
}

namespace ns
{
	USDT_DEFINE_SEMA(ns_exp_sema);

	__weak __optimize void ns_func(int x)
	{
		USDT(test, ns_no_sema);
		USDT_WITH_SEMA(test, ns_imp_sema, x);

		printf("ns: ns_imp_sema is %s.\n", USDT_IS_ACTIVE(test, ns_imp_sema) ? "ACTIVE" : "INACTIVE");

		USDT_WITH_EXPLICIT_SEMA(global_exp_sema, test, global_exp_sema_sub_ns, x);
		USDT_WITH_EXPLICIT_SEMA(ns_exp_sema, test, ns_exp_sema_main, x);
		USDT_WITH_EXPLICIT_SEMA(nested_ns_exp_sema, test, nested_ns_exp_sema_sub_ns, x);

		printf("ns: global_exp_sema is %s.\n", USDT_SEMA_IS_ACTIVE(global_exp_sema) ? "ACTIVE" : "INACTIVE");
		printf("ns: ns_exp_sema is %s.\n", USDT_SEMA_IS_ACTIVE(ns_exp_sema) ? "ACTIVE" : "INACTIVE");
		printf("ns: nested_ns_exp_sema is %s.\n", USDT_SEMA_IS_ACTIVE(nested_ns_exp_sema) ? "ACTIVE" : "INACTIVE");
	}
}

namespace ns2 { namespace ns2_nested
{
	USDT_DEFINE_SEMA(nested_ns_exp_sema);

	__weak __optimize void nested_ns_func(int x)
	{
		USDT(test, nested_ns_no_sema);
		USDT_WITH_SEMA(test, nested_ns_imp_sema, x);

		printf("nested_ns: nested_ns_imp_sema is %s.\n", USDT_IS_ACTIVE(test, nested_ns_imp_sema) ? "ACTIVE" : "INACTIVE");

		USDT_WITH_EXPLICIT_SEMA(global_exp_sema, test, global_exp_sema_sub_nested_ns, x);
		USDT_WITH_EXPLICIT_SEMA(ns_exp_sema, test, ns_exp_sema_sub_nested_ns, x);
		USDT_WITH_EXPLICIT_SEMA(nested_ns_exp_sema, test, nested_ns_exp_sema_main, x);

		printf("nested_ns: global_exp_sema is %s.\n", USDT_SEMA_IS_ACTIVE(global_exp_sema) ? "ACTIVE" : "INACTIVE");
		printf("nested_ns: ns_exp_sema is %s.\n", USDT_SEMA_IS_ACTIVE(ns_exp_sema) ? "ACTIVE" : "INACTIVE");
		printf("nested_ns: nested_ns_exp_sema is %s.\n", USDT_SEMA_IS_ACTIVE(nested_ns_exp_sema) ? "ACTIVE" : "INACTIVE");
	}
}}

int main(int argc, char **argv)
{
	if (handle_args(argc, argv))
		return 0;

	global_func(1);
	ns::ns_func(2);
	ns2::ns2_nested::nested_ns_func(3);

	return 0;
}

const char *USDT_SPECS =
"test:global_no_sema base=BASE1 sema=0 argn=0 args=.\n"
"test:global_imp_sema base=BASE1 sema=SEMA1 argn=1 args=*.\n"
"test:global_exp_sema_main base=BASE1 sema=SEMA2 argn=1 args=*.\n"
"test:ns_exp_sema_sub_global base=BASE1 sema=SEMA3 argn=1 args=*.\n"
"test:nested_ns_exp_sema_sub_global base=BASE1 sema=SEMA4 argn=1 args=*.\n"

"test:ns_no_sema base=BASE1 sema=0 argn=0 args=.\n"
"test:ns_imp_sema base=BASE1 sema=SEMA5 argn=1 args=*.\n"
"test:global_exp_sema_sub_ns base=BASE1 sema=SEMA2 argn=1 args=*.\n"
"test:ns_exp_sema_main base=BASE1 sema=SEMA3 argn=1 args=*.\n"
"test:nested_ns_exp_sema_sub_ns base=BASE1 sema=SEMA4 argn=1 args=*.\n"

"test:nested_ns_no_sema base=BASE1 sema=0 argn=0 args=.\n"
"test:nested_ns_imp_sema base=BASE1 sema=SEMA6 argn=1 args=*.\n"
"test:global_exp_sema_sub_nested_ns base=BASE1 sema=SEMA2 argn=1 args=*.\n"
"test:ns_exp_sema_sub_nested_ns base=BASE1 sema=SEMA3 argn=1 args=*.\n"
"test:nested_ns_exp_sema_main base=BASE1 sema=SEMA4 argn=1 args=*.\n"
;

const char *UNTRACED_OUTPUT =
"global: global_imp_sema is INACTIVE.\n"
"global: global_exp_sema is INACTIVE.\n"
"global: ns_exp_sema is INACTIVE.\n"
"global: nested_ns_exp_sema is INACTIVE.\n"
"ns: ns_imp_sema is INACTIVE.\n"
"ns: global_exp_sema is INACTIVE.\n"
"ns: ns_exp_sema is INACTIVE.\n"
"ns: nested_ns_exp_sema is INACTIVE.\n"
"nested_ns: nested_ns_imp_sema is INACTIVE.\n"
"nested_ns: global_exp_sema is INACTIVE.\n"
"nested_ns: ns_exp_sema is INACTIVE.\n"
"nested_ns: nested_ns_exp_sema is INACTIVE.\n"
;

const char *BPFTRACE_SCRIPT =
"test:global_no_sema { triggered }\n"
"test:ns_no_sema { triggered }\n"
"test:nested_ns_no_sema { triggered }\n"

"test:global_imp_sema { x=%d -> arg0 }\n"
"test:ns_imp_sema { x=%d -> arg0 }\n"
"test:nested_ns_imp_sema { x=%d -> arg0 }\n"

/* attach to non-main USDTs with shared explicit semas; they should still activate all semas */
"test:global_exp_sema_sub_ns { x=%d -> arg0 }\n"
"test:ns_exp_sema_sub_nested_ns { x=%d -> arg0 }\n"
"test:nested_ns_exp_sema_sub_global { x=%d -> arg0 }\n"
;

const char *BPFTRACE_OUTPUT =
"test:global_no_sema: triggered\n"
"test:global_imp_sema: x=1\n"
"test:nested_ns_exp_sema_sub_global: x=1\n"

"test:ns_no_sema: triggered\n"
"test:ns_imp_sema: x=2\n"
"test:global_exp_sema_sub_ns: x=2\n"

"test:nested_ns_no_sema: triggered\n"
"test:nested_ns_imp_sema: x=3\n"
"test:ns_exp_sema_sub_nested_ns: x=3\n"
;

/* All semas are active, even if not all USDTs are attached, due to shared explicit semaphores. */
const char *TRACED_OUTPUT = ""
"global: global_imp_sema is ACTIVE.\n"
"global: global_exp_sema is ACTIVE.\n"
"global: ns_exp_sema is ACTIVE.\n"
"global: nested_ns_exp_sema is ACTIVE.\n"
"ns: ns_imp_sema is ACTIVE.\n"
"ns: global_exp_sema is ACTIVE.\n"
"ns: ns_exp_sema is ACTIVE.\n"
"ns: nested_ns_exp_sema is ACTIVE.\n"
"nested_ns: nested_ns_imp_sema is ACTIVE.\n"
"nested_ns: global_exp_sema is ACTIVE.\n"
"nested_ns: ns_exp_sema is ACTIVE.\n"
"nested_ns: nested_ns_exp_sema is ACTIVE.\n"
;