📄 gdb.exp
字号:
global gdb_prompt set count 0 while {$count < 10} { gdb_expect { -re "Quit this debugging session\\? \\(y or n\\) $" { send_gdb "n\n" incr count } -re "Create a core file of GDB\\? \\(y or n\\) $" { send_gdb "n\n" incr count } -re "$gdb_prompt $" { # We're resynchronized. return 1 } timeout { perror "Could not resync from internal error (timeout)" return 0 } } } perror "Could not resync from internal error (resync count exceeded)" return 0}# gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS# Send a command to gdb; test the result.## COMMAND is the command to execute, send to GDB with send_gdb. If# this is the null string no command is sent.# MESSAGE is a message to be printed with the built-in failure patterns# if one of them matches. If MESSAGE is empty COMMAND will be used.# EXPECT_ARGUMENTS will be fed to expect in addition to the standard# patterns. Pattern elements will be evaluated in the caller's# context; action elements will be executed in the caller's context.# Unlike patterns for gdb_test, these patterns should generally include# the final newline and prompt.## Returns:# 1 if the test failed, according to a built-in failure pattern# 0 if only user-supplied patterns matched# -1 if there was an internal error.# # You can use this function thus:## gdb_test_multiple "print foo" "test foo" {# -re "expected output 1" {# pass "print foo"# }# -re "expected output 2" {# fail "print foo"# }# }## The standard patterns, such as "Program exited..." and "A problem# ...", all being implicitly appended to that list.#proc gdb_test_multiple { command message user_code } { global verbose global gdb_prompt global GDB upvar timeout timeout upvar expect_out expect_out if { $message == "" } { set message $command } # TCL/EXPECT WART ALERT # Expect does something very strange when it receives a single braced # argument. It splits it along word separators and performs substitutions. # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is # evaluated as "\[ab\]". But that's not how TCL normally works; inside a # double-quoted list item, "\[ab\]" is just a long way of representing # "[ab]", because the backslashes will be removed by lindex. # Unfortunately, there appears to be no easy way to duplicate the splitting # that expect will do from within TCL. And many places make use of the # "\[0-9\]" construct, so we need to support that; and some places make use # of the "[func]" construct, so we need to support that too. In order to # get this right we have to substitute quoted list elements differently # from braced list elements. # We do this roughly the same way that Expect does it. We have to use two # lists, because if we leave unquoted newlines in the argument to uplevel # they'll be treated as command separators, and if we escape newlines # we mangle newlines inside of command blocks. This assumes that the # input doesn't contain a pattern which contains actual embedded newlines # at this point! regsub -all {\n} ${user_code} { } subst_code set subst_code [uplevel list $subst_code] set processed_code "" set patterns "" set expecting_action 0 foreach item $user_code subst_item $subst_code { if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } { lappend processed_code $item continue } if {$item == "-indices" || $item == "-re" || $item == "-ex"} { lappend processed_code $item continue } if { $expecting_action } { lappend processed_code "uplevel [list $item]" set expecting_action 0 # Cosmetic, no effect on the list. append processed_code "\n" continue } set expecting_action 1 lappend processed_code $subst_item if {$patterns != ""} { append patterns "; " } append patterns "\"$subst_item\"" } # Also purely cosmetic. regsub -all {\r} $patterns {\\r} patterns regsub -all {\n} $patterns {\\n} patterns if $verbose>2 then { send_user "Sending \"$command\" to gdb\n" send_user "Looking to match \"$patterns\"\n" send_user "Message is \"$message\"\n" } set result -1 set string "${command}\n"; if { $command != "" } { while { "$string" != "" } { set foo [string first "\n" "$string"]; set len [string length "$string"]; if { $foo < [expr $len - 1] } { set str [string range "$string" 0 $foo]; if { [send_gdb "$str"] != "" } { global suppress_flag; if { ! $suppress_flag } { perror "Couldn't send $command to GDB."; } fail "$message"; return $result; } # since we're checking if each line of the multi-line # command are 'accepted' by GDB here, # we need to set -notransfer expect option so that # command output is not lost for pattern matching # - guo gdb_expect 2 { -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 } timeout { verbose "partial: timeout" 3 } } set string [string range "$string" [expr $foo + 1] end]; } else { break; } } if { "$string" != "" } { if { [send_gdb "$string"] != "" } { global suppress_flag; if { ! $suppress_flag } { perror "Couldn't send $command to GDB."; } fail "$message"; return $result; } } } if [target_info exists gdb,timeout] { set tmt [target_info gdb,timeout]; } else { if [info exists timeout] { set tmt $timeout; } else { global timeout; if [info exists timeout] { set tmt $timeout; } else { set tmt 60; } } } set code { -re ".*A problem internal to GDB has been detected" { fail "$message (GDB internal error)" gdb_internal_error_resync } -re "\\*\\*\\* DOSEXIT code.*" { if { $message != "" } { fail "$message"; } gdb_suppress_entire_file "GDB died"; set result -1; } } append code $processed_code append code { -re "Ending remote debugging.*$gdb_prompt $" { if ![isnative] then { warning "Can`t communicate to remote target." } gdb_exit gdb_start set result -1 } -re "Undefined\[a-z\]* command:.*$gdb_prompt $" { perror "Undefined command \"$command\"." fail "$message" set result 1 } -re "Ambiguous command.*$gdb_prompt $" { perror "\"$command\" is not a unique command name." fail "$message" set result 1 } -re "Program exited with code \[0-9\]+.*$gdb_prompt $" { if ![string match "" $message] then { set errmsg "$message (the program exited)" } else { set errmsg "$command (the program exited)" } fail "$errmsg" set result -1 } -re "EXIT code \[0-9\r\n\]+Program exited normally.*$gdb_prompt $" { if ![string match "" $message] then { set errmsg "$message (the program exited)" } else { set errmsg "$command (the program exited)" } fail "$errmsg" set result -1 } -re "The program is not being run.*$gdb_prompt $" { if ![string match "" $message] then { set errmsg "$message (the program is no longer running)" } else { set errmsg "$command (the program is no longer running)" } fail "$errmsg" set result -1 } -re "\r\n$gdb_prompt $" { if ![string match "" $message] then { fail "$message" } set result 1 } "<return>" { send_gdb "\n" perror "Window too small." fail "$message" set result -1 } -re "\\(y or n\\) " { send_gdb "n\n" perror "Got interactive prompt." fail "$message" set result -1 } eof { perror "Process no longer exists" if { $message != "" } { fail "$message" } return -1 } full_buffer { perror "internal buffer is full." fail "$message" set result -1 } timeout { if ![string match "" $message] then { fail "$message (timeout)" } set result 1 } } set result 0 set code [catch {gdb_expect $tmt $code} string] if {$code == 1} { global errorInfo errorCode; return -code error -errorinfo $errorInfo -errorcode $errorCode $string } elseif {$code == 2} { return -code return $string } elseif {$code == 3} { return } elseif {$code > 4} { return -code $code $string } return $result}# gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE# Send a command to gdb; test the result.## COMMAND is the command to execute, send to GDB with send_gdb. If# this is the null string no command is sent.# PATTERN is the pattern to match for a PASS, and must NOT include# the \r\n sequence immediately before the gdb prompt.# MESSAGE is an optional message to be printed. If this is# omitted, then the pass/fail messages use the command string as the# message. (If this is the empty string, then sometimes we don't# call pass or fail at all; I don't understand this at all.)# QUESTION is a question GDB may ask in response to COMMAND, like# "are you sure?"# RESPONSE is the response to send if QUESTION appears.## Returns:# 1 if the test failed,# 0 if the test passes,# -1 if there was an internal error.# proc gdb_test { args } { global verbose global gdb_prompt global GDB upvar timeout timeout if [llength $args]>2 then { set message [lindex $args 2] } else { set message [lindex $args 0] } set command [lindex $args 0] set pattern [lindex $args 1] if [llength $args]==5 { set question_string [lindex $args 3]; set response_string [lindex $args 4]; } else { set question_string "^FOOBAR$" } return [gdb_test_multiple $command $message { -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" { if ![string match "" $message] then { pass "$message" } } -re "(${question_string})$" { send_gdb "$response_string\n"; exp_continue; } }]}# Test that a command gives an error. For pass or fail, return# a 1 to indicate that more tests can proceed. However a timeout# is a serious error, generates a special fail message, and causes# a 0 to be returned to indicate that more tests are likely to fail# as well.proc test_print_reject { args } { global gdb_prompt global verbose if [llength $args]==2 then { set expectthis [lindex $args 1] } else { set expectthis "should never match this bogus string" } set sendthis [lindex $args 0] if $verbose>2 then { send_user "Sending \"$sendthis\" to gdb\n" send_user "Looking to match \"$expectthis\"\n" } send_gdb "$sendthis\n" #FIXME: Should add timeout as parameter. gdb_expect { -re "A .* in expression.*\\.*$gdb_prompt $" { pass "reject $sendthis" return 1 } -re "Invalid syntax in expression.*$gdb_prompt $" { pass "reject $sendthis" return 1 } -re "Junk after end of expression.*$gdb_prompt $" { pass "reject $sendthis" return 1 } -re "Invalid number.*$gdb_prompt $" { pass "reject $sendthis" return 1 } -re "Invalid character constant.*$gdb_prompt $" { pass "reject $sendthis" return 1 } -re "No symbol table is loaded.*$gdb_prompt $" { pass "reject $sendthis" return 1 } -re "No symbol .* in current context.*$gdb_prompt $" { pass "reject $sendthis" return 1 } -re "Unmatched single quote.*$gdb_prompt $" { pass "reject $sendthis" return 1 } -re "A character constant must contain at least one character.*$gdb_prompt $" { pass "reject $sendthis" return 1 } -re "$expectthis.*$gdb_prompt $" { pass "reject $sendthis" return 1 } -re ".*$gdb_prompt $" { fail "reject $sendthis" return 1 } default { fail "reject $sendthis (eof or timeout)" return 0 } }}# Given an input string, adds backslashes as needed to create a# regexp that will match the string.proc string_to_regexp {str} { set result $str regsub -all {[]*+.|()^$\[\\]} $str {\\&} result return $result}# Same as gdb_test, but the second parameter is not a regexp,# but a string that must match exactly.proc gdb_test_exact { args } { upvar timeout timeout set command [lindex $args 0] # This applies a special meaning to a null string pattern. Without # this, "$pattern\r\n$gdb_prompt $" will match anything, including error # messages from commands that should have no output except a new # prompt. With this, only results of a null string will match a null # string pattern. set pattern [lindex $args 1] if [string match $pattern ""] { set pattern [string_to_regexp [lindex $args 0]] } else { set pattern [string_to_regexp [lindex $args 1]] } # It is most natural to write the pattern argument with only # embedded \n's, especially if you are trying to avoid Tcl quoting # problems. But gdb_expect really wants to see \r\n in patterns. So # transform the pattern here. First transform \r\n back to \n, in # case some users of gdb_test_exact already do the right thing. regsub -all "\r\n" $pattern "\n" pattern regsub -all "\n" $pattern "\r\n" pattern if [llength $args]==3 then { set message [lindex $args 2] } else { set message $command }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -