void debugDumpAstSubTreeConvertedBackToSource()

in agent/native/ext/AST_debug.cpp [643:702]


void debugDumpAstSubTreeConvertedBackToSource( StringView compiledFileFullPath, StringView compiledFileRelativePath, zend_ast* ast, StringView isBeforeProcessSuffix, TextOutputStream* txtOutStream )
{
    ResultCode resultCode;
    FILE* convertedBackToSourceFile = NULL;
    int errnoValue = 0;
    StringBuffer convertedBackToSourceFileFullPath = ELASTIC_APM_EMPTY_STRING_BUFFER;
    zend_string* convertedBackToSourceText = NULL;
    String textAsCString;

    StringView convertedBackToSourceFileExtensionSuffix = ELASTIC_APM_STRING_LITERAL_TO_VIEW( ".php" );
    StringView convertedBackToSourceFileFullPathParts[] = {
        stringBufferToView( g_astProcessDebugDumpOutDir ),
        compiledFileRelativePath,
        isBeforeProcessSuffix,
        ELASTIC_APM_STRING_LITERAL_TO_VIEW( ".converted_back_to_source" ),
        convertedBackToSourceFileExtensionSuffix
    };
    ELASTIC_APM_CALL_IF_FAILED_GOTO( buildFileFullPath( ELASTIC_APM_MAKE_ARRAY_VIEW_FROM_STATIC( StringViewArrayView, convertedBackToSourceFileFullPathParts ), /* out */ &convertedBackToSourceFileFullPath ) );

    errnoValue = openFile( convertedBackToSourceFileFullPath.begin, "w", /* out */ &convertedBackToSourceFile );
    if ( errnoValue != 0 )
    {
        ELASTIC_APM_LOG_ERROR( "Failed to open file; convertedBackToSourceFileFullPath: %s; errno: %d (%s)", convertedBackToSourceFileFullPath.begin, errnoValue, streamErrNo( errnoValue, txtOutStream ) );
        ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
    }

    ELASTIC_APM_LOG_INFO( "Printing AST converted back to source of %s to %s ...", compiledFileFullPath.begin, convertedBackToSourceFileFullPath.begin );
    convertedBackToSourceText = zend_ast_export( /* prefix */ "", ast, /* suffix */ "" );
    textAsCString = nullableZStringToString( convertedBackToSourceText );
    if ( textAsCString == NULL )
    {
        ELASTIC_APM_LOG_INFO( "Nothing to print for AST of %s converted back to source to %s", compiledFileFullPath.begin, convertedBackToSourceFileFullPath.begin );
    }
    else
    {
        fputs( textAsCString, convertedBackToSourceFile );
        ELASTIC_APM_LOG_INFO( "Printed AST converted back to source of %s to %s. Contents:\n%s"
                              , compiledFileFullPath.begin, convertedBackToSourceFileFullPath.begin, nullableZStringToString( convertedBackToSourceText ) );
    }

    resultCode = resultSuccess;
    finally:
    if ( convertedBackToSourceFile != NULL )
    {
        fclose( convertedBackToSourceFile );
        convertedBackToSourceFile = NULL;
    }
    ELASTIC_APM_FREE_STRING_BUFFER_AND_SET_TO_NULL( /* in,out */ convertedBackToSourceFileFullPath );
    if ( convertedBackToSourceText != NULL )
    {
        zend_string_release( convertedBackToSourceText );
        convertedBackToSourceText = NULL;
    }

    ELASTIC_APM_UNUSED( resultCode );
    return;

    failure:
    goto finally;
}