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