Sun Dec 11 20:33:53 2011 UTC ()
Rework the debug printing to use functions instead of clutting the system call
code.


(reinoud)
diff -r1.12 -r1.13 src/sys/arch/usermode/usermode/syscall.c

cvs diff -r1.12 -r1.13 src/sys/arch/usermode/usermode/syscall.c (expand / switch to unified diff)

--- src/sys/arch/usermode/usermode/syscall.c 2011/12/09 17:20:21 1.12
+++ src/sys/arch/usermode/usermode/syscall.c 2011/12/11 20:33:52 1.13
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: syscall.c,v 1.12 2011/12/09 17:20:21 reinoud Exp $ */ 1/* $NetBSD: syscall.c,v 1.13 2011/12/11 20:33:52 reinoud Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca> 4 * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -17,27 +17,27 @@ @@ -17,27 +17,27 @@
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: syscall.c,v 1.12 2011/12/09 17:20:21 reinoud Exp $"); 30__KERNEL_RCSID(0, "$NetBSD: syscall.c,v 1.13 2011/12/11 20:33:52 reinoud Exp $");
31 31
32#include <sys/types.h> 32#include <sys/types.h>
33#include <sys/param.h> 33#include <sys/param.h>
34#include <sys/systm.h> 34#include <sys/systm.h>
35#include <sys/proc.h> 35#include <sys/proc.h>
36#include <sys/lwp.h> 36#include <sys/lwp.h>
37#include <sys/sched.h> 37#include <sys/sched.h>
38#include <sys/ktrace.h> 38#include <sys/ktrace.h>
39#include <sys/syscall.h> 39#include <sys/syscall.h>
40#include <sys/syscallvar.h> 40#include <sys/syscallvar.h>
41#include <sys/syscallargs.h> 41#include <sys/syscallargs.h>
42 42
43#include <sys/userret.h> 43#include <sys/userret.h>
@@ -61,26 +61,32 @@ child_return(void *arg) @@ -61,26 +61,32 @@ child_return(void *arg)
61 lwp_t *l = arg; 61 lwp_t *l = arg;
62// struct pcb *pcb = lwp_getpcb(l); 62// struct pcb *pcb = lwp_getpcb(l);
63 63
64 /* XXX? */ 64 /* XXX? */
65// frame->registers[0] = 0; 65// frame->registers[0] = 0;
66 66
67 aprint_debug("child return! lwp %p\n", l); 67 aprint_debug("child return! lwp %p\n", l);
68 userret(l); 68 userret(l);
69 ktrsysret(SYS_fork, 0, 0); 69 ktrsysret(SYS_fork, 0, 0);
70} 70}
71 71
72extern const char *const syscallnames[]; 72extern const char *const syscallnames[];
73 73
 74static void syscall_args_print(int code, int nargs, int argsize,
 75 register_t *args);
 76static void syscall_retvals_print(int code, int nargs, register_t *args,
 77 int error, register_t *rval);
 78
 79
