Debugging an AutoIt script or get currently executed script line number

馋奶兔 提交于 2019-11-30 17:26:39

问题


My AutoIt script sends a list of clicks and key-presses to automate an old closed source application.

It has bugs so I want to know how I can debug AutoIt scripts. Or at least output the script's line number (to show code executed in real time).


回答1:


In SciTE from Tools select “Trace: Add trace lines”. This will add a ConsoleWrite to each line if nothing is selected. If you select some code first it will add ConsoleWrite lines to what you have selected.

If you are getting an error in your compiled code you can add this to the top of your script before compiling it. When it errors out it will give you the right line number in your script.

#Au3Stripper_Parameters=/mo



回答2:


How can I debug AutoIt code?

Syntax

Au3Check

As per Documentation - Intro - AutoIt Syntax Checker (Au3Check):

Checks the script for syntax errors.

Example:

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7

Reports (almost) every non-runtime error, prior to execution and compile.

Runtime

Current line

… show me which code is executed in real time …

As per Documentation - Function Reference - AutoItSetOption():

TrayIconDebug If enabled shows the current script line in the tray icon tip to help debugging.

Example:

AutoItSetOption("TrayIconDebug", 1)

Compiled scripts

#AutoIt3Wrapper_Run_Debug_Mode=Y runs script with console debugging.

Due to prepended include files, line numbers (in error messages) of compiled scripts do not match original .au3 file. As per Documentation - SciTE4AutoIt3 - Au3Stripper:

If there are no errors, run Au3Stripper to create an Stripped source file Scriptname_Stripped in the same folder containing all the source (the script and any #include files)

Example:

#AutoIt3Wrapper_Run_Au3Stripper=y

Line numbers in error messages of compiled scripts match those in scriptname_stripped.au3 now (present after compile).

Error codes and return values

  • ConsoleWrite(). Add #AutoIt3Wrapper_Change2CUI=y if to be read from compiled script.
    • Macros of use include @error, @extended and @ScriptLineNumber
    • SciTE4AutoIt3 > Tools > Trace: Add Trace Lines inserts such a command for every line. Displays current @error code (and concerning line).
    • SciTE4AutoIt3 > Tools > Debug to Console (Alt + D) inserts such a command for currently selected line. Displays @ScriptLineNumber, return value and @error code. Executes original line a second time for its return value (may be undesired).
  • VarGetType() returns the internal type representation of a variant.
  • _ArrayDisplay() to view structure and content of array variables.
  • Possibly log to file or database in case of remote debugging requirement.
  • Error logging UDF's (example) for simplification.

Assertions

As per Documentation - User Defined Function Reference - _Assert():

Display a message if assertion fails

Related. Useful for regression tests and to verify edge cases and assumptions (or to simply abort in case of unexpected circumstances). Related functions include:

  • IsBinary()
  • IsBool()
  • IsDllStruct()
  • IsFloat()
  • IsHWnd()
  • IsInt()
  • IsNumber()
  • IsPtr()
  • IsString()
  • IsDeclared()
  • IsFunc()
  • IsKeyWord()
  • FileExists()

Error handling

Generally functions return either- (or a combination of) return value or -error code. Non 0 error code values (sometimes negative) signify failure. Return values (if used as such) alternate between 0 (failure) and 1 (success).

Handle errors as they appear. Scripts debug themselves provided for correct error handling. Avoid progressively nesting If -statements. Example:

Global Enum  $RANDOM_FLT, _
             $RANDOM_INT

Global Enum  $ERROR_OK, _
             $ERROR_AIR, _
             $ERROR_WATER, _
             $ERROR_FOOD, _
             $ERROR_ENUM

Global       $g_aError[$ERROR_ENUM]
             $g_aError[$ERROR_OK]    = 'survived'
             $g_aError[$ERROR_AIR]   = 'no air'
             $g_aError[$ERROR_WATER] = 'no water'
             $g_aError[$ERROR_FOOD]  = 'no food'

Global Const $g_sMsgConsole          = 'error:%i - %s\n'

While True
    Live()

    If @error Then
        ConsoleWrite(StringFormat($g_sMsgConsole, @error, $g_aError[@error])); And one of following:
        ContinueLoop
;       ExitLoop
;       Return; If in Local scope.
;       Exit @error
    EndIf

    ; Continuation conditional to successful execution of Live():
    ConsoleWrite(StringFormat($g_sMsgConsole, @error, $g_aError[@error]))
WEnd

Func Live()
    Local Const $iError    = Random($ERROR_OK, $ERROR_ENUM - 1, $RANDOM_INT), _
                $iExtended = 0, _
                $iReturn   = $iError ? 0 : 1

    Return SetError($iError, $iExtended, $iReturn)
EndFunc

Or more specifically:

Live()

Switch @error

    Case $ERROR_AIR
        ; Handle specific error here.

    Case $ERROR_WATER
        ; Handle specific error here.

    Case $ERROR_FOOD
        ; Handle specific error here.

EndSwitch

Return values enable constructs like:

If Not SomeFunction() Then
    ; Handle here.
EndIf
; Continuation of script here.

Correlating error codes to text messages may help. Especially useful if shared among multiple related functions that call upon each other transparently returning encountered error codes (some FindOnPage() returning $ERR_PAGELOAD from a LoadPage() dependency for example).



来源:https://stackoverflow.com/questions/34109244/debugging-an-autoit-script-or-get-currently-executed-script-line-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!