4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1994, by Sun Microsytems, Inc.
26 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include <string.h> /* for strerror() */
32 #include <sys/types.h>
41 #include <tnf/tnfctl.h>
43 extern tnfctl_handle_t
*g_hndl
;
45 typedef struct _pidlist
{
47 struct _pidlist
*next
;
50 static boolean_t
check_kernelmode(tnfctl_trace_attrs_t
*attrs_p
);
53 check_kernelmode(tnfctl_trace_attrs_t
*attrs_p
)
55 extern int g_kernelmode
;
59 (void) fprintf(stderr
, gettext(
60 "This command is only available "
61 "in kernel mode (prex invoked with the -k flag)\n"));
65 err
= tnfctl_trace_attrs_get(g_hndl
, attrs_p
);
67 (void) fprintf(stderr
, gettext(
68 "error on checking trace attributes : %s\n"),
69 tnfctl_strerror(err
));
77 * Print trace buffer status (is one allocated, and if so, how big is it.
82 tnfctl_trace_attrs_t attrs
;
84 if (check_kernelmode(&attrs
))
86 if (attrs
.trace_buf_state
== TNFCTL_BUF_NONE
) {
87 (void) printf(gettext("No trace buffer allocated\n"));
89 (void) printf(gettext("Trace buffer size is %d bytes\n"),
90 attrs
.trace_buf_size
);
91 if (attrs
.trace_buf_state
== TNFCTL_BUF_BROKEN
) {
92 (void) printf(gettext("Tracing system has failed -- "
93 "tracing suspended\n"));
100 * Allocate a trace buffer. Check for reasonable size; reject if there's
104 prbk_buffer_alloc(int size
)
106 tnfctl_errcode_t err
;
107 tnfctl_trace_attrs_t attrs
;
109 if (check_kernelmode(&attrs
))
112 if (attrs
.trace_buf_state
!= TNFCTL_BUF_NONE
) {
113 (void) fprintf(stderr
,
114 gettext("There is already a buffer allocated\n"));
117 if (size
< attrs
.trace_min_size
) {
118 (void) fprintf(stderr
, gettext(
119 "Size %d is less than the minimum buffer size of %d -- "
120 "buffer size set to %d bytes\n"),
121 size
, attrs
.trace_min_size
, attrs
.trace_min_size
);
122 size
= attrs
.trace_min_size
;
125 err
= tnfctl_buffer_alloc(g_hndl
, NULL
, size
);
127 (void) fprintf(stderr
,
128 gettext("error in allocating buffer: %s\n"),
129 tnfctl_strerror(err
));
133 /* get the trace attributes again */
134 if (check_kernelmode(&attrs
))
136 (void) printf(gettext("Buffer of size %d bytes allocated\n"),
137 attrs
.trace_buf_size
);
142 * Deallocate the kernel's trace buffer.
145 prbk_buffer_dealloc()
147 tnfctl_errcode_t err
;
149 if (check_kernelmode(NULL
))
152 err
= tnfctl_buffer_dealloc(g_hndl
);
154 case (TNFCTL_ERR_NONE
):
155 (void) printf(gettext("buffer deallocated\n"));
157 case (TNFCTL_ERR_NOBUF
):
158 (void) fprintf(stderr
,
159 gettext("There is no buffer to deallocate\n"));
161 case (TNFCTL_ERR_BADDEALLOC
):
162 (void) fprintf(stderr
,
163 gettext("Can't deallocate the buffer when "
164 "tracing is active\n"));
167 (void) fprintf(stderr
,
168 gettext("error in deleting buffer: %s\n"),
169 tnfctl_strerror(err
));
176 * Process filter routines.
178 * Process id sets are encoded as "pidlists": a linked list of pids.
179 * In a feeble attempt at encapsulation, the pidlist_t type is private
180 * to this file; prexgram.y manipulates pidlists only as opaque handles.
184 * Add the given pid (new) to the pidlist (pl).
187 prbk_pidlist_add(void *pl
, int new)
190 pidlist_t
*npl
= (pidlist_t
*) malloc(sizeof (*npl
));
193 (void) fprintf(stderr
,
194 gettext("Out of memory -- can't process pid %d\n"),
204 * Add the pids in the given pidlist to the process filter list.
205 * For each pid, check whether it's already in the filter list,
206 * and whether the process exists.
209 prbk_pfilter_add(void *pl
)
211 pidlist_t
*ppl
= (pidlist_t
*) pl
;
213 tnfctl_errcode_t err
;
215 if (check_kernelmode(NULL
))
217 while (ppl
!= NULL
) {
218 err
= tnfctl_filter_list_add(g_hndl
, ppl
->pid
);
220 (void) fprintf(stderr
, gettext("Process %ld: %s\n"),
221 ppl
->pid
, tnfctl_strerror(err
));
230 * Drop the pids in the given pidlist from the process filter list.
231 * For each pid, complain if it's not in the process filter list;
232 * and if the process no longer exists (and hence has already implicitly
233 * been dropped from the process filter list), say so.
236 prbk_pfilter_drop(void *pl
)
238 pidlist_t
*ppl
= (pidlist_t
*) pl
;
240 tnfctl_errcode_t err
;
242 if (check_kernelmode(NULL
))
245 while (ppl
!= NULL
) {
247 err
= tnfctl_filter_list_delete(g_hndl
, tmp
->pid
);
249 case (TNFCTL_ERR_NONE
):
251 case (TNFCTL_ERR_BADARG
):
252 (void) fprintf(stderr
,
253 gettext("Process %ld is not being traced\n"),
256 case (TNFCTL_ERR_NOPROCESS
):
257 (void) printf(gettext("Process %ld has exited\n"),
261 (void) fprintf(stderr
, gettext("Process %ld: %s\n"),
262 tmp
->pid
, tnfctl_strerror(err
));
271 * Turn process filter mode on or off. The process filter is maintained
272 * even when process filtering is off, but has no effect: all processes
276 prbk_set_pfilter_mode(boolean_t onoff
)
278 tnfctl_errcode_t err
;
280 if (check_kernelmode(NULL
))
282 err
= tnfctl_filter_state_set(g_hndl
, onoff
);
284 (void) fprintf(stderr
, gettext("pfilter: %s\n"),
285 tnfctl_strerror(err
));
291 * Report whether process filter mode is currently on or off, and
292 * dump the current process filter set.
295 prbk_show_pfilter_mode()
297 tnfctl_errcode_t err
;
298 tnfctl_trace_attrs_t attrs
;
303 if (check_kernelmode(&attrs
))
305 (void) printf(gettext("Process filtering is %s\n"),
306 attrs
.filter_state
? "on" : "off");
307 err
= tnfctl_filter_list_get(g_hndl
, &pids_p
, &pid_count
);
309 (void) fprintf(stderr
,
310 gettext("error in getting process filter list: %s\n"),
311 tnfctl_strerror(err
));
314 (void) printf(gettext("Process filter set is "));
316 (void) printf("empty.\n");
320 for (i
= 0; i
< pid_count
; i
++, cur_pid
++) {
321 (void) printf("%ld%s", *cur_pid
,
322 (i
!= (pid_count
- 1)) ? ", " : "}\n");
328 * Check for process filtering on with empty pid filter.
331 prbk_warn_pfilter_empty(void)
333 tnfctl_errcode_t err
;
336 tnfctl_trace_attrs_t attrs
;
338 if (check_kernelmode(&attrs
))
340 if (attrs
.filter_state
) {
341 err
= tnfctl_filter_list_get(g_hndl
, &pids_p
, &pid_count
);
343 (void) fprintf(stderr
,
344 gettext("error in getting process filter list: %s\n"),
345 tnfctl_strerror(err
));
349 (void) fprintf(stderr
,
350 gettext("Warning: Process filtering on, \
351 but pid filter list is empty\n"));
357 * Turn kernel tracing on or off.
360 prbk_set_tracing(boolean_t onoff
)
362 tnfctl_errcode_t err
;
364 if (check_kernelmode(NULL
))
367 err
= tnfctl_trace_state_set(g_hndl
, onoff
);
369 (void) fprintf(stderr
,
370 gettext("error in setting tracing state: %s\n"),
371 tnfctl_strerror(err
));
376 * Show whether kernel tracing is currently on or off.
381 tnfctl_trace_attrs_t attrs
;
383 if (check_kernelmode(&attrs
))
385 (void) printf(gettext("Tracing is %s\n"),
386 attrs
.trace_state
? "on" : "off");