3 *
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
7 * with the License.
8 *
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.
13 *
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]
19 *
21 */
23 * Copyright (c) 1994, by Sun Microsytems, Inc.
24 */
26#pragma ident	"%Z%%M%	%I%	%E% SMI"
28#include <stdio.h>
29#include <strings.h>
30#include "expr.h"
31#include "y.tab.h"
34static char *helptopics[NUMHELPTOPICS] = {
35	"intro", "functions", "kernel_mode", "probe_spec", "processes",
36	"set_spec" };
37static char *helptopicstrings[NUMHELPTOPICS] = {
38/* help intro */
39"Introduction to prex\n"
41"prex is used to control probes in a target process, or in the kernel.\n"
42"If you are reading this help text, you have sucessfully invoked prex,\n"
43"by either connecting to an existing process (prex -p), a new\n"
44"process (prex myprogram), or to the kernel (prex -k).\n"
46"Most often, the user will want to enable some probes in the target,\n"
47"continue the target - either to completion, or until the target is\n"
48"interrupted - and then exit from prex to perform analysis on the resulting\n"
49"tracefile.  An ascii dump of the tracefile can be obtained using the\n"
50"tnfdump(1) command.\n"
52"The tracefile can be found in /tmp/trace-<pid> by default, or in a location\n"
53"of your choice, if you specify the -o option when you invoke prex.\n"
54"You can query the name of the current trace file by using the command.\n"
55"list tracefile.\n"
57"Upon invocation, prex reads commands from the files ~/.prexrc and\n"
58"./.prexrc (in that order).  The \"source\" command may be used to take\n"
59"commands from an arbitrary file or set of files.\n"
61"Help is available for a variety of topics, and for each prex command.\n"
62"Type help with no arguments for a list of available help topics.\n"
64"end of help for topic intro\n",
65/* help functions */
66"Probe Functions\n"
68"Note - probe functions are not available from kernel mode\n"
70"It is possible to use prex to connect functions to probe points, such that\n"
71"the function is invoked each time a given probe point is hit.  Currently,\n"
72"the only function available from prex is the &debug function, which prints\n"
73"out the arguments sent in to the probe, as well as the value (if any)\n"
74"associated with the sunw%debug attribute in the detail field.\n"
76"Relevant commands:\n"
77"    list fcns                    # list the defined probe functions\n"
78"    connect &debug name=myprobe  # attach probe &debug to probe myprobe\n"
79"    connect &debug $myset        # attach probe &debug to probes in $myset\n"
80"    clear name=myprobe           # disconnect probe functions from myprobe\n"
81"    clear $myset                 # disconnect probe functions from $myset\n"
83"end of help for topic functions\n",
84/* help kernel_mode */
85"Controlling Kernel Probes\n"
87"The Solaris kernel is instrumented with a small number of strategically\n"
88"placed probes, documented in tnf_kernel_probes(4).  The superuser can\n"
89"control these probes by running prex with the \"-k\" option.\n"
91"In kernel mode, trace output is written to an in-core buffer, rather\n"
92"than to a file on disk.  This buffer can be extracted with the tnfxtract(1)\n"
93"commmand.  This buffer must be set up before tracing can begin, through the\n"
94"use of the \"buffer alloc\" command.  After kernel tracing is complete (and\n"
95"after the buffer has been extracted), the buffer can be deallocated from\n"
96"prex using the \"buffer dealloc\" command.\n"
98"As in user mode, kernel probe control is accomplished using the commands\n"
99"\"trace\", \"untrace\", \"enable\", and \"disable\".  Additionally, in\n"
100"kernel mode, a \"master switch\" is provided to turn all tracing activity\n"
101"on or off.  This switch is toggled using the commands \"ktrace on\" and\n"
102"\"ktrace off\".  Unlike user mode, where the target is stopped while\n"
103"tracing paramaters are manipulated from prex, the kernel does not stop\n"
104"running during a tracing session.  Using the \"ktrace\" command, one can\n"
105"set up all tracing parameters in advance of a session, without actually\n"
106"writing trace records until a \"ktrace on\" command is given.\n"
108"Kernel mode also provides the ability to limit tracing to those kernel\n"
109"probes hit on behalf of a specific user process.  The pfilter command\n"
110"is provided to toggle process filtering on or off, and to specify the\n"
111"set of processes that comprise the filter.  If pid 0 is a member of the\n"
112"filter list, then any threads not associated with a process are included.\n"
114"Note that after a kernel tracing session, all tracing parameters are left\n"
115"as-is.  One should re-enter prex to disable and untrace probes, turn off\n"
116"process filtering, and deallocate the in-core trace buffer.\n"
118"Relevant Commands:\n"
119"    buffer alloc 2m    # allocate a 2M in-core buffer\n"
120"    enable $all        # enable all kernel probes\n"
121"    trace $all         # trace all kernel probes\n"
122"    ktrace on          # turn on kernel tracing\n"
123"    pfilter add 1234   # add pid 1234 to the filter list\n"
124"    pfilter on         # turn on process filtering\n"
125"    ktrace off         # turn off kernel tracing\n"
126"Also see tnfxtract(1), which is used to extract the in-core trace buffer\n"
127"to an on-disk tracefile.\n"
129"end of help for topic kernel_mode\n",
130/* help probe_spec */
131"Probe Specification\n"
133"Many prex commands operate on probes or sets of probes.  Probes are\n"
134"specified by a list of space-separated selectors of the form:\n"
135"      <attribute>=<value>\n"
136"If the \""
137"<attribute>=\" is omitted, the attribute defaults to \"keys=\".\n"
138"The \""
139"<value>\" can be either a string or an ed(1) regular expression\n"
140"enclosed in slashes.  Regular expressions in prex are unanchored, meaning\n"
141"that any value that contains the given regex as a substring is a valid\n"
142"match, regardless of position.  To anchor a regular expression, use \"^\"\n"
143"to match the beginning of a line, or \"$\" to match the end of a line.\n"
145"If a list of selectors is specified, an OR operation is applied - the\n"
146"resulting probe_spec includes probes that match any of the selectors.\n"
147"See the prex(1) man page for a complete specification of the accepted\n"
150"The \"list\" command is used to view available probes in the target,\n"
151"and to display their attributes. The \"trace\" and \"untrace\" commands\n"
152"determine whether a probe will write a trace record when hit.  The\n"
153"\"enable\" and \"disable\" commands indicate whether a probe will perform\n"
154"any action (such as a calling a connected function or creating a trace\n"
155"record) when hit.   Normally, a probe is enabled and traced for tracing,\n"
156"and disabled and untraced otherwise.  It is possible to enable a probe\n"
157"with tracing off to get debug output without writing trace records.\n"
159"Relevant Commands:\n"
160"   list probes $all         # list probes in set $all (all probes)\n"
161"   list probes file=test.c  # list probes with a specific attribute\n"
162"   list 'file' probes $all  # list the file attribute in all probes\n"
163"   list probes name=/^thr/  # list probes whose name attribute matches\n"
164"                            # the given regular expression\n"
165"   list probes name=/^thr/ keys=vm  # list probes matching either selector\n"
166"   enable name=/^thr/       # enable probes whose name matches this regex\n"
167"   trace $all               # trace all probes\n"
168"   untrace $myset           # untrace probes in set $myset\n"
170"end of help for topic probe_spec\n",
171/* help processes */
172"Controlling Processes with prex\n"
174"Prex is used to control probes in a given process, or in the kernel.\n"
175"The process which prex is to control is identified as an argument when\n"
176"prex is invoked.  If the \"-p"
177" <pid>\" switch is used, prex connects to the\n"
178"specified process.  Otherwise prex exec's the command supplied at the\n"
179"end of its argument list.  In either case, prex stops the target process\n"
180"immediately so that the user may set up probe control.\n"
182"Once probe control is set up (typically using the \"enable\" and \"trace\"\n"
183"commands), the process is continued using the \"continue\" command.  Prex\n"
184"remains attached to the target, and the user can force the target to\n"
185"stop again by typing control-C, at which time additional probe control\n"
186"directives may be given.\n"
188"Upon quitting from prex, the target process is normally resumed if prex\n"
189"attached to it, or killed if prex invoked it.  An optional argument may\n"
190"be given with the \"quit\" command to explicitly specify whether to kill\n"
191"the target, continue it, or leave it suspended.\n"
193"If the target forks, any probe that the child encounters will be logged to\n"
194"the same trace file as the parent.  If the child calls exec, it will no\n"
195"longer be traced.\n"
197"In kernel mode (prex -k), process filtering may be enabled, to limit\n"
198"tracing to those kernel probes hit on behalf of a specific process or\n"
199"set of processes.  Kernel-mode process filtering is controlled using\n"
200"the \"pfilter\" command.\n"
203"Relevant Commands:\n"
204"    continue               # continue target (user mode only)\n"
205"    Control-C              # stop target (user mode only)\n"
206"    quit resume            # quit prex, continue target\n"
207"    quit suspend           # quit prex, suspend target\n"
208"    quit kill              # quit prex, kill target\n"
209"    quit                   # quit prex, default action\n"
210"# Note: pfilter commands apply only to kernel mode\n"
211"    pfilter                # show pfilter status\n"
212"    pfilter on             # turn on process filter mode\n"
213"    pfilter off            # turn off process filter mode\n"
214"    pfilter add 1234       # add to process filter pid list\n"
215"    pfilter delete 1234    # delete from process filter pid list\n"
217"end of help for topic processes\n",
218/* help set_spec */
219"Specifying Probe Sets\n"
221"Prex provides the ability to define named sets of probes to simplify\n"
222"commands operating on multiple probes.  The set \"$all\" is predefined,\n"
223"as the set of all probes in the target.  A set is defined using the\n"
224"\"create\" command, and can be used as an argument to the \"list\",\n"
225"\"enable\", \"disable\", \"trace\", \"untrace\", \"connect\" and\n"
226"\"clear\" commands.\n"
228"Relevant Commands:\n"
229"    create $myset name=/^thr/        # create a set\n"
230"    list probes $myset               # list probes in a set\n"
231"    list sets                        # list defined sets\n"
232"    enable $myset                    # enable a set of probes\n"
233"    trace $myset                     # trace a set of probes\n"
235"end of help for topic set_spec\n"
238static char *helpstr_continue =
240"Usage:  continue\n"
242"\"continue\" is used to resume execution of the target process.  This\n"
243"command is not available in kernel mode, since the kernel is never stopped.\n"
245"end of help for cmd continue\n";
246static char *helpstr_disable =
248"Usage: disable <probe_spec>|<set_spec>\n"
250"\"disable\" is used to to turn off all tracing activity associated with a\n"
251"probe or a set of probes.\n"
253"End of help for cmd disable\n";
254static char *helpstr_enable=
256"Usage: enable <probe_spec>|<set_spec>\n"
258"\"enable\" is used to specify that any activity associated with the probe\n"
259"or probes will be performed when the probe is hit.  This includes connected\n"
260"probe functions as well as the generation of trace records.  Note that in\n"
261"order for a probe to generate a trace record, it must be \"traced\" as well\n"
262"as enabled.\n"
264"End of help for cmd enable\n";
265static char *helpstr_help =
267"Usage: help [<cmd>|<topic>]\n"
269"\"help\" lists all available help topics when run without any arguments.\n"
270"If a valid topic or command-name is supplied as an argument, help text for\n"
271"the topic or command is displayed.\n"
273"End of help for cmd help\n";
274static char *helpstr_list =
276"Usage: list probes <probe_spec>|<set_spec> \n"
277"       list <attrs> probes <probe_spec>|<set_spec>\n"
278"       list sets\n"
279"       list fcns\n"
280"       list history\n"
281"       list tracefile\n"
283"\"list\" displays information about currently defined probes, sets, and\n"
284"probe functions.  When listing probes, one can limit the output to a\n"
285"desired set of attributes by specifying an attribute list as a set of\n"
286"strings.  If an attribute is also a reserved word (such as \"trace\", it\n"
287"must be enclosed in single quotes.  For example:\n"
289"       list file 'trace' probes $all\n"
291"\"list\" history lists the probe control commands history, and\n"
292"\"list\" tracefile displays the current trace file name.\n"
294"End of help for cmd list\n";
295static char *helpstr_quit =
297"Usage: quit\n"
298"       quit kill\n"
299"       quit resume\n"
300"       quit suspend\n"
302"The \"quit\" command exits prex, leaving the target in a state specified\n"
303"by the user, or taking a default action if no instructions are specified.\n"
304"An optional argument may be used to indicated that the target should be\n"
305"killed, resumed, or left suspended.  If no argument is supplied, then\n"
306"prex's default behavior is to resume a process to which it had attached,\n"
307"and to kill a process which it had invoked.\n"
309"End of help for cmd quit\n";
310static char *helpstr_source =
312"Usage: source <filename>\n"
314"The \"source\" command is used to invoke a set of prex commands stored in\n"
315"a file.  A sourced file may in turn source other files.  The files\n"
316"~/.prexrc and ./.prexrc are sourced automatically (in that order) when prex\n"
317"is invoked, and may be used to store commonly used probe and set\n"
318"specifications, or probe control directives.  Commands in sourced files\n"
319"may override the effects of commands in previously sourced files.\n"
321"End of help for cmd source\n";
322static char *helpstr_trace =
324"Usage: trace <probe_spec>|<set_spec>\n"
326"\"trace\" is used to turn on tracing for the specified probe or probes.\n"
327"A \"traced\" probe that is also \"enabled\" will generate a trace record\n"
328"when it is hit.\n"
330"End of help for cmd trace\n";
331static char *helpstr_untrace =
333"Usage: untrace <probe_spec>|<set_spec>\n"
335"\"untrace\" turns tracing off for the specified probe or probes.  A probe\n"
336"will not generate a trace record when it is not traced, although connected\n"
337"probe functions will still be invoked as long as a probe is \"enabled\".\n"
339"End of help for cmd untrace\n";
340static char *helpstr_buffer =
342"Usage: buffer alloc <size>\n"
343"       buffer dealloc\n"
345"Note:  Kernel Mode Only\n"
347"\"buffer\" allocates or deallocates the in-core buffer used to hold\n"
348"kernel trace records.  Size can be specified in kilobytes or megabytes,\n"
349"by appending the character 'k' or 'm' to a numeric value (e.g. \"2m\").\n"
350"A buffer must be allocated prior to a kernel tracing session.  Once\n"
351"allocated, the buffer remains usable until deallocated, even through\n"
352"multiple invocations of prex.\n"
354"Before the buffer is deallocated, data may be extracted to an on-disk\n"
355"tracefile using tnfxtract(1).\n"
357"End of help for cmd buffer\n";
358static char *helpstr_ktrace =
360"Usage: ktrace on\n"
361"       ktrace off\n"
363"Note:  Kernel Mode Only\n"
365"\"ktrace\" toggles the master switch that indicates whether kernel\n"
366"tracing is active or inactive.  Since the kernel cannot be stopped while\n"
367"a tracing experiment is set up, \"ktrace\" is provided so that tracing\n"
368"can be set up as desired before any trace records are generated\n"
370"End of help for cmd ktrace\n";
371static char *helpstr_pfilter =
373"Usage: pfilter\n"
374"       pfilter on\n"
375"       pfilter off\n"
376"       pfilter add <pidlist>\n"
377"       pfilter delete <pidlist>\n"
379"Note:  Kernel Mode Only\n"
381"\"pfilter\" controls process filtering by toggling process-filter mode,\n"
382"and maintaining a list of process id's on which to filter.  When process\n"
383"filtering mode is on, tracing is limited to the kernel events hit on behalf\n"
384"of the processes in the pid list.  Process id 0 is used to represent all\n"
385"threads not associated with a process.\n"
387"When run without arguments, \"pfilter\" displays the current process-filter\n"
388"mode and pid list.  A process filter pid list can be maintained whether\n"
389"or not process filter mode is currently active.\n"
391"A process must exist in order to be added to the pid list, and the pid list\n"
392"is automatically updated to delete any processes that no longer exist.  If\n"
393"the pid list becomes empty while process filtering is on, prex issues a\n"
394"warning.  Process filtering mode is persistent between invocations of prex\n"
395"so it should be turned off manually when a tracing experiment is complete.\n"
397"End of help for cmd pfilter\n";
398static char *helpstr_clear =
400"Usage: clear <probe_spec>|<set_spec>\n"
402"Note:  Not available in Kernel Mode\n"
404"\"clear\" disconnects any probe functions that have previously been\n"
405"connected to a probe or group of probes using the \"connect\" command.\n"
406"The \"clear\" command cannot be used in kernel mode, since probe functions\n"
407"are not available for kernel probes.\n"
409"End of help for cmd clear\n";
410static char *helpstr_connect =
412"Usage: connect <function> <probe_spec>|<set_spec>\n"
414"Note:  Not available in Kernel Mode\n"
416"\"connect\" connects a probe function to a probe or group of probes.\n"
417"Currently, the only probe function available from prex is \"&debug\", which\n"
418"prints out the arguments sent in to the probe, as well as the value (if\n"
419"any) associated with the sunw%debug attribute in the detail field.\n"
420"In order for a probe function to be invoked, the probe to which the\n"
421"function is attached must be \"enabled\", but need not be \"traced\".\n"
423"The \"clear\" command is available to disconnect a probe function from\n"
424"a probe or group of probes.\n"
426"End of help for cmd connect\n";
428static char *helpstr =
430"Usage: help [topic|command]\n"
433"\tintro         functions    kernel_mode\n"
434"\tprobe_spec    processes    set_spec\n"
436"User and Kernel Mode Commands\n"
437"\tdisable       enable       help       list\n"
438"\tquit          source       trace      untrace\n"
440"Additional user-mode-only commands\n"
441"\tclear         connect      continue\n"
443"Additional kernel-mode-only (prex -k) commands\n"
444"\tbuffer        ktrace       pfilter\n"
448static char	*oldhelpstr =
449"grammar for non-terminals:\n"
452"filename ::=	QUOTED_STR\n"
454"selector_list ::= 	/* empty */ |\n"
455"		<selector_list> <selector>\n"
457"spec_list ::=	/*empty */ |\n"
458"		<spec_list> <spec>\n"
460"selector ::=	<spec>=<spec> |		/* whitespace around '=' optional */\n"
461"		<spec>			/* keys attribute is default */\n"
463"spec ::= 	IDENT |\n"
464"		QUOTED_STR |\n"
465"		REGEXP\n"
467"pidlist ::=	<pid> |\n"
468"		<pid> ',' <pidlist>\n"
469"pid ::=		INT\n"
471"Reg-exps to match terminals:\n"
474"IDENT 		= [a-zA-Z_\\.%]{[a-zA-Z0-9_\\.%]}+ \n"
475"QUOTED_STR	= '[^\\n']*'		/* any string in single quotes	*/\n"
476"REGEXP		= /[^\\n/]/		/* reg-exp's have to be in / /	*/\n"
477"INT		= [0-9]+\n"
482"# set creation and set listing\n"
483"create $<set_name> <selector_list>\n"
484"list sets				# list the defined sets\n"
486"# function listing\n"
487"list fcns				# list the defined functions.\n"
489"# commands to connect and disconnect probe functions\n"
490"# (not available in kernel mode)\n"
491"connect &<fcn_handle> $<set_name>	# eg. connect &debug $all\n"
492"connect &<fcn_handle> <selector_list>\n"
494"# command to disconnect all connected probe functions\n"
495"# (not available in kernel mode)\n"
496"clear $<set_name>\n"
497"clear <selector_list>\n"
499"# commands to toggle the tracing mode\n"
500"trace $<set_name>\n"
501"trace <selector_list>\n"
502"untrace $<set_name>\n"
503"untrace <selector_list>\n"
505"# commands to enable and disable probes\n"
506"enable $<set_name>\n"
507"enable <selector_list>\n"
508"disable $<set_name>\n"
509"disable <selector_list>\n"
510"list history		# lists probe control commands issued\n"
511"list tracefile		# lists the current trace file name\n"
513"# commands to list probes or to list values\n"
514"list <spec_list> probes $<set_name>	#eg. list probes $all\n"
515"list <spec_list> probes <selector_list> #eg. list name probes file=test.c\n"
516"list values <spec_list>		# eg. list values keys\n"
518"# help command\n"
521"# source a file of prex commands\n"
522"source <filename>\n"
524"# process control - ^C stops target and returns to 'prex>' prompt\n"
525"# (In kernel mode, `continue' is a no-op, and 'quit' detaches prex\n"
526"# from the kernel.)\n"
527"continue		# continues target\n"
528"quit kill		# quit prex, kill target\n"
529"quit resume		# quit prex, continue target\n"
530"quit suspend		# quit prex, leave target suspended\n"
531"quit			# quit prex (continue or kill target)\n"
535"# Kernel mode commands\n"
536"# \"master switch\" enabling/disabling all tracing\n"
537"ktrace on		# Enabled probes will generate trace output\n"
538"ktrace off		# All trace output suppressed\n"
539"# Create, destroy, or show the size of the kernel trace buffer\n"
540"buffer [ alloc [ size ] | dealloc ]\n"
541"# Control per-process kernel trace filtering\n"
542"pfilter off		# Filtering off:  trace all processes\n"
543"pfilter on		# Filtering on:  trace only processes in filter set\n"
544"pfilter add <pidlist>		# Add specified process ids to the filter set\n"
545"pfilter delete <pidlist>	# Drop specified pids from the filter set\n"
552	(void) fputs(helpstr, stdout);
554}				/* end help */
557help_on_topic(char *topic)
559	int i;
561	if (topic && strlen(topic)) {
562		for (i = 0; i < NUMHELPTOPICS; i++)
563		if (strcmp(topic, helptopics[i]) == 0)
564		    break;
565		if (i < NUMHELPTOPICS)
566		    fputs(helptopicstrings[i], stdout);
567		else {
568			printf("No help for %s\n",  topic);
569			help();
570		}
571	}
576help_on_command(int cmd)
578	switch (cmd) {
579	case CONTINUE:
580		fputs(helpstr_continue, stdout);
581		break;
582	case DISABLE:
583		fputs(helpstr_disable, stdout);
584		break;
585	case ENABLE:
586		fputs(helpstr_enable, stdout);
587		break;
588	case HELP:
589		fputs(helpstr_help, stdout);
590		break;
591	case LIST:
592		fputs(helpstr_list, stdout);
593		break;
594	case QUIT:
595		fputs(helpstr_quit, stdout);
596		break;
597	case SOURCE:
598		fputs(helpstr_source, stdout);
599		break;
600	case TRACE:
601		fputs(helpstr_trace, stdout);
602		break;
603	case UNTRACE:
604		fputs(helpstr_untrace, stdout);
605		break;
606	case BUFFER:
607		fputs(helpstr_buffer, stdout);
608		break;
609	case KTRACE:
610		fputs(helpstr_ktrace, stdout);
611		break;
612	case PFILTER:
613		fputs(helpstr_pfilter, stdout);
614		break;
615	case CLEAR:
616		fputs(helpstr_clear, stdout);
617		break;
618	case CONNECT:
619		fputs(helpstr_connect, stdout);
620		break;
621	default:
622		fputs("No help for this command\n", stdout);
623		break;
624	}
626}				/* end help */