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