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