74void 80void
75syscall(void) 81syscall(void)
76{  82{
77 lwp_t *l = curlwp; 83 lwp_t *l = curlwp;
78 const struct proc * const p = l->l_proc; 84 const struct proc * const p = l->l_proc;
79 const struct sysent *callp; 85 const struct sysent *callp;
80 struct pcb *pcb = lwp_getpcb(l); 86 struct pcb *pcb = lwp_getpcb(l);
81 ucontext_t *ucp = &pcb->pcb_userret_ucp; 87 ucontext_t *ucp = &pcb->pcb_userret_ucp;
82 register_t copyargs[2+SYS_MAXSYSARGS]; 88 register_t copyargs[2+SYS_MAXSYSARGS];
83 register_t *args; 89 register_t *args;
84 register_t rval[2]; 90 register_t rval[2];
85 uint32_t code, opcode; 91 uint32_t code, opcode;
86 uint nargs, argsize; 92 uint nargs, argsize;
@@ -93,80 +99,98 @@ syscall(void) @@ -93,80 +99,98 @@ syscall(void)
93 /* XXX do we want do do emulation? */ 99 /* XXX do we want do do emulation? */
94 md_syscall_get_opcode(ucp, &opcode); 100 md_syscall_get_opcode(ucp, &opcode);
95 md_syscall_get_syscallnumber(ucp, &code); 101 md_syscall_get_syscallnumber(ucp, &code);
96 code &= (SYS_NSYSENT -1); 102 code &= (SYS_NSYSENT -1);
97 103
98 callp = p->p_emul->e_sysent + code; 104 callp = p->p_emul->e_sysent + code;
99 nargs = callp->sy_narg; 105 nargs = callp->sy_narg;
100 argsize = callp->sy_argsize; 106 argsize = callp->sy_argsize;
101 107
102 args = copyargs; 108 args = copyargs;
103 rval[0] = rval[1] = 0; 109 rval[0] = rval[1] = 0;
104 error = md_syscall_getargs(l, ucp, nargs, argsize, args); 110 error = md_syscall_getargs(l, ucp, nargs, argsize, args);
105 111
 112#if 0
 113 aprint_debug("syscall no. %d, ", code);
 114 aprint_debug("nargs %d, argsize %d => ", nargs, argsize);
 115 dprintf_debug("syscall no. %d, ", code);
 116 dprintf_debug("nargs %d, argsize %d => ", nargs, argsize);
 117#endif
 118
 119 /*
 120 * TODO change the pre and post printing into functions so they can be
 121 * easily adjusted and dont clobber up this space
 122 */
 123
 124 if (!error)
 125 syscall_args_print(code, nargs, argsize, args);
 126
 127 md_syscall_inc_pc(ucp, opcode);
 128
 129 if (!error)
 130 error = (*callp->sy_call)(l, args, rval);
 131
 132 syscall_retvals_print(code, nargs, args, error, rval);
 133
 134//out:
 135 switch (error) {
 136 default:
 137 /* fall trough */
 138 case 0:
 139 md_syscall_set_returnargs(l, ucp, error, rval);
 140 /* fall trough */
 141 case EJUSTRETURN:
 142 break;
 143 case ERESTART:
 144 md_syscall_dec_pc(ucp, opcode);
 145 /* nothing to do */
 146 break;
 147 }
 148 //dprintf_debug("end of syscall : return to userland\n");
 149//if (code != 4) printf("userret() code %d\n", code);
 150 userret(l);
 151}
 152
 153
 154static void
 155syscall_args_print(int code, int nargs, int argsize, register_t *args)
 156{
106 if (code != 4) { 157 if (code != 4) {
107 printf("code %3d, nargs %d, argsize %3d\t%s(",  158 printf("code %3d, nargs %d, argsize %3d\t%s(",
108 code, nargs, argsize, syscallnames[code]); 159 code, nargs, argsize, syscallnames[code]);
109 switch (code) { 160 switch (code) {
110 case 5: 161 case 5:
111 printf("\"%s\", %"PRIx32", %"PRIx32"", (char *) (args[0]), (uint) args[1], (uint) args[2]); 162 printf("\"%s\", %"PRIx32", %"PRIx32"", (char *) (args[0]), (uint) args[1], (uint) args[2]);
112 break; 163 break;
113 case 33: 164 case 33:
114 printf("\"%s\", %"PRIx32"", (char *) (args[0]), (uint) args[1]); 165 printf("\"%s\", %"PRIx32"", (char *) (args[0]), (uint) args[1]);
115 break; 166 break;
116 case 50: 167 case 50:
117 printf("\"%s\"", (char *) (args[0])); 168 printf("\"%s\"", (char *) (args[0]));
118 break; 169 break;
119 case 58: 170 case 58:
120 printf("\"%s\", %"PRIx32", %"PRIx32"", (char *) (args[0]), (uint) (args[1]), (uint) args[2]); 171 printf("\"%s\", %"PRIx32", %"PRIx32"", (char *) (args[0]), (uint) (args[1]), (uint) args[2]);
121 default: 172 default:
122 for (int i = 0; i < nargs; i++) 173 for (int i = 0; i < nargs; i++)
123 printf("%"PRIx32", ", (uint) args[i]); 174 printf("%"PRIx32", ", (uint) args[i]);
124 if (nargs) 175 if (nargs)
125 printf("\b\b"); 176 printf("\b\b");
126 } 177 }
127 printf(") "); 178 printf(") ");
128 } 179 }
129#if 0 
130 aprint_debug("syscall no. %d, ", code); 
131 aprint_debug("nargs %d, argsize %d => ", nargs, argsize); 
132 dprintf_debug("syscall no. %d, ", code); 
133 dprintf_debug("nargs %d, argsize %d => ", nargs, argsize); 
134#endif 
135#if 1 180#if 1
136 if ((code == 4)) { 181 if ((code == 4)) {
137 dprintf_debug("[us] %s", (char *) args[1]); 182 dprintf_debug("[us] %s", (char *) args[1]);
138// printf("[us] %s", (char *) args[1]); 183// printf("[us] %s", (char *) args[1]);
139 } 184 }
140#endif 185#endif
141 if (code == 440) 186}
142 printf("stat(%d, %p) ", (uint32_t) args[0], 
143 (void *) args[1]); 
144 
145 md_syscall_inc_pc(ucp, opcode); 
146 187
147 if (!error)  
148 error = (*callp->sy_call)(l, args, rval); 
149 188
 189static void
 190syscall_retvals_print(int code, int nargs, register_t *args, int error, register_t *rval)
 191{
150 if (code != 4) 192 if (code != 4)
151 printf("=> %s: %d, (%"PRIx32", %"PRIx32")\n", 193 printf("=> %s: %d, (%"PRIx32", %"PRIx32")\n",
152 error?"ERROR":"OK", error, (uint) (rval[0]), (uint) (rval[1])); 194 error?"ERROR":"OK", error, (uint) (rval[0]), (uint) (rval[1]));
153 
154//out: 
155 switch (error) { 
156 default: 
157 /* fall trough */ 
158 case 0: 
159 md_syscall_set_returnargs(l, ucp, error, rval); 
160 /* fall trough */ 
161 case EJUSTRETURN: 
162 break; 
163 case ERESTART: 
164 md_syscall_dec_pc(ucp, opcode); 
165 /* nothing to do */ 
166 break; 
167 } 
168 //dprintf_debug("end of syscall : return to userland\n"); 
169//if (code != 4) printf("userret() code %d\n", code); 
170 userret(l); 
171} 195}
172 196