Path: blob/main/sys/contrib/dev/acpica/compiler/asldebug.c
48371 views
/******************************************************************************1*2* Module Name: asldebug -- Debug output support3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#include <contrib/dev/acpica/compiler/aslcompiler.h>152#include "aslcompiler.y.h"153154155#define _COMPONENT ACPI_COMPILER156ACPI_MODULE_NAME ("asldebug")157158159/* Local prototypes */160161static void162UtDumpParseOpName (163ACPI_PARSE_OBJECT *Op,164UINT32 Level,165UINT32 DataLength);166167static char *168UtCreateEscapeSequences (169char *InString);170171172/*******************************************************************************173*174* FUNCTION: CvDbgPrint175*176* PARAMETERS: Type - Type of output177* Fmt - Printf format string178* ... - variable printf list179*180* RETURN: None181*182* DESCRIPTION: Print statement for debug messages within the converter.183*184******************************************************************************/185186void187CvDbgPrint (188char *Fmt,189...)190{191va_list Args;192193194if (!AcpiGbl_CaptureComments || !AcpiGbl_DebugAslConversion)195{196return;197}198199va_start (Args, Fmt);200(void) vfprintf (AcpiGbl_ConvDebugFile, Fmt, Args);201va_end (Args);202return;203}204205206/*******************************************************************************207*208* FUNCTION: UtDumpIntegerOp209*210* PARAMETERS: Op - Current parse op211* Level - Current output indentation level212* IntegerLength - Output length of the integer (2/4/8/16)213*214* RETURN: None215*216* DESCRIPTION: Emit formatted debug output for "integer" ops.217* Note: IntegerLength must be one of 2,4,8,16.218*219******************************************************************************/220221void222UtDumpIntegerOp (223ACPI_PARSE_OBJECT *Op,224UINT32 Level,225UINT32 IntegerLength)226{227228/* Emit the ParseOp name, leaving room for the integer */229230UtDumpParseOpName (Op, Level, IntegerLength);231232/* Emit the integer based upon length */233234switch (IntegerLength)235{236case 2: /* Byte */237case 4: /* Word */238case 8: /* Dword */239240DbgPrint (ASL_TREE_OUTPUT,241"%*.*X", IntegerLength, IntegerLength, (UINT32) Op->Asl.Value.Integer);242break;243244case 16: /* Qword and Integer */245246DbgPrint (ASL_TREE_OUTPUT,247"%8.8X%8.8X", ACPI_FORMAT_UINT64 (Op->Asl.Value.Integer));248break;249250default:251break;252}253}254255256/*******************************************************************************257*258* FUNCTION: UtDumpStringOp259*260* PARAMETERS: Op - Current parse op261* Level - Current output indentation level262*263* RETURN: None264*265* DESCRIPTION: Emit formatted debug output for String/Pathname ops.266*267******************************************************************************/268269void270UtDumpStringOp (271ACPI_PARSE_OBJECT *Op,272UINT32 Level)273{274char *String;275276277String = Op->Asl.Value.String;278if (Op->Asl.ParseOpcode != PARSEOP_STRING_LITERAL)279{280/*281* For the "path" ops NAMEPATH, NAMESEG, METHODCALL -- if the282* ExternalName is valid, it takes precedence. In these cases the283* Value.String is the raw "internal" name from the AML code, which284* we don't want to use, because it contains non-ascii characters.285*/286if (Op->Asl.ExternalName)287{288String = Op->Asl.ExternalName;289}290}291292if (!String)293{294DbgPrint (ASL_TREE_OUTPUT,295" ERROR: Could not find a valid String/Path pointer\n");296return;297}298299String = UtCreateEscapeSequences (String);300301/* Emit the ParseOp name, leaving room for the string */302303UtDumpParseOpName (Op, Level, strlen (String));304DbgPrint (ASL_TREE_OUTPUT, "%s", String);305}306307308/*******************************************************************************309*310* FUNCTION: UtCreateEscapeSequences311*312* PARAMETERS: InString - ASCII string to be expanded313*314* RETURN: Expanded string315*316* DESCRIPTION: Expand all non-printable ASCII bytes (0-0x1F) to escape317* sequences. For example, hex 14 becomes \x14318*319* NOTE: Since this function is used for debug output only, it does320* not attempt to translate into the "known" escape sequences321* such as \a, \f, \t, etc.322*323******************************************************************************/324325static char *326UtCreateEscapeSequences (327char *InString)328{329char *String = InString;330char *OutString;331char *OutStringPtr;332UINT32 InStringLength = 0;333UINT32 EscapeCount = 0;334335336/*337* Determine up front how many escapes are within the string.338* Obtain the input string length while doing so.339*/340while (*String)341{342if ((*String <= 0x1F) || (*String >= 0x7F))343{344EscapeCount++;345}346347InStringLength++;348String++;349}350351if (!EscapeCount)352{353return (InString); /* No escapes, nothing to do */354}355356/* New string buffer, 3 extra chars per escape (4 total) */357358OutString = UtLocalCacheCalloc (InStringLength + (EscapeCount * 3));359OutStringPtr = OutString;360361/* Convert non-ascii or non-printable chars to escape sequences */362363while (*InString)364{365if ((*InString <= 0x1F) || (*InString >= 0x7F))366{367/* Insert a \x hex escape sequence */368369OutStringPtr[0] = '\\';370OutStringPtr[1] = 'x';371OutStringPtr[2] = AcpiUtHexToAsciiChar (*InString, 4);372OutStringPtr[3] = AcpiUtHexToAsciiChar (*InString, 0);373OutStringPtr += 4;374}375else /* Normal ASCII character */376{377*OutStringPtr = *InString;378OutStringPtr++;379}380381InString++;382}383384return (OutString);385}386387388/*******************************************************************************389*390* FUNCTION: UtDumpBasicOp391*392* PARAMETERS: Op - Current parse op393* Level - Current output indentation level394*395* RETURN: None396*397* DESCRIPTION: Generic formatted debug output for "basic" ops that have no398* associated strings or integer values.399*400******************************************************************************/401402void403UtDumpBasicOp (404ACPI_PARSE_OBJECT *Op,405UINT32 Level)406{407408/* Just print out the ParseOp name, there is no extra data */409410UtDumpParseOpName (Op, Level, 0);411}412413414/*******************************************************************************415*416* FUNCTION: UtDumpParseOpName417*418* PARAMETERS: Op - Current parse op419* Level - Current output indentation level420* DataLength - Length of data to appear after the name421*422* RETURN: None423*424* DESCRIPTION: Indent and emit the ascii ParseOp name for the op425*426******************************************************************************/427428static void429UtDumpParseOpName (430ACPI_PARSE_OBJECT *Op,431UINT32 Level,432UINT32 DataLength)433{434char *ParseOpName;435UINT32 IndentLength;436UINT32 NameLength;437UINT32 LineLength;438UINT32 PaddingLength;439440441/* Emit the LineNumber/IndentLevel prefix on each output line */442443DbgPrint (ASL_TREE_OUTPUT,444"%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);445446ParseOpName = UtGetOpName (Op->Asl.ParseOpcode);447448/* Calculate various lengths for output alignment */449450IndentLength = Level * DEBUG_SPACES_PER_INDENT;451NameLength = strlen (ParseOpName);452LineLength = IndentLength + 1 + NameLength + 1 + DataLength;453PaddingLength = (DEBUG_MAX_LINE_LENGTH + 1) - LineLength;454455/* Parse tree indentation is based upon the nesting/indent level */456457if (Level)458{459DbgPrint (ASL_TREE_OUTPUT, "%*s", IndentLength, " ");460}461462/* Emit the actual name here */463464DbgPrint (ASL_TREE_OUTPUT, " %s", ParseOpName);465466/* Emit extra padding blanks for alignment of later data items */467468if (LineLength > DEBUG_MAX_LINE_LENGTH)469{470/* Split a long line immediately after the ParseOpName string */471472DbgPrint (ASL_TREE_OUTPUT, "\n%*s",473(DEBUG_FULL_LINE_LENGTH - DataLength), " ");474}475else476{477DbgPrint (ASL_TREE_OUTPUT, "%*s", PaddingLength, " ");478}479}480481482