[ACPI] ACPICA 20060210
[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 - 2006, 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
127                 /* This is the case where the original source has not been modified */
128
129                 return (function_name + 4);
130         }
131
132         if (*(ACPI_CAST_PTR(u32, function_name)) == ACPI_PREFIX_LOWER) {
133
134                 /* This is the case where the source has been 'linuxized' */
135
136                 return (function_name + 5);
137         }
138
139         return (function_name);
140 }
141
142 /*******************************************************************************
143  *
144  * FUNCTION:    acpi_ut_debug_print
145  *
146  * PARAMETERS:  requested_debug_level - Requested debug print level
147  *              line_number         - Caller's line number (for error output)
148  *              function_name       - Caller's procedure name
149  *              module_name         - Caller's module name
150  *              component_id        - Caller's component ID
151  *              Format              - Printf format field
152  *              ...                 - Optional printf arguments
153  *
154  * RETURN:      None
155  *
156  * DESCRIPTION: Print error message with prefix consisting of the module name,
157  *              line number, and component ID.
158  *
159  ******************************************************************************/
160
161 void ACPI_INTERNAL_VAR_XFACE
162 acpi_ut_debug_print(u32 requested_debug_level,
163                     u32 line_number,
164                     const char *function_name,
165                     char *module_name, u32 component_id, char *format, ...)
166 {
167         u32 thread_id;
168         va_list args;
169
170         /*
171          * Stay silent if the debug level or component ID is disabled
172          */
173         if (!(requested_debug_level & acpi_dbg_level) ||
174             !(component_id & acpi_dbg_layer)) {
175                 return;
176         }
177
178         /*
179          * Thread tracking and context switch notification
180          */
181         thread_id = acpi_os_get_thread_id();
182
183         if (thread_id != acpi_gbl_prev_thread_id) {
184                 if (ACPI_LV_THREADS & acpi_dbg_level) {
185                         acpi_os_printf
186                             ("\n**** Context Switch from TID %X to TID %X ****\n\n",
187                              acpi_gbl_prev_thread_id, thread_id);
188                 }
189
190                 acpi_gbl_prev_thread_id = thread_id;
191         }
192
193         /*
194          * Display the module name, current line number, thread ID (if requested),
195          * current procedure nesting level, and the current procedure name
196          */
197         acpi_os_printf("%8s-%04ld ", module_name, line_number);
198
199         if (ACPI_LV_THREADS & acpi_dbg_level) {
200                 acpi_os_printf("[%04lX] ", thread_id);
201         }
202
203         acpi_os_printf("[%02ld] %-22.22s: ",
204                        acpi_gbl_nesting_level,
205                        acpi_ut_trim_function_name(function_name));
206
207         va_start(args, format);
208         acpi_os_vprintf(format, args);
209 }
210
211 EXPORT_SYMBOL(acpi_ut_debug_print);
212
213 /*******************************************************************************
214  *
215  * FUNCTION:    acpi_ut_debug_print_raw
216  *
217  * PARAMETERS:  requested_debug_level - Requested debug print level
218  *              line_number         - Caller's line number
219  *              function_name       - Caller's procedure name
220  *              module_name         - Caller's module name
221  *              component_id        - Caller's component ID
222  *              Format              - Printf format field
223  *              ...                 - Optional printf arguments
224  *
225  * RETURN:      None
226  *
227  * DESCRIPTION: Print message with no headers.  Has same interface as
228  *              debug_print so that the same macros can be used.
229  *
230  ******************************************************************************/
231
232 void ACPI_INTERNAL_VAR_XFACE
233 acpi_ut_debug_print_raw(u32 requested_debug_level,
234                         u32 line_number,
235                         const char *function_name,
236                         char *module_name, u32 component_id, char *format, ...)
237 {
238         va_list args;
239
240         if (!(requested_debug_level & acpi_dbg_level) ||
241             !(component_id & acpi_dbg_layer)) {
242                 return;
243         }
244
245         va_start(args, format);
246         acpi_os_vprintf(format, args);
247 }
248
249 EXPORT_SYMBOL(acpi_ut_debug_print_raw);
250
251 /*******************************************************************************
252  *
253  * FUNCTION:    acpi_ut_trace
254  *
255  * PARAMETERS:  line_number         - Caller's line number
256  *              function_name       - Caller's procedure name
257  *              module_name         - Caller's module name
258  *              component_id        - Caller's component ID
259  *
260  * RETURN:      None
261  *
262  * DESCRIPTION: Function entry trace.  Prints only if TRACE_FUNCTIONS bit is
263  *              set in debug_level
264  *
265  ******************************************************************************/
266
267 void
268 acpi_ut_trace(u32 line_number,
269               const char *function_name, char *module_name, u32 component_id)
270 {
271
272         acpi_gbl_nesting_level++;
273         acpi_ut_track_stack_ptr();
274
275         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
276                             line_number, function_name, module_name,
277                             component_id, "%s\n", acpi_gbl_fn_entry_str);
278 }
279
280 EXPORT_SYMBOL(acpi_ut_trace);
281
282 /*******************************************************************************
283  *
284  * FUNCTION:    acpi_ut_trace_ptr
285  *
286  * PARAMETERS:  line_number         - Caller's line number
287  *              function_name       - Caller's procedure name
288  *              module_name         - Caller's module name
289  *              component_id        - Caller's component ID
290  *              Pointer             - Pointer to display
291  *
292  * RETURN:      None
293  *
294  * DESCRIPTION: Function entry trace.  Prints only if TRACE_FUNCTIONS bit is
295  *              set in debug_level
296  *
297  ******************************************************************************/
298
299 void
300 acpi_ut_trace_ptr(u32 line_number,
301                   const char *function_name,
302                   char *module_name, u32 component_id, void *pointer)
303 {
304         acpi_gbl_nesting_level++;
305         acpi_ut_track_stack_ptr();
306
307         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
308                             line_number, function_name, module_name,
309                             component_id, "%s %p\n", acpi_gbl_fn_entry_str,
310                             pointer);
311 }
312
313 /*******************************************************************************
314  *
315  * FUNCTION:    acpi_ut_trace_str
316  *
317  * PARAMETERS:  line_number         - Caller's line number
318  *              function_name       - Caller's procedure name
319  *              module_name         - Caller's module name
320  *              component_id        - Caller's component ID
321  *              String              - Additional string to display
322  *
323  * RETURN:      None
324  *
325  * DESCRIPTION: Function entry trace.  Prints only if TRACE_FUNCTIONS bit is
326  *              set in debug_level
327  *
328  ******************************************************************************/
329
330 void
331 acpi_ut_trace_str(u32 line_number,
332                   const char *function_name,
333                   char *module_name, u32 component_id, char *string)
334 {
335
336         acpi_gbl_nesting_level++;
337         acpi_ut_track_stack_ptr();
338
339         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
340                             line_number, function_name, module_name,
341                             component_id, "%s %s\n", acpi_gbl_fn_entry_str,
342                             string);
343 }
344
345 /*******************************************************************************
346  *
347  * FUNCTION:    acpi_ut_trace_u32
348  *
349  * PARAMETERS:  line_number         - Caller's line number
350  *              function_name       - Caller's procedure name
351  *              module_name         - Caller's module name
352  *              component_id        - Caller's component ID
353  *              Integer             - Integer to display
354  *
355  * RETURN:      None
356  *
357  * DESCRIPTION: Function entry trace.  Prints only if TRACE_FUNCTIONS bit is
358  *              set in debug_level
359  *
360  ******************************************************************************/
361
362 void
363 acpi_ut_trace_u32(u32 line_number,
364                   const char *function_name,
365                   char *module_name, u32 component_id, u32 integer)
366 {
367
368         acpi_gbl_nesting_level++;
369         acpi_ut_track_stack_ptr();
370
371         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
372                             line_number, function_name, module_name,
373                             component_id, "%s %08X\n", acpi_gbl_fn_entry_str,
374                             integer);
375 }
376
377 /*******************************************************************************
378  *
379  * FUNCTION:    acpi_ut_exit
380  *
381  * PARAMETERS:  line_number         - Caller's line number
382  *              function_name       - Caller's procedure name
383  *              module_name         - Caller's module name
384  *              component_id        - Caller's component ID
385  *
386  * RETURN:      None
387  *
388  * DESCRIPTION: Function exit trace.  Prints only if TRACE_FUNCTIONS bit is
389  *              set in debug_level
390  *
391  ******************************************************************************/
392
393 void
394 acpi_ut_exit(u32 line_number,
395              const char *function_name, char *module_name, u32 component_id)
396 {
397
398         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
399                             line_number, function_name, module_name,
400                             component_id, "%s\n", acpi_gbl_fn_exit_str);
401
402         acpi_gbl_nesting_level--;
403 }
404
405 EXPORT_SYMBOL(acpi_ut_exit);
406
407 /*******************************************************************************
408  *
409  * FUNCTION:    acpi_ut_status_exit
410  *
411  * PARAMETERS:  line_number         - Caller's line number
412  *              function_name       - Caller's procedure name
413  *              module_name         - Caller's module name
414  *              component_id        - Caller's component ID
415  *              Status              - Exit status code
416  *
417  * RETURN:      None
418  *
419  * DESCRIPTION: Function exit trace.  Prints only if TRACE_FUNCTIONS bit is
420  *              set in debug_level. Prints exit status also.
421  *
422  ******************************************************************************/
423
424 void
425 acpi_ut_status_exit(u32 line_number,
426                     const char *function_name,
427                     char *module_name, u32 component_id, acpi_status status)
428 {
429
430         if (ACPI_SUCCESS(status)) {
431                 acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
432                                     line_number, function_name, module_name,
433                                     component_id, "%s %s\n",
434                                     acpi_gbl_fn_exit_str,
435                                     acpi_format_exception(status));
436         } else {
437                 acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
438                                     line_number, function_name, module_name,
439                                     component_id, "%s ****Exception****: %s\n",
440                                     acpi_gbl_fn_exit_str,
441                                     acpi_format_exception(status));
442         }
443
444         acpi_gbl_nesting_level--;
445 }
446
447 EXPORT_SYMBOL(acpi_ut_status_exit);
448
449 /*******************************************************************************
450  *
451  * FUNCTION:    acpi_ut_value_exit
452  *
453  * PARAMETERS:  line_number         - Caller's line number
454  *              function_name       - Caller's procedure name
455  *              module_name         - Caller's module name
456  *              component_id        - Caller's component ID
457  *              Value               - Value to be printed with exit msg
458  *
459  * RETURN:      None
460  *
461  * DESCRIPTION: Function exit trace.  Prints only if TRACE_FUNCTIONS bit is
462  *              set in debug_level. Prints exit value also.
463  *
464  ******************************************************************************/
465
466 void
467 acpi_ut_value_exit(u32 line_number,
468                    const char *function_name,
469                    char *module_name, u32 component_id, acpi_integer value)
470 {
471
472         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
473                             line_number, function_name, module_name,
474                             component_id, "%s %8.8X%8.8X\n",
475                             acpi_gbl_fn_exit_str, ACPI_FORMAT_UINT64(value));
476
477         acpi_gbl_nesting_level--;
478 }
479
480 EXPORT_SYMBOL(acpi_ut_value_exit);
481
482 /*******************************************************************************
483  *
484  * FUNCTION:    acpi_ut_ptr_exit
485  *
486  * PARAMETERS:  line_number         - Caller's line number
487  *              function_name       - Caller's procedure name
488  *              module_name         - Caller's module name
489  *              component_id        - Caller's component ID
490  *              Ptr                 - Pointer to display
491  *
492  * RETURN:      None
493  *
494  * DESCRIPTION: Function exit trace.  Prints only if TRACE_FUNCTIONS bit is
495  *              set in debug_level. Prints exit value also.
496  *
497  ******************************************************************************/
498
499 void
500 acpi_ut_ptr_exit(u32 line_number,
501                  const char *function_name,
502                  char *module_name, u32 component_id, u8 * ptr)
503 {
504
505         acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
506                             line_number, function_name, module_name,
507                             component_id, "%s %p\n", acpi_gbl_fn_exit_str, ptr);
508
509         acpi_gbl_nesting_level--;
510 }
511
512 #endif
513
514 /*******************************************************************************
515  *
516  * FUNCTION:    acpi_ut_dump_buffer
517  *
518  * PARAMETERS:  Buffer              - Buffer to dump
519  *              Count               - Amount to dump, in bytes
520  *              Display             - BYTE, WORD, DWORD, or QWORD display
521  *              component_iD        - Caller's component ID
522  *
523  * RETURN:      None
524  *
525  * DESCRIPTION: Generic dump buffer in both hex and ascii.
526  *
527  ******************************************************************************/
528
529 void acpi_ut_dump_buffer(u8 * buffer, u32 count, u32 display, u32 component_id)
530 {
531         acpi_native_uint i = 0;
532         acpi_native_uint j;
533         u32 temp32;
534         u8 buf_char;
535
536         /* Only dump the buffer if tracing is enabled */
537
538         if (!((ACPI_LV_TABLES & acpi_dbg_level) &&
539               (component_id & acpi_dbg_layer))) {
540                 return;
541         }
542
543         if ((count < 4) || (count & 0x01)) {
544                 display = DB_BYTE_DISPLAY;
545         }
546
547         /* Nasty little dump buffer routine! */
548
549         while (i < count) {
550
551                 /* Print current offset */
552
553                 acpi_os_printf("%6.4X: ", (u32) i);
554
555                 /* Print 16 hex chars */
556
557                 for (j = 0; j < 16;) {
558                         if (i + j >= count) {
559
560                                 /* Dump fill spaces */
561
562                                 acpi_os_printf("%*s", ((display * 2) + 1), " ");
563                                 j += (acpi_native_uint) display;
564                                 continue;
565                         }
566
567                         switch (display) {
568                         default:        /* Default is BYTE display */
569
570                                 acpi_os_printf("%02X ", buffer[i + j]);
571                                 break;
572
573                         case DB_WORD_DISPLAY:
574
575                                 ACPI_MOVE_16_TO_32(&temp32, &buffer[i + j]);
576                                 acpi_os_printf("%04X ", temp32);
577                                 break;
578
579                         case DB_DWORD_DISPLAY:
580
581                                 ACPI_MOVE_32_TO_32(&temp32, &buffer[i + j]);
582                                 acpi_os_printf("%08X ", temp32);
583                                 break;
584
585                         case DB_QWORD_DISPLAY:
586
587                                 ACPI_MOVE_32_TO_32(&temp32, &buffer[i + j]);
588                                 acpi_os_printf("%08X", temp32);
589
590                                 ACPI_MOVE_32_TO_32(&temp32, &buffer[i + j + 4]);
591                                 acpi_os_printf("%08X ", temp32);
592                                 break;
593                         }
594
595                         j += (acpi_native_uint) display;
596                 }
597
598                 /*
599                  * Print the ASCII equivalent characters but watch out for the bad
600                  * unprintable ones (printable chars are 0x20 through 0x7E)
601                  */
602                 acpi_os_printf(" ");
603                 for (j = 0; j < 16; j++) {
604                         if (i + j >= count) {
605                                 acpi_os_printf("\n");
606                                 return;
607                         }
608
609                         buf_char = buffer[i + j];
610                         if (ACPI_IS_PRINT(buf_char)) {
611                                 acpi_os_printf("%c", buf_char);
612                         } else {
613                                 acpi_os_printf(".");
614                         }
615                 }
616
617                 /* Done with that line. */
618
619                 acpi_os_printf("\n");
620                 i += 16;
621         }
622
623         return;
624 }