xref: /freebsd/sys/contrib/dev/acpica/components/utilities/utdebug.c (revision c6ec7d31830ab1c80edae95ad5e4b9dba10c47ac)
1 /******************************************************************************
2  *
3  * Module Name: utdebug - Debug print routines
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2012, 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 #define __UTDEBUG_C__
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 
49 #define _COMPONENT          ACPI_UTILITIES
50         ACPI_MODULE_NAME    ("utdebug")
51 
52 
53 #ifdef ACPI_DEBUG_OUTPUT
54 
55 static ACPI_THREAD_ID       AcpiGbl_PrevThreadId = (ACPI_THREAD_ID) 0xFFFFFFFF;
56 static char                 *AcpiGbl_FnEntryStr = "----Entry";
57 static char                 *AcpiGbl_FnExitStr  = "----Exit-";
58 
59 /* Local prototypes */
60 
61 static const char *
62 AcpiUtTrimFunctionName (
63     const char              *FunctionName);
64 
65 
66 /*******************************************************************************
67  *
68  * FUNCTION:    AcpiUtInitStackPtrTrace
69  *
70  * PARAMETERS:  None
71  *
72  * RETURN:      None
73  *
74  * DESCRIPTION: Save the current CPU stack pointer at subsystem startup
75  *
76  ******************************************************************************/
77 
78 void
79 AcpiUtInitStackPtrTrace (
80     void)
81 {
82     ACPI_SIZE               CurrentSp;
83 
84 
85     AcpiGbl_EntryStackPointer = &CurrentSp;
86 }
87 
88 
89 /*******************************************************************************
90  *
91  * FUNCTION:    AcpiUtTrackStackPtr
92  *
93  * PARAMETERS:  None
94  *
95  * RETURN:      None
96  *
97  * DESCRIPTION: Save the current CPU stack pointer
98  *
99  ******************************************************************************/
100 
101 void
102 AcpiUtTrackStackPtr (
103     void)
104 {
105     ACPI_SIZE               CurrentSp;
106 
107 
108     if (&CurrentSp < AcpiGbl_LowestStackPointer)
109     {
110         AcpiGbl_LowestStackPointer = &CurrentSp;
111     }
112 
113     if (AcpiGbl_NestingLevel > AcpiGbl_DeepestNesting)
114     {
115         AcpiGbl_DeepestNesting = AcpiGbl_NestingLevel;
116     }
117 }
118 
119 
120 /*******************************************************************************
121  *
122  * FUNCTION:    AcpiUtTrimFunctionName
123  *
124  * PARAMETERS:  FunctionName        - Ascii string containing a procedure name
125  *
126  * RETURN:      Updated pointer to the function name
127  *
128  * DESCRIPTION: Remove the "Acpi" prefix from the function name, if present.
129  *              This allows compiler macros such as __FUNCTION__ to be used
130  *              with no change to the debug output.
131  *
132  ******************************************************************************/
133 
134 static const char *
135 AcpiUtTrimFunctionName (
136     const char              *FunctionName)
137 {
138 
139     /* All Function names are longer than 4 chars, check is safe */
140 
141     if (*(ACPI_CAST_PTR (UINT32, FunctionName)) == ACPI_PREFIX_MIXED)
142     {
143         /* This is the case where the original source has not been modified */
144 
145         return (FunctionName + 4);
146     }
147 
148     if (*(ACPI_CAST_PTR (UINT32, FunctionName)) == ACPI_PREFIX_LOWER)
149     {
150         /* This is the case where the source has been 'linuxized' */
151 
152         return (FunctionName + 5);
153     }
154 
155     return (FunctionName);
156 }
157 
158 
159 /*******************************************************************************
160  *
161  * FUNCTION:    AcpiDebugPrint
162  *
163  * PARAMETERS:  RequestedDebugLevel - Requested debug print level
164  *              LineNumber          - Caller's line number (for error output)
165  *              FunctionName        - Caller's procedure name
166  *              ModuleName          - Caller's module name
167  *              ComponentId         - Caller's component ID
168  *              Format              - Printf format field
169  *              ...                 - Optional printf arguments
170  *
171  * RETURN:      None
172  *
173  * DESCRIPTION: Print error message with prefix consisting of the module name,
174  *              line number, and component ID.
175  *
176  ******************************************************************************/
177 
178 void  ACPI_INTERNAL_VAR_XFACE
179 AcpiDebugPrint (
180     UINT32                  RequestedDebugLevel,
181     UINT32                  LineNumber,
182     const char              *FunctionName,
183     const char              *ModuleName,
184     UINT32                  ComponentId,
185     const char              *Format,
186     ...)
187 {
188     ACPI_THREAD_ID          ThreadId;
189     va_list                 args;
190 
191 
192     /*
193      * Stay silent if the debug level or component ID is disabled
194      */
195     if (!(RequestedDebugLevel & AcpiDbgLevel) ||
196         !(ComponentId & AcpiDbgLayer))
197     {
198         return;
199     }
200 
201     /*
202      * Thread tracking and context switch notification
203      */
204     ThreadId = AcpiOsGetThreadId ();
205     if (ThreadId != AcpiGbl_PrevThreadId)
206     {
207         if (ACPI_LV_THREADS & AcpiDbgLevel)
208         {
209             AcpiOsPrintf (
210                 "\n**** Context Switch from TID %u to TID %u ****\n\n",
211                 (UINT32) AcpiGbl_PrevThreadId, (UINT32) ThreadId);
212         }
213 
214         AcpiGbl_PrevThreadId = ThreadId;
215     }
216 
217     /*
218      * Display the module name, current line number, thread ID (if requested),
219      * current procedure nesting level, and the current procedure name
220      */
221     AcpiOsPrintf ("%8s-%04ld ", ModuleName, LineNumber);
222 
223     if (ACPI_LV_THREADS & AcpiDbgLevel)
224     {
225         AcpiOsPrintf ("[%u] ", (UINT32) ThreadId);
226     }
227 
228     AcpiOsPrintf ("[%02ld] %-22.22s: ",
229         AcpiGbl_NestingLevel, AcpiUtTrimFunctionName (FunctionName));
230 
231     va_start (args, Format);
232     AcpiOsVprintf (Format, args);
233     va_end (args);
234 }
235 
236 ACPI_EXPORT_SYMBOL (AcpiDebugPrint)
237 
238 
239 /*******************************************************************************
240  *
241  * FUNCTION:    AcpiDebugPrintRaw
242  *
243  * PARAMETERS:  RequestedDebugLevel - Requested debug print level
244  *              LineNumber          - Caller's line number
245  *              FunctionName        - Caller's procedure name
246  *              ModuleName          - Caller's module name
247  *              ComponentId         - Caller's component ID
248  *              Format              - Printf format field
249  *              ...                 - Optional printf arguments
250  *
251  * RETURN:      None
252  *
253  * DESCRIPTION: Print message with no headers. Has same interface as
254  *              DebugPrint so that the same macros can be used.
255  *
256  ******************************************************************************/
257 
258 void  ACPI_INTERNAL_VAR_XFACE
259 AcpiDebugPrintRaw (
260     UINT32                  RequestedDebugLevel,
261     UINT32                  LineNumber,
262     const char              *FunctionName,
263     const char              *ModuleName,
264     UINT32                  ComponentId,
265     const char              *Format,
266     ...)
267 {
268     va_list                 args;
269 
270 
271     if (!(RequestedDebugLevel & AcpiDbgLevel) ||
272         !(ComponentId & AcpiDbgLayer))
273     {
274         return;
275     }
276 
277     va_start (args, Format);
278     AcpiOsVprintf (Format, args);
279     va_end (args);
280 }
281 
282 ACPI_EXPORT_SYMBOL (AcpiDebugPrintRaw)
283 
284 
285 /*******************************************************************************
286  *
287  * FUNCTION:    AcpiUtTrace
288  *
289  * PARAMETERS:  LineNumber          - Caller's line number
290  *              FunctionName        - Caller's procedure name
291  *              ModuleName          - Caller's module name
292  *              ComponentId         - Caller's component ID
293  *
294  * RETURN:      None
295  *
296  * DESCRIPTION: Function entry trace. Prints only if TRACE_FUNCTIONS bit is
297  *              set in DebugLevel
298  *
299  ******************************************************************************/
300 
301 void
302 AcpiUtTrace (
303     UINT32                  LineNumber,
304     const char              *FunctionName,
305     const char              *ModuleName,
306     UINT32                  ComponentId)
307 {
308 
309     AcpiGbl_NestingLevel++;
310     AcpiUtTrackStackPtr ();
311 
312     AcpiDebugPrint (ACPI_LV_FUNCTIONS,
313         LineNumber, FunctionName, ModuleName, ComponentId,
314         "%s\n", AcpiGbl_FnEntryStr);
315 }
316 
317 ACPI_EXPORT_SYMBOL (AcpiUtTrace)
318 
319 
320 /*******************************************************************************
321  *
322  * FUNCTION:    AcpiUtTracePtr
323  *
324  * PARAMETERS:  LineNumber          - Caller's line number
325  *              FunctionName        - Caller's procedure name
326  *              ModuleName          - Caller's module name
327  *              ComponentId         - Caller's component ID
328  *              Pointer             - Pointer to display
329  *
330  * RETURN:      None
331  *
332  * DESCRIPTION: Function entry trace. Prints only if TRACE_FUNCTIONS bit is
333  *              set in DebugLevel
334  *
335  ******************************************************************************/
336 
337 void
338 AcpiUtTracePtr (
339     UINT32                  LineNumber,
340     const char              *FunctionName,
341     const char              *ModuleName,
342     UINT32                  ComponentId,
343     void                    *Pointer)
344 {
345 
346     AcpiGbl_NestingLevel++;
347     AcpiUtTrackStackPtr ();
348 
349     AcpiDebugPrint (ACPI_LV_FUNCTIONS,
350         LineNumber, FunctionName, ModuleName, ComponentId,
351         "%s %p\n", AcpiGbl_FnEntryStr, Pointer);
352 }
353 
354 
355 /*******************************************************************************
356  *
357  * FUNCTION:    AcpiUtTraceStr
358  *
359  * PARAMETERS:  LineNumber          - Caller's line number
360  *              FunctionName        - Caller's procedure name
361  *              ModuleName          - Caller's module name
362  *              ComponentId         - Caller's component ID
363  *              String              - Additional string to display
364  *
365  * RETURN:      None
366  *
367  * DESCRIPTION: Function entry trace. Prints only if TRACE_FUNCTIONS bit is
368  *              set in DebugLevel
369  *
370  ******************************************************************************/
371 
372 void
373 AcpiUtTraceStr (
374     UINT32                  LineNumber,
375     const char              *FunctionName,
376     const char              *ModuleName,
377     UINT32                  ComponentId,
378     char                    *String)
379 {
380 
381     AcpiGbl_NestingLevel++;
382     AcpiUtTrackStackPtr ();
383 
384     AcpiDebugPrint (ACPI_LV_FUNCTIONS,
385         LineNumber, FunctionName, ModuleName, ComponentId,
386         "%s %s\n", AcpiGbl_FnEntryStr, String);
387 }
388 
389 
390 /*******************************************************************************
391  *
392  * FUNCTION:    AcpiUtTraceU32
393  *
394  * PARAMETERS:  LineNumber          - Caller's line number
395  *              FunctionName        - Caller's procedure name
396  *              ModuleName          - Caller's module name
397  *              ComponentId         - Caller's component ID
398  *              Integer             - Integer to display
399  *
400  * RETURN:      None
401  *
402  * DESCRIPTION: Function entry trace. Prints only if TRACE_FUNCTIONS bit is
403  *              set in DebugLevel
404  *
405  ******************************************************************************/
406 
407 void
408 AcpiUtTraceU32 (
409     UINT32                  LineNumber,
410     const char              *FunctionName,
411     const char              *ModuleName,
412     UINT32                  ComponentId,
413     UINT32                  Integer)
414 {
415 
416     AcpiGbl_NestingLevel++;
417     AcpiUtTrackStackPtr ();
418 
419     AcpiDebugPrint (ACPI_LV_FUNCTIONS,
420         LineNumber, FunctionName, ModuleName, ComponentId,
421         "%s %08X\n", AcpiGbl_FnEntryStr, Integer);
422 }
423 
424 
425 /*******************************************************************************
426  *
427  * FUNCTION:    AcpiUtExit
428  *
429  * PARAMETERS:  LineNumber          - Caller's line number
430  *              FunctionName        - Caller's procedure name
431  *              ModuleName          - Caller's module name
432  *              ComponentId         - Caller's component ID
433  *
434  * RETURN:      None
435  *
436  * DESCRIPTION: Function exit trace. Prints only if TRACE_FUNCTIONS bit is
437  *              set in DebugLevel
438  *
439  ******************************************************************************/
440 
441 void
442 AcpiUtExit (
443     UINT32                  LineNumber,
444     const char              *FunctionName,
445     const char              *ModuleName,
446     UINT32                  ComponentId)
447 {
448 
449     AcpiDebugPrint (ACPI_LV_FUNCTIONS,
450         LineNumber, FunctionName, ModuleName, ComponentId,
451         "%s\n", AcpiGbl_FnExitStr);
452 
453     AcpiGbl_NestingLevel--;
454 }
455 
456 ACPI_EXPORT_SYMBOL (AcpiUtExit)
457 
458 
459 /*******************************************************************************
460  *
461  * FUNCTION:    AcpiUtStatusExit
462  *
463  * PARAMETERS:  LineNumber          - Caller's line number
464  *              FunctionName        - Caller's procedure name
465  *              ModuleName          - Caller's module name
466  *              ComponentId         - Caller's component ID
467  *              Status              - Exit status code
468  *
469  * RETURN:      None
470  *
471  * DESCRIPTION: Function exit trace. Prints only if TRACE_FUNCTIONS bit is
472  *              set in DebugLevel. Prints exit status also.
473  *
474  ******************************************************************************/
475 
476 void
477 AcpiUtStatusExit (
478     UINT32                  LineNumber,
479     const char              *FunctionName,
480     const char              *ModuleName,
481     UINT32                  ComponentId,
482     ACPI_STATUS             Status)
483 {
484 
485     if (ACPI_SUCCESS (Status))
486     {
487         AcpiDebugPrint (ACPI_LV_FUNCTIONS,
488             LineNumber, FunctionName, ModuleName, ComponentId,
489             "%s %s\n", AcpiGbl_FnExitStr,
490             AcpiFormatException (Status));
491     }
492     else
493     {
494         AcpiDebugPrint (ACPI_LV_FUNCTIONS,
495             LineNumber, FunctionName, ModuleName, ComponentId,
496             "%s ****Exception****: %s\n", AcpiGbl_FnExitStr,
497             AcpiFormatException (Status));
498     }
499 
500     AcpiGbl_NestingLevel--;
501 }
502 
503 ACPI_EXPORT_SYMBOL (AcpiUtStatusExit)
504 
505 
506 /*******************************************************************************
507  *
508  * FUNCTION:    AcpiUtValueExit
509  *
510  * PARAMETERS:  LineNumber          - Caller's line number
511  *              FunctionName        - Caller's procedure name
512  *              ModuleName          - Caller's module name
513  *              ComponentId         - Caller's component ID
514  *              Value               - Value to be printed with exit msg
515  *
516  * RETURN:      None
517  *
518  * DESCRIPTION: Function exit trace. Prints only if TRACE_FUNCTIONS bit is
519  *              set in DebugLevel. Prints exit value also.
520  *
521  ******************************************************************************/
522 
523 void
524 AcpiUtValueExit (
525     UINT32                  LineNumber,
526     const char              *FunctionName,
527     const char              *ModuleName,
528     UINT32                  ComponentId,
529     UINT64                  Value)
530 {
531 
532     AcpiDebugPrint (ACPI_LV_FUNCTIONS,
533         LineNumber, FunctionName, ModuleName, ComponentId,
534         "%s %8.8X%8.8X\n", AcpiGbl_FnExitStr,
535         ACPI_FORMAT_UINT64 (Value));
536 
537     AcpiGbl_NestingLevel--;
538 }
539 
540 ACPI_EXPORT_SYMBOL (AcpiUtValueExit)
541 
542 
543 /*******************************************************************************
544  *
545  * FUNCTION:    AcpiUtPtrExit
546  *
547  * PARAMETERS:  LineNumber          - Caller's line number
548  *              FunctionName        - Caller's procedure name
549  *              ModuleName          - Caller's module name
550  *              ComponentId         - Caller's component ID
551  *              Ptr                 - Pointer to display
552  *
553  * RETURN:      None
554  *
555  * DESCRIPTION: Function exit trace. Prints only if TRACE_FUNCTIONS bit is
556  *              set in DebugLevel. Prints exit value also.
557  *
558  ******************************************************************************/
559 
560 void
561 AcpiUtPtrExit (
562     UINT32                  LineNumber,
563     const char              *FunctionName,
564     const char              *ModuleName,
565     UINT32                  ComponentId,
566     UINT8                   *Ptr)
567 {
568 
569     AcpiDebugPrint (ACPI_LV_FUNCTIONS,
570         LineNumber, FunctionName, ModuleName, ComponentId,
571         "%s %p\n", AcpiGbl_FnExitStr, Ptr);
572 
573     AcpiGbl_NestingLevel--;
574 }
575 
576 #endif
577 
578 
579 /*******************************************************************************
580  *
581  * FUNCTION:    AcpiUtDumpBuffer
582  *
583  * PARAMETERS:  Buffer              - Buffer to dump
584  *              Count               - Amount to dump, in bytes
585  *              Display             - BYTE, WORD, DWORD, or QWORD display
586  *              Offset              - Beginning buffer offset (display only)
587  *
588  * RETURN:      None
589  *
590  * DESCRIPTION: Generic dump buffer in both hex and ascii.
591  *
592  ******************************************************************************/
593 
594 void
595 AcpiUtDumpBuffer (
596     UINT8                   *Buffer,
597     UINT32                  Count,
598     UINT32                  Display,
599     UINT32                  BaseOffset)
600 {
601     UINT32                  i = 0;
602     UINT32                  j;
603     UINT32                  Temp32;
604     UINT8                   BufChar;
605 
606 
607     if (!Buffer)
608     {
609         AcpiOsPrintf ("Null Buffer Pointer in DumpBuffer!\n");
610         return;
611     }
612 
613     if ((Count < 4) || (Count & 0x01))
614     {
615         Display = DB_BYTE_DISPLAY;
616     }
617 
618     /* Nasty little dump buffer routine! */
619 
620     while (i < Count)
621     {
622         /* Print current offset */
623 
624         AcpiOsPrintf ("%6.4X: ", (BaseOffset + i));
625 
626         /* Print 16 hex chars */
627 
628         for (j = 0; j < 16;)
629         {
630             if (i + j >= Count)
631             {
632                 /* Dump fill spaces */
633 
634                 AcpiOsPrintf ("%*s", ((Display * 2) + 1), " ");
635                 j += Display;
636                 continue;
637             }
638 
639             switch (Display)
640             {
641             case DB_BYTE_DISPLAY:
642             default:    /* Default is BYTE display */
643 
644                 AcpiOsPrintf ("%02X ", Buffer[(ACPI_SIZE) i + j]);
645                 break;
646 
647 
648             case DB_WORD_DISPLAY:
649 
650                 ACPI_MOVE_16_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j]);
651                 AcpiOsPrintf ("%04X ", Temp32);
652                 break;
653 
654 
655             case DB_DWORD_DISPLAY:
656 
657                 ACPI_MOVE_32_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j]);
658                 AcpiOsPrintf ("%08X ", Temp32);
659                 break;
660 
661 
662             case DB_QWORD_DISPLAY:
663 
664                 ACPI_MOVE_32_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j]);
665                 AcpiOsPrintf ("%08X", Temp32);
666 
667                 ACPI_MOVE_32_TO_32 (&Temp32, &Buffer[(ACPI_SIZE) i + j + 4]);
668                 AcpiOsPrintf ("%08X ", Temp32);
669                 break;
670             }
671 
672             j += Display;
673         }
674 
675         /*
676          * Print the ASCII equivalent characters but watch out for the bad
677          * unprintable ones (printable chars are 0x20 through 0x7E)
678          */
679         AcpiOsPrintf (" ");
680         for (j = 0; j < 16; j++)
681         {
682             if (i + j >= Count)
683             {
684                 AcpiOsPrintf ("\n");
685                 return;
686             }
687 
688             BufChar = Buffer[(ACPI_SIZE) i + j];
689             if (ACPI_IS_PRINT (BufChar))
690             {
691                 AcpiOsPrintf ("%c", BufChar);
692             }
693             else
694             {
695                 AcpiOsPrintf (".");
696             }
697         }
698 
699         /* Done with that line. */
700 
701         AcpiOsPrintf ("\n");
702         i += 16;
703     }
704 
705     return;
706 }
707 
708 
709 /*******************************************************************************
710  *
711  * FUNCTION:    AcpiUtDebugDumpBuffer
712  *
713  * PARAMETERS:  Buffer              - Buffer to dump
714  *              Count               - Amount to dump, in bytes
715  *              Display             - BYTE, WORD, DWORD, or QWORD display
716  *              ComponentID         - Caller's component ID
717  *
718  * RETURN:      None
719  *
720  * DESCRIPTION: Generic dump buffer in both hex and ascii.
721  *
722  ******************************************************************************/
723 
724 void
725 AcpiUtDebugDumpBuffer (
726     UINT8                   *Buffer,
727     UINT32                  Count,
728     UINT32                  Display,
729     UINT32                  ComponentId)
730 {
731 
732     /* Only dump the buffer if tracing is enabled */
733 
734     if (!((ACPI_LV_TABLES & AcpiDbgLevel) &&
735         (ComponentId & AcpiDbgLayer)))
736     {
737         return;
738     }
739 
740     AcpiUtDumpBuffer (Buffer, Count, Display, 0);
741 }
742