aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/acpi/amldb/debug.c
blob: cf5b7d3d156dd5625457cd18172f9cb417f92753 (plain) (blame)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*-
 * Copyright (c) 1999 Takanori Watanabe
 * Copyright (c) 1999, 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$Id: debug.c,v 1.19 2000/08/16 18:15:00 iwasaki Exp $
 *	$FreeBSD$
 */

#include <sys/param.h>

#include <aml/aml_name.h>
#include <aml/aml_amlmem.h>
#include <aml/aml_status.h>
#include <aml/aml_env.h>
#include <aml/aml_obj.h>
#include <aml/aml_evalobj.h>
#include <aml/aml_parse.h>
#include <aml/aml_region.h>
#include <aml/aml_store.h>
#include <aml/aml_common.h>

#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "debug.h"

static int
print_named_object(struct aml_name *name, va_list ap)
{

	aml_print_curname(name);
	printf("\n");

	return (0);	/* always return success to continue the search */
}

void 
aml_dbgr(struct aml_environ *env1, struct aml_environ *env2)
{
#define CMDBUFLEN	512
#define ARGBUFLEN	512
	static	char lastcommand[CMDBUFLEN];
	char	commandline[CMDBUFLEN];
	char	argbuf[7][ARGBUFLEN];
	char	*ptr, *method;
	char	*np, *ep;
	int 	i;
	int	argnum;
	struct	aml_name *name;
	union	aml_object argv[7], *retval;

	while (1) {
		fputs("AML>", stderr);
		fgets(commandline, 512, stdin);
		commandline[512 - 1] = '\n';	/* safety */
		if (feof(stdin)) {
			commandline[0] = 'q';
		}
		if (commandline[0] == '\n') {
			memcpy(commandline, lastcommand, sizeof commandline);
		}
		memcpy(lastcommand, commandline, sizeof commandline);
		switch (commandline[0]) {
		case 's':
			if (env2 != NULL) {
				env2->stat = aml_stat_step;
			}
			/* FALLTHROUGH */
		case 'n':
			env1->stat = aml_stat_step;
			return;
		case 'c':
			env1->stat = aml_stat_none;
			return;
		case 'q':
			env1->stat = aml_stat_panic;
			return;
		case 't':
			/* NULL terminate */
			ptr = &commandline[1];
			while (ptr[0] != '\n')
				ptr++;
			ptr[0] = '\0';

			/* move pointer to object name */
			ptr = &commandline[1];
			while (ptr[0] == ' ')
				ptr++;

			/* show current tree if no argument */
			if (ptr[0] == '\0') {
				aml_showtree(env1->curname, 0);
				goto show_variables;
			}
			/* start from root? */
			if (ptr[0] == '\\') {
				if (ptr[1] == '\0') {
					aml_showtree(aml_get_rootname(), 0);
					goto show_variables;
				}
				if ((name = aml_find_from_namespace(aml_get_rootname(), ptr))) {
					aml_showtree(name, 0);
					goto show_variables;
				}
			}
			if ((name = aml_find_from_namespace(env1->curname, ptr))) {
				aml_showtree(name, 0);
			}
show_variables:
			for (i = 0; i < 7; i++) {
				struct aml_name *tmp =
				aml_local_stack_getArgX(NULL, i);

				if (tmp == NULL || tmp->property == NULL) {
					break;
				}
				printf("  Arg%d    ", i);
				aml_showobject(tmp->property);
			}
			for (i = 0; i < 8; i++) {
				struct aml_name *tmp =
				aml_local_stack_getLocalX(i);

				if (tmp == NULL || tmp->property == NULL) {
					continue;
				}
				printf("  Local%d  ", i);
				aml_showobject(tmp->property);
			}
			break;
		case 'i':
			aml_debug_prompt_reginput =
			    (aml_debug_prompt_reginput == 0) ? 1 : 0;
			if (aml_debug_prompt_reginput)
				fputs("REGION INPUT ON\n", stderr);
			else
				fputs("REGION INPUT OFF\n", stderr);
			break;
		case 'o':
			aml_debug_prompt_regoutput =
			    (aml_debug_prompt_regoutput == 0) ? 1 : 0;
			if (aml_debug_prompt_regoutput)
				fputs("REGION OUTPUT ON\n", stderr);
			else
				fputs("REGION OUTPUT OFF\n", stderr);
			break;
		case 'm':
			memman_statistics(aml_memman);
			break;
		case 'r':
			/* NULL terminate */
			ptr = &commandline[1];
			while (ptr[0] != '\n')
				ptr++;
			ptr[0] = '\0';

			/* move pointer to method name */
			ptr = &commandline[1];
			while (ptr[0] == ' ')
				ptr++;

			if (ptr[0] == '\0') {
				break;
			}
			name = aml_find_from_namespace(aml_get_rootname(), ptr);
			if (name == NULL) {
				printf("%s:%d:aml_dbgr: not found name %s\n",
				    __FILE__, __LINE__, ptr);
				break;
			}
			if (name->property == NULL ||
			    name->property->type != aml_t_method) {
				printf("%s:%d:aml_dbgr: not method %s\n",
				    __FILE__, __LINE__, ptr);
				break;
			}
			aml_showobject(name->property);
			method = ptr;

			argnum = name->property->meth.argnum & 0x07;
			if (argnum) {
				fputs("  Enter argument values "
				      "(ex. number 1 / string foo). "
				      "'q' to quit.\n", stderr);
			}
			/* get and parse argument values */
			for (i = 0; i < argnum; i++) {
retry:
				fprintf(stderr, "  Arg%d ? ", i);
				if (read(0, argbuf[i], ARGBUFLEN) == 0) {
					fputs("\n", stderr);
					goto retry;
				}
				argbuf[i][ARGBUFLEN - 1] = '\n';
				if (argbuf[i][0] == 'q') {
					goto finish_execution;
				}
				if (argbuf[i][0] == '\n') {
					goto retry;
				}
				/* move pointer to the value */
				ptr = &argbuf[i][0];
				while (ptr[0] != ' ' && ptr[0] != '\n') {
					ptr++;
				}
				while (ptr[0] == ' ') {
					ptr++;
				}
				if (ptr[0] == '\n') {
					goto retry;
				}
				switch (argbuf[i][0]) {
				case 'n':
					argv[i].type = aml_t_num;
					np = ptr;
					if (ptr[0] == '0' &&
					    ptr[1] == 'x') {
						argv[i].num.number = strtoq(ptr, &ep, 16);
					} else {
						argv[i].num.number = strtoq(ptr, &ep, 10);
					}
					if (np == ep) {
						fputs("Wrong value for number.\n",
						    stderr);
						goto retry;
					}
					break;
				case 's':
					argv[i].type = aml_t_string;
					argv[i].str.needfree = 0;
					argv[i].str.string = ptr;
					/* NULL ternimate */
					while (ptr[0] != '\n') {
						ptr++;
					}
					ptr[0] = '\0';
					break;
				default:
					fputs("Invalid data type "
					      "(supports number or string only)\n",
					    stderr);
					goto retry;
				}
			}
			bzero(lastcommand, sizeof lastcommand);
			fprintf(stderr, "==== Running %s. ====\n", method);
			aml_local_stack_push(aml_local_stack_create());
			retval = aml_invoke_method_by_name(method, argnum, argv);
			aml_showobject(retval);
			aml_local_stack_delete(aml_local_stack_pop());
			fprintf(stderr, "==== %s finished. ====\n", method);
finish_execution:
			break;
		case 'f':
			/* NULL terminate */
			ptr = &commandline[1];
			while (ptr[0] != '\n')
				ptr++;
			ptr[0] = '\0';

			/* move pointer to object name */
			ptr = &commandline[1];
			while (ptr[0] == ' ')
				ptr++;

			aml_apply_foreach_found_objects(aml_get_rootname(),
			    ptr, print_named_object);
			break;
		case 'h':
			fputs("s	Single step\n"
			      "n	Step program\n"
			      "c	Continue program being debugged\n"
			      "q	Quit method execution\n"
			      "t	Show local name space tree and variables\n"
			      "i	Toggle region input prompt\n"
			      "o	Toggle region output prompt\n"
			      "m	Show memory management statistics\n"
			      "r	Run specified method\n"
			      "f	Find named objects from namespace.\n"
			      "h	Show this messsage\n", stderr);
			break;
		}
	}
}