source: mainline/src/debug/print.c@ cd95d784

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cd95d784 was 9c0a9b3, checked in by Jakub Vana <jakub.vana@…>, 20 years ago

1) memcopy and _memcopy functions rewriten to ANSI C norm.
2) Repaired ia32,ia64 and mips version of SPARTAN to work with this memcopy functions
3) Warning for non declared funcions added and repaired ia32,ia64 and mips versions to pass build process with this warning and Werror option

  • Property mode set to 100644
File size: 6.3 KB
Line 
1/*
2 * Copyright (C) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <putchar.h>
30#include <print.h>
31#include <synch/spinlock.h>
32#include <arch/arg.h>
33#include <arch/asm.h>
34#include <arch.h>
35
36
37static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */
38static spinlock_t printflock; /**< printf spinlock */
39
40
41/** Print NULL terminated string
42 *
43 * Print characters from str using putchar() until
44 * \x00 character is reached.
45 *
46 * @param str Characters to print.
47 *
48 */
49void print_str(const char *str)
50{
51 int i = 0;
52 char c;
53
54 while (c = str[i++])
55 putchar(c);
56}
57
58
59/** Print hexadecimal digits
60 *
61 * Print fixed count of hexadecimal digits from
62 * the number num. The digits are printed in
63 * natural left-to-right order starting with
64 * the width-th digit.
65 *
66 * @param num Number containing digits.
67 * @param width Count of digits to print.
68 *
69 */
70void print_fixed_hex(const __u64 num, const int width)
71{
72 int i;
73
74 for (i = width*8 - 4; i >= 0; i -= 4)
75 putchar(digits[(num>>i) & 0xf]);
76}
77
78
79/** Print number in given base
80 *
81 * Print significant digits of a number in given
82 * base.
83 *
84 * @param num Number to print.
85 * @param base Base to print the number in (should
86 * be in range 2 .. 16).
87 *
88 */
89void print_number(const __native num, const unsigned int base)
90{
91 int val = num;
92 char d[sizeof(__native)*8+1]; /* this is good enough even for base == 2 */
93 int i = sizeof(__native)*8-1;
94
95 do {
96 d[i--] = digits[val % base];
97 } while (val /= base);
98
99 d[sizeof(__native)*8] = 0;
100 print_str(&d[i + 1]);
101}
102
103
104/** General formatted text print
105 *
106 * Print text formatted according the fmt parameter
107 * and variant arguments. Each formatting directive
108 * begins with % (percentage) character and one of the
109 * following character:
110 *
111 * % Prints the percentage character.
112 * s The next variant argument is treated as char*
113 * and printed as a NULL terminated string.
114 * c The next variant argument is treated as a single char.
115 * p The next variant argument is treated as a maximum
116 * bit-width integer with respect to architecture
117 * and printed in full hexadecimal width.
118 * P As with 'p', but '0x' is prefixed.
119 * q The next variant argument is treated as a 64b integer
120 * and printed in full hexadecimal width.
121 * Q As with 'q', but '0x' is prefixed.
122 * l The next variant argument is treated as a 32b integer
123 * and printed in full hexadecimal width.
124 * L As with 'l', but '0x' is prefixed.
125 * w The next variant argument is treated as a 16b integer
126 * and printed in full hexadecimal width.
127 * W As with 'w', but '0x' is prefixed.
128 * b The next variant argument is treated as a 8b integer
129 * and printed in full hexadecimal width.
130 * N As with 'b', but '0x' is prefixed.
131 * d The next variant argument is treated as integer
132 * and printed in standard decimal format (only significant
133 * digits).
134 * x The next variant argument is treated as integer
135 * and printed in standard hexadecimal format (only significant
136 * digits).
137 * X As with 'x', but '0x' is prefixed.
138 *
139 * All other characters from fmt except the formatting directives
140 * are printed in verbatim.
141 *
142 * @param fmt Formatting NULL terminated string.
143 *
144 */
145void printf(const char *fmt, ...)
146{
147 int irqpri, i = 0;
148 va_list ap;
149 char c;
150
151 va_start(ap, fmt);
152
153 irqpri = cpu_priority_high();
154 spinlock_lock(&printflock);
155
156 while (c = fmt[i++]) {
157 switch (c) {
158
159 /* control character */
160 case '%':
161 switch (c = fmt[i++]) {
162
163 /* percentile itself */
164 case '%':
165 break;
166
167 /*
168 * String and character conversions.
169 */
170 case 's':
171 print_str(va_arg(ap, char_ptr));
172 goto loop;
173
174 case 'c':
175 c = (char) va_arg(ap, int);
176 break;
177
178 /*
179 * Hexadecimal conversions with fixed width.
180 */
181 case 'P':
182 print_str("0x");
183 case 'p':
184 print_fixed_hex(va_arg(ap, __native), sizeof(__native));
185 goto loop;
186
187 case 'Q':
188 print_str("0x");
189 case 'q':
190 print_fixed_hex(va_arg(ap, __u64), INT64);
191 goto loop;
192
193 case 'L':
194 print_str("0x");
195 case 'l':
196 print_fixed_hex(va_arg(ap, __native), INT32);
197 goto loop;
198
199 case 'W':
200 print_str("0x");
201 case 'w':
202 print_fixed_hex(va_arg(ap, __native), INT16);
203 goto loop;
204
205 case 'B':
206 print_str("0x");
207 case 'b':
208 print_fixed_hex(va_arg(ap, __native), INT8);
209 goto loop;
210
211 /*
212 * Decimal and hexadecimal conversions.
213 */
214 case 'd':
215 print_number(va_arg(ap, __native), 10);
216 goto loop;
217
218 case 'X':
219 print_str("0x");
220 case 'x':
221 print_number(va_arg(ap, __native), 16);
222 goto loop;
223
224 /*
225 * Bad formatting.
226 */
227 default:
228 goto out;
229 }
230
231 default: putchar(c);
232 }
233
234loop:
235 ;
236 }
237
238out:
239 spinlock_unlock(&printflock);
240 cpu_priority_restore(irqpri);
241
242 va_end(ap);
243}
Note: See TracBrowser for help on using the repository browser.