Merge branch 'release' of master.kernel.org:/pub/scm/linux/kernel/git/lenb/linux...
[linux-2.6] / drivers / acpi / utilities / utdebug.c
1 /******************************************************************************
2  *
3  * Module Name: utdebug - Debug print routines
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include <linux/module.h>
45
46 #include <acpi/acpi.h>
47
48 #define _COMPONENT          ACPI_UTILITIES
49 ACPI_MODULE_NAME("utdebug")
50
51 #ifdef ACPI_DEBUG_OUTPUT
52 static u32 acpi_gbl_prev_thread_id = 0xFFFFFFFF;
53 static char *acpi_gbl_fn_entry_str = "----Entry";
54 static char *acpi_gbl_fn_exit_str = "----Exit-";
55
56 /* Local prototypes */
57
58 static const char *acpi_ut_trim_function_name(const char *function_name);
59
60 /*******************************************************************************
61  *
62  * FUNCTION:    acpi_ut_init_stack_ptr_trace
63  *
64  * PARAMETERS:  None
65  *
66  * RETURN:      None
67  *
68  * DESCRIPTION: Save the current CPU stack pointer at subsystem startup
69  *
70  ******************************************************************************/
71
72 void acpi_ut_init_stack_ptr_trace(void)
73 {
74         u32 current_sp;
75
76         acpi_gbl_entry_stack_pointer = ACPI_PTR_DIFF(&current_sp, NULL);
77 }
78
79 /*******************************************************************************
80  *
81  * FUNCTION:    acpi_ut_track_stack_ptr
82  *
83  * PARAMETERS:  None
84  *
85  * RETURN:      None
86  *
87  * DESCRIPTION: Save the current CPU stack pointer
88  *
89  ******************************************************************************/
90
91 void acpi_ut_track_stack_ptr(void)
92 {
93         acpi_size current_sp;
94
95         current_sp = ACPI_PTR_DIFF(&current_sp, NULL);
96
97         if (current_sp < acpi_gbl_lowest_stack_pointer) {
98                 acpi_gbl_lowest_stack_pointer = current_sp;
99         }
100
101         if (acpi_gbl_nesting_level > acpi_gbl_deepest_nesting) {
102                 acpi_gbl_deepest_nesting = acpi_gbl_nesting_level;
103         }
104 }
105
106 /*******************************************************************************
107  *
108  * FUNCTION:    acpi_ut_trim_function_name
109  *
110  * PARAMETERS:  function_name       - Ascii string containing a procedure name
111  *
112  * RETURN:      Updated pointer to the function name
113  *
114  * DESCRIPTION: Remove the "Acpi" prefix from the function name, if present.
115  *              This allows compiler macros such as __FUNCTION__ to be used
116  *              with no change to the debug output.
117  *
118  ******************************************************************************/
119
120 static const char *acpi_ut_trim_function_name(const char *function_name)
121 {
122
123         /* All Function names are longer than 4 chars, check is safe */
124
125         if (*(ACPI_CAST_PTR(u32, function_name)) == ACPI_PREFIX_MIXED) {
126                 /* This is the case where the original source has not been modified */
127
128                 return (function_name + 4);
129         }
130
131         if (*(ACPI_CAST_PTR(u32, function_name)) == ACPI_PREFIX_LOWER) {
132                 /* This is the case where the source has been 'linuxized' */
133
134                 return (function_name + 5);
135         }
136
137         return (function_name);
138 }
139
140 /*******************************************************************************
141  *
142  * FUNCTION:    acpi_ut_debug_print
143  *
144  * PARAMETERS:  requested_debug_level - Requested debug print level
145  *              line_number         - Caller's line number (for error output)
146  *              function_name       - Caller's procedure name
147  *              module_name         - Caller's module name
148  *              component_id        - Caller's component ID
149  *              Format              - Printf format field
150  *              ...                 - Optional printf arguments
151  *
152  * RETURN:      None
153  *
154  * DESCRIPTION: Print error message with prefix consisting of the module name,
155  *              line number, and component ID.
156  *
157  ******************************************************************************/
158
159 void ACPI_INTERNAL_VAR_XFACE
160 acpi_ut_debug_print(u32 requested_debug_level,
161                     u32 line_number,
162                     const char *function_name,
163                     char *module_name, u32 component_id, char *format, ...)
164 {
165         u32 thread_id;
166         va_list args;
167
168         /*
169          * Stay silent if the debug level or component ID is disabled
170          */
171         if (!(requested_debug_level & acpi_dbg_level) ||
172             !(component_id & acpi_dbg_layer)) {
173                 return;
174         }
175
176         /*
177          * Thread tracking and context switch notification
178          */
179         thread_id = acpi_os_get_thread_id();
180
181         if (thread_id != acpi_gbl_prev_thread_id) {
182                 if (ACPI_LV_THREADS & acpi_dbg_level) {
183                         acpi_os_printf
184                             ("\n**** Context Switch from TID %X to TID %X ****\n\n",
185                              acpi_gbl_prev_thread_id, thread_id);
186                 }
187
188                 acpi_gbl_prev_thread_id = thread_id;
189         }
190
191         /*
192          * Display the module name, current line number, thread ID (if requested),
193          * current procedure nesting level, and the current procedure name
194          */
195         acpi_os_printf("%8s-%04ld ", module_name, line_number);
196
197         if (ACPI_LV_THREADS & acpi_dbg_level) {
198                 acpi_os_printf("[%04lX] ", thread_id);
199         }
200
201         acpi_os_printf("[%02ld] %-22.22s: ",
202                        acpi_gbl_nesting_level,
203                        acpi_ut_trim_function_name(function_name));
204
205         va_start(args, format);
206         acpi_os_vprintf(format, args);
207 }
208
209 EXPORT_SYMBOL(acpi_ut_debug_print);
210
211 /*******************************************************************************
212  *
213  * FUNCTION:    acpi_ut_debug_print_raw
214  *
215  * PARAMETERS:  requested_debug_level - Requested debug print level
216  *              line_number         - Caller's line number
217  *              function_name       - Caller's procedure name
218  *              module_name         - Caller's module name
219  *              component_id        - Caller's component ID
220  *              Format              - Printf format field
221  *              ...                 - Optional printf arguments
222  *
223  * RETURN:      None
224  *
225  * DESCRIPTION: Print message with no headers.  Has same interface as
226  *              debug_print so that the same macros can be used.
227  *
228  ******************************************************************************/
229
230 void ACPI_INTERNAL_VAR_XFACE
231 acpi_ut_debug_print_raw(u32 requested_debug_level,
232                         u32 line_number,
233                         const char *function_name,
234                         char *module_name, u32 component_id, char *format, ...)
235 {
236         va_list args;
237
238         if (!(requested_debug_level & acpi_dbg_level) ||
239             !(component_id & acpi_dbg_layer)) {
240                 return;
241         }
242
243         va_start(args, format);
244         acpi_os_vprintf(format, args);
245 }
246
247 EXPORT_SYMBOL(acpi_ut_debug_print_raw);
248
249 /*******************************************************************************
250  *
251  * FUNCTION:    acpi_ut_trace
252  *
253  * PARAMETERS:  line_number         - Caller's line number
254  *              function_name       - Caller's procedure name
255  *              module_name         - Caller's module name
256  *              component_id        - Caller's component ID
257  *
258  * RETURN:      None
259  *
260  * DESCRIPTION: Function entry trace.  Prints only if TRACE_FUNCTIONS bit is
261  *              set in debug_level
262  *
263  ******************************************************************************/
264
265 void
266 acpi_ut_trace(u32 line_number,
267               const char *function_name, char *module_name, u32 component_id)
268 {
269
270         acpi_gbl_nesting_level++;
271         acpi_ut_track_stack_ptr();
272
273         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
274                             line_number, function_name, module_name,
275                             component_id, "%s\n", acpi_gbl_fn_entry_str);
276 }
277
278 EXPORT_SYMBOL(acpi_ut_trace);
279
280 /*******************************************************************************
281  *
282  * FUNCTION:    acpi_ut_trace_ptr
283  *
284  * PARAMETERS:  line_number         - Caller's line number
285  *              function_name       - Caller's procedure name
286  *              module_name         - Caller's module name
287  *              component_id        - Caller's component ID
288  *              Pointer             - Pointer to display
289  *
290  * RETURN:      None
291  *
292  * DESCRIPTION: Function entry trace.  Prints only if TRACE_FUNCTIONS bit is
293  *              set in debug_level
294  *
295  ******************************************************************************/
296
297 void
298 acpi_ut_trace_ptr(u32 line_number,
299                   const char *function_name,
300                   char *module_name, u32 component_id, void *pointer)
301 {
302         acpi_gbl_nesting_level++;
303         acpi_ut_track_stack_ptr();
304
305         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
306                             line_number, function_name, module_name,
307                             component_id, "%s %p\n", acpi_gbl_fn_entry_str,
308                             pointer);
309 }
310
311 /*******************************************************************************
312  *
313  * FUNCTION:    acpi_ut_trace_str
314  *
315  * PARAMETERS:  line_number         - Caller's line number
316  *              function_name       - Caller's procedure name
317  *              module_name         - Caller's module name
318  *              component_id        - Caller's component ID
319  *              String              - Additional string to display
320  *
321  * RETURN:      None
322  *
323  * DESCRIPTION: Function entry trace.  Prints only if TRACE_FUNCTIONS bit is
324  *              set in debug_level
325  *
326  ******************************************************************************/
327
328 void
329 acpi_ut_trace_str(u32 line_number,
330                   const char *function_name,
331                   char *module_name, u32 component_id, char *string)
332 {
333
334         acpi_gbl_nesting_level++;
335         acpi_ut_track_stack_ptr();
336
337         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
338                             line_number, function_name, module_name,
339                             component_id, "%s %s\n", acpi_gbl_fn_entry_str,
340                             string);
341 }
342
343 /*******************************************************************************
344  *
345  * FUNCTION:    acpi_ut_trace_u32
346  *
347  * PARAMETERS:  line_number         - Caller's line number
348  *              function_name       - Caller's procedure name
349  *              module_name         - Caller's module name
350  *              component_id        - Caller's component ID
351  *              Integer             - Integer to display
352  *
353  * RETURN:      None
354  *
355  * DESCRIPTION: Function entry trace.  Prints only if TRACE_FUNCTIONS bit is
356  *              set in debug_level
357  *
358  ******************************************************************************/
359
360 void
361 acpi_ut_trace_u32(u32 line_number,
362                   const char *function_name,
363                   char *module_name, u32 component_id, u32 integer)
364 {
365
366         acpi_gbl_nesting_level++;
367         acpi_ut_track_stack_ptr();
368
369         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
370                             line_number, function_name, module_name,
371                             component_id, "%s %08X\n", acpi_gbl_fn_entry_str,
372                             integer);
373 }
374
375 /*******************************************************************************
376  *
377  * FUNCTION:    acpi_ut_exit
378  *
379  * PARAMETERS:  line_number         - Caller's line number
380  *              function_name       - Caller's procedure name
381  *              module_name         - Caller's module name
382  *              component_id        - Caller's component ID
383  *
384  * RETURN:      None
385  *
386  * DESCRIPTION: Function exit trace.  Prints only if TRACE_FUNCTIONS bit is
387  *              set in debug_level
388  *
389  ******************************************************************************/
390
391 void
392 acpi_ut_exit(u32 line_number,
393              const char *function_name, char *module_name, u32 component_id)
394 {
395
396         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
397                             line_number, function_name, module_name,
398                             component_id, "%s\n", acpi_gbl_fn_exit_str);
399
400         acpi_gbl_nesting_level--;
401 }
402
403 EXPORT_SYMBOL(acpi_ut_exit);
404
405 /*******************************************************************************
406  *
407  * FUNCTION:    acpi_ut_status_exit
408  *
409  * PARAMETERS:  line_number         - Caller's line number
410  *              function_name       - Caller's procedure name
411  *              module_name         - Caller's module name
412  *              component_id        - Caller's component ID
413  *              Status              - Exit status code
414  *
415  * RETURN:      None
416  *
417  * DESCRIPTION: Function exit trace.  Prints only if TRACE_FUNCTIONS bit is
418  *              set in debug_level. Prints exit status also.
419  *
420  ******************************************************************************/
421
422 void
423 acpi_ut_status_exit(u32 line_number,
424                     const char *function_name,
425                     char *module_name, u32 component_id, acpi_status status)
426 {
427
428         if (ACPI_SUCCESS(status)) {
429                 acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
430                                     line_number, function_name, module_name,
431                                     component_id, "%s %s\n",
432                                     acpi_gbl_fn_exit_str,
433                                     acpi_format_exception(status));
434         } else {
435                 acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
436                                     line_number, function_name, module_name,
437                                     component_id, "%s ****Exception****: %s\n",
438                                     acpi_gbl_fn_exit_str,
439                                     acpi_format_exception(status));
440         }
441
442         acpi_gbl_nesting_level--;
443 }
444
445 EXPORT_SYMBOL(acpi_ut_status_exit);
446
447 /*******************************************************************************
448  *
449  * FUNCTION:    acpi_ut_value_exit
450  *
451  * PARAMETERS:  line_number         - Caller's line number
452  *              function_name       - Caller's procedure name
453  *              module_name         - Caller's module name
454  *              component_id        - Caller's component ID
455  *              Value               - Value to be printed with exit msg
456  *
457  * RETURN:      None
458  *
459  * DESCRIPTION: Function exit trace.  Prints only if TRACE_FUNCTIONS bit is
460  *              set in debug_level. Prints exit value also.
461  *
462  ******************************************************************************/
463
464 void
465 acpi_ut_value_exit(u32 line_number,
466                    const char *function_name,
467                    char *module_name, u32 component_id, acpi_integer value)
468 {
469
470         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
471                             line_number, function_name, module_name,
472                             component_id, "%s %8.8X%8.8X\n",
473                             acpi_gbl_fn_exit_str, ACPI_FORMAT_UINT64(value));
474
475         acpi_gbl_nesting_level--;
476 }
477
478 EXPORT_SYMBOL(acpi_ut_value_exit);
479
480 /*******************************************************************************
481  *
482  * FUNCTION:    acpi_ut_ptr_exit
483  *
484  * PARAMETERS:  line_number         - Caller's line number
485  *              function_name       - Caller's procedure name
486  *              module_name         - Caller's module name
487  *              component_id        - Caller's component ID
488  *              Ptr                 - Pointer to display
489  *
490  * RETURN:      None
491  *
492  * DESCRIPTION: Function exit trace.  Prints only if TRACE_FUNCTIONS bit is
493  *              set in debug_level. Prints exit value also.
494  *
495  ******************************************************************************/
496
497 void
498 acpi_ut_ptr_exit(u32 line_number,
499                  const char *function_name,
500                  char *module_name, u32 component_id, u8 * ptr)
501 {
502
503         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
504                             line_number, function_name, module_name,
505                             component_id, "%s %p\n", acpi_gbl_fn_exit_str, ptr);
506
507         acpi_gbl_nesting_level--;
508 }
509
510 #endif
511
512 /*******************************************************************************
513  *
514  * FUNCTION:    acpi_ut_dump_buffer
515  *
516  * PARAMETERS:  Buffer              - Buffer to dump
517  *              Count               - Amount to dump, in bytes
518  *              Display             - BYTE, WORD, DWORD, or QWORD display
519  *              component_iD        - Caller's component ID
520  *
521  * RETURN:      None
522  *
523  * DESCRIPTION: Generic dump buffer in both hex and ascii.
524  *
525  ******************************************************************************/
526
527 void acpi_ut_dump_buffer(u8 * buffer, u32 count, u32 display, u32 component_id)
528 {
529         acpi_native_uint i = 0;
530         acpi_native_uint j;
531         u32 temp32;
532         u8 buf_char;
533
534         /* Only dump the buffer if tracing is enabled */
535
536         if (!((ACPI_LV_TABLES & acpi_dbg_level) &&
537               (component_id & acpi_dbg_layer))) {
538                 return;
539         }
540
541         if ((count < 4) || (count & 0x01)) {
542                 display = DB_BYTE_DISPLAY;
543         }
544
545         /* Nasty little dump buffer routine! */
546
547         while (i < count) {
548                 /* Print current offset */
549
550                 acpi_os_printf("%6.4X: ", (u32) i);
551
552                 /* Print 16 hex chars */
553
554                 for (j = 0; j < 16;) {
555                         if (i + j >= count) {
556                                 /* Dump fill spaces */
557
558                                 acpi_os_printf("%*s", ((display * 2) + 1), " ");
559                                 j += (acpi_native_uint) display;
560                                 continue;
561                         }
562
563                         switch (display) {
564                         default:        /* Default is BYTE display */
565
566                                 acpi_os_printf("%02X ", buffer[i + j]);
567                                 break;
568
569                         case DB_WORD_DISPLAY:
570
571                                 ACPI_MOVE_16_TO_32(&temp32, &buffer[i + j]);
572                                 acpi_os_printf("%04X ", temp32);
573                                 break;
574
575                         case DB_DWORD_DISPLAY:
576
577                                 ACPI_MOVE_32_TO_32(&temp32, &buffer[i + j]);
578                                 acpi_os_printf("%08X ", temp32);
579                                 break;
580
581                         case DB_QWORD_DISPLAY:
582
583                                 ACPI_MOVE_32_TO_32(&temp32, &buffer[i + j]);
584                                 acpi_os_printf("%08X", temp32);
585
586                                 ACPI_MOVE_32_TO_32(&temp32, &buffer[i + j + 4]);
587                                 acpi_os_printf("%08X ", temp32);
588                                 break;
589                         }
590
591                         j += (acpi_native_uint) display;
592                 }
593
594                 /*
595                  * Print the ASCII equivalent characters but watch out for the bad
596                  * unprintable ones (printable chars are 0x20 through 0x7E)
597                  */
598                 acpi_os_printf(" ");
599                 for (j = 0; j < 16; j++) {
600                         if (i + j >= count) {
601                                 acpi_os_printf("\n");
602                                 return;
603                         }
604
605                         buf_char = buffer[i + j];
606                         if (ACPI_IS_PRINT(buf_char)) {
607                                 acpi_os_printf("%c", buf_char);
608                         } else {
609                                 acpi_os_printf(".");
610                         }
611                 }
612
613                 /* Done with that line. */
614
615                 acpi_os_printf("\n");
616                 i += 16;
617         }
618
619         return;
620 }