【问题标题】:How to execute tcl scripts in tchsh with arguments如何使用参数在 tchsh 中执行 tcl 脚本
【发布时间】:2012-08-15 08:13:07
【问题描述】:

通常我会像这样在 shell 下调用我的 tcl 脚本。

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

如果由于某些 C++ 实现的模块需要启动 gdb 进行调试。我必须通过 gdb 启动 tclsh。所以问题是如何在 tcl sh 中使用参数执行我的脚本。

我需要类似的东西:

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

使用 exec 并不理想,因为它会占用另一个进程并丢失我的断点设置。

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

【问题讨论】:

    标签: shell tcl tclsh


    【解决方案1】:

    我认为以下内容应该适合您:

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

    所以设置 argv 和 argc 以获取正确的参数,然后只需在 Tcl 代码中执行源代码即可。

    或者,gdb run 命令允许您将命令行参数传递给要调试的可执行文件。那么如果你调试tclsh那么运行命令如下有什么问题呢?

    run mytest.tcl -opt1 foo -opt2 bar
    

    例如在 cygwin 下我可以做到以下几点:

    $ 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
    

    【讨论】:

    • "run mytest.tcl -opt1 foo ..." 看起来很棒。我该如何在 gdb 中做到这一点? 1. > gdb tclsh 2. (输入 tclsh), 按 ctrl-C 切换到 gdb shell 3. (gdb) break myCppCode.cc:123 (make bp pending on future lib loaded) 4. (gdb) continue 5. start从一开始呢? (是或否)我应该在这里做什么?如果选择“是”,则不会在断点处停止。如果选择“否”,它会切换回 tclsh 并且什么也不做。有什么遗漏的吗?
    • 当您在 gdb 下运行 tclsh 时,您应该得到标准 (gdb) 命令提示符。您应该可以在此处设置断点,然后使用指定的参数运行程序。
    【解决方案2】:

    如果您在 gdb 会话中运行 tclsh 并设置参数,您可以执行以下操作($ 是 shell 提示符,(gdb) 是 gdb 提示符,我忽略了所有打印的消息通过 gdb):

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

    您可能还需要set env FOO=bar 来设置环境,具体取决于您的脚本中发生的情况。 Tcl 自己的构建文件使用这样的技术在调试测试套件的运行时传递参数。

    【讨论】:

    • 啊,我没有设置 env FOO=bar。这样我的断点就不会被触发,因为它附加到发布版本。
    【解决方案3】:

    为什么不直接运行

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

    何时需要调试应用程序?

    【讨论】:

    • 因为我需要设置断点,否则它只会执行并直接终止。
    • gdb 将以交互模式启动。您可以设置断点并继续使用run 命令。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2021-05-20
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多