Normally I invoke my tcl script under shell like this.

> tclsh8.5 mytest.tcl -opt1 foo -opt2 bar

In case need to launch gdb to debug due to some modules implemented in C++. I have to launch tclsh via gdb. So the question is how to execute my script in tcl sh with arguments.

I need something like:

tclsh> run mytest.tcl -opt1 foo -opt2 bar

Using exec is not ideal as it folks another process and losses my breakpoints settings.

tclsh> exec mytest.tcl -opt1 foo -opt2 bar
有帮助吗?

解决方案

I would think something like the following should work for you:

set argv [list -opt1 foo -opt2 bar]
set argc 4
source mytest.tcl

So set argv and argc to get the arguments correct and then just source in your Tcl code to execute.

Alternatively the gdb run command allows you to pass command line arguments to the executable to be debugged. So if your debugging tclsh then the what is the problem with the run command as follows?

run mytest.tcl -opt1 foo -opt2 bar

For example under cygwin I'm able to do the following:

$ tclsh test.tcl
This is a test
$ gdb -q tclsh.exe
(no debugging symbols found)
(gdb) run test.tcl
Starting program: /usr/bin/tclsh.exe test.tcl

其他提示

If you are running tclsh within a gdb session and set arguments, you do something like this ($ is a shell prompt, (gdb) is a gdb prompt, and I've left out all the messages that get printed by gdb):

$ gdb tclsh
(gdb) set args mytest.tcl -opt1 foo -opt2 bar
(gdb) ... set some breakpoints ...
(gdb) run

You might also need set env FOO=bar to set up the environment, depending on what is going on in your script. Tcl's own build files use techniques like this to pass in arguments when debugging the running of the test suite.

Why do not just run

gdb --args tclsh8.5 mytest.tcl -opt1 foo -opt2 bar

when you need to debug your application?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top