xref: /titanic_41/usr/src/cmd/tnf/prex/fcn.c (revision f8919bdadda3ebb97bd55cc14a16e0271ed57615)
1 /*
2  * CDDL HEADER START
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  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1994, by Sun Microsytems, Inc.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Includes
30  */
31 
32 #include <stdlib.h>
33 #include <string.h>
34 #include <libintl.h>
35 #include "queue.h"
36 #include "set.h"
37 #include "fcn.h"
38 #include "new.h"
39 #include "source.h"
40 
41 
42 /*
43  * Globals
44  */
45 
46 static queue_node_t g_fcnlist = {
47 	&g_fcnlist,
48 &g_fcnlist};
49 
50 /*
51  * Forward Declarations
52  */
53 
54 static void fcn_destroy(fcn_t * fcn_p);
55 static void fcn_print(FILE * stream, fcn_t * fcn_p);
56 
57 
58 /*
59  * fcn() - builds a function block and inserts it on the global list.
60  */
61 
62 #define	NSYMS	1
63 
64 void
65 fcn(char *name_p, char *entry_name_p)
66 {
67 	fcn_t		  *new_p;
68 	fcn_t		  *old_p;
69 
70 	/* does this setname exist already? */
71 	old_p = fcn_find(name_p);
72 	if (old_p)
73 		fcn_destroy(old_p);
74 
75 	/* create a new set */
76 	new_p = new(fcn_t);
77 	queue_init(&new_p->qn);
78 	new_p->name_p = name_p;
79 	new_p->entry_name_p = entry_name_p;
80 
81 #ifdef OLD
82 	/*
83 	 * allocate a target function block, and stuff the init and fini
84 	 * addrs
85 	 */
86 	prbstat = prb_targmem_alloc(g_procfd, sizeof (probe_funcs_t),
87 					&new_p->funcs_p);
88 	if (prbstat) {
89 		semantic_err(gettext("problem allocating target memory"));
90 		goto Error;
91 	}
92 	prbstat = prb_proc_write(g_procfd, new_p->funcs_p,
93 		&new_p->funcs, sizeof (probe_funcs_t));
94 	if (prbstat) {
95 		semantic_err(gettext(
96 				"setup problem, initial/final "
97 				"funcs in target memory"));
98 		goto Error;
99 	}
100 #endif
101 
102 	/* append the new set to the global list */
103 	(void) queue_append(&g_fcnlist, &new_p->qn);
104 
105 	return;
106 
107 Error:
108 	if (new_p)
109 		free(new_p);
110 	return;
111 
112 }				/* end fcn */
113 
114 
115 /*
116  * fcn_destroy() - destroys a fcn and related resources
117  */
118 
119 static void
120 fcn_destroy(fcn_t * fcn_p)
121 {
122 	if (!fcn_p)
123 		return;
124 
125 	/* remove ourselves from any list */
126 	if (!queue_isempty(&fcn_p->qn))
127 		(void) queue_remove(&fcn_p->qn);
128 
129 	if (fcn_p->name_p)
130 		free(fcn_p->name_p);
131 	if (fcn_p->entry_name_p)
132 		free(fcn_p->entry_name_p);
133 
134 	free(fcn_p);
135 
136 }				/* end fcn_destroy */
137 
138 
139 /*
140  * fcn_list() - pretty prints the global fcnlist
141  */
142 
143 void
144 fcn_list(void)
145 {
146 	fcn_t		  *fcn_p;
147 
148 	fcn_p = (fcn_t *) & g_fcnlist;
149 	while ((fcn_p = (fcn_t *) queue_next(&g_fcnlist, &fcn_p->qn))) {
150 		fcn_print(stdout, fcn_p);
151 	}
152 
153 }				/* end fcn_list */
154 
155 
156 /*
157  * fcn_print() - pretty prints a fcn
158  */
159 
160 static void
161 fcn_print(FILE * stream, fcn_t * fcn_p)
162 {
163 	if (!fcn_p)
164 		return;
165 
166 	(void) fprintf(stream, "&%-8s %-24s\n",
167 		fcn_p->name_p, fcn_p->entry_name_p);
168 
169 }				/* end fcn_print */
170 
171 
172 /*
173  * fcn_findname() - find the created name, given an entry name
174  */
175 
176 char		   *
177 fcn_findname(const char * const entry_p)
178 {
179 	fcn_t		  *fcn_p;
180 
181 	if (!entry_p)
182 		return (NULL);
183 
184 	fcn_p = (fcn_t *) & g_fcnlist;
185 	while ((fcn_p = (fcn_t *) queue_next(&g_fcnlist, &fcn_p->qn)))
186 		if (strcmp(entry_p, fcn_p->entry_name_p) == 0)
187 			return (fcn_p->name_p);
188 
189 	return (NULL);
190 
191 }				/* end fcn_findname */
192 
193 
194 /*
195  * fcn_find() - finds a fcn by name
196  */
197 
198 fcn_t		  *
199 fcn_find(char *fcnname_p)
200 {
201 	fcn_t		  *fcn_p;
202 
203 	if (!fcnname_p)
204 		return (NULL);
205 
206 	fcn_p = (fcn_t *) & g_fcnlist;
207 	while ((fcn_p = (fcn_t *) queue_next(&g_fcnlist, &fcn_p->qn)))
208 		if (strcmp(fcnname_p, fcn_p->name_p) == 0)
209 			return (fcn_p);
210 
211 	return (NULL);
212 
213 }				/* end set_find */
214