Revel调试 – 如何进行调试

huangapple go评论91阅读模式
英文:

Revel debugging - How to

问题

有人可以提供一些关于如何调试Revel应用程序的指导吗?也许我很笨,但我无法做到。一些清晰的步骤将会非常有帮助。

英文:

Can some one provide some guidance about how to debug a Revel application. I might be dumb but I can't manage to do it. Some clear steps would be of real help.

答案1

得分: 9

Revel只是在标准Go库的基础上添加了一些库,并创建了一些包装器,为您提供了一个良好的结构。

您可以像调试其他Go应用程序一样使用GDB来调试它。

更具体地说,您可以首先构建它,然后调试相应的可执行文件(假设我的GOPATH是~/workspace):

[user@host ~]$ workspace/bin/revel new app
~
~ revel! http://revel.github.io
~
您的应用程序已准备就绪:
/home/user/workspace/src/app

您可以使用以下命令运行它:
revel run app
[user@host ~]$ workspace/bin/revel build app app
~
~ revel! http://revel.github.io
~
INFO 2014/05/30 12:21:25 revel.go:320: Loaded module static
TRACE 2014/05/30 12:21:25 build.go:128: Exec: [/usr/bin/git --git-dir=/home/user/workspace/src/app/.git describe --always --dirty]
WARN 2014/05/30 12:21:25 build.go:132: Cannot determine git repository version: exit status 128
TRACE 2014/05/30 12:21:25 build.go:77: Exec: [/home/user/go/bin/go build -ldflags -X app/app.APP_VERSION "" -tags -o /home/user/workspace/bin/app app/app/tmp]

然后,您将在/home/user/workspace/bin/app中拥有一个独立的可执行文件,您可以像这样独立于revel运行它(请注意,命令行参数是必需的,否则程序将崩溃):

[user@host ~]$ workspace/bin/app -importPath app revel -srcPath "workspace/src" -runMode dev
INFO 2014/05/30 12:51:40 revel.go:320: Loaded module static
INFO 2014/05/30 12:51:40 revel.go:320: Loaded module testrunner
INFO 2014/05/30 12:51:40 main.go:26: Running revel server
Listening on :9000...

然后,您可以像调试其他程序一样使用GDB调试此可执行文件:

[user@host ~]$ gdb -silent --args workspace/bin/app -importPath app revel -srcPath "workspace/src" -runMode dev
Reading symbols from workspace/bin/app...done.
Loading Go Runtime support.

然后使用GDB命令启动程序并设置断点:

(gdb) start
Temporary breakpoint 1 at 0x400c00: file /home/user/workspace/src/app/app/tmp/main.go, line 23.
Starting program: /home/user/workspace/bin/app -importPath app revel -srcPath workspace/src -runMode dev
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[New Thread 0x7fffe7804700 (LWP 5742)]

Temporary breakpoint 1, main.main () at /home/user/workspace/src/app/app/tmp/main.go:23
23 func main() {
(gdb) b workspace/src/app/app/controllers/app.go:9
Breakpoint 2 at 0x465610: file /home/user/workspace/src/app/app/controllers/app.go, line 9.
(gdb) cont
Continuing.
INFO 2014/05/30 13:12:31 revel.go:320: Loaded module static
INFO 2014/05/30 13:12:31 main.go:26: Running revel server
TRACE 2014/05/30 13:12:31 controller.go:375: Registered controller: App
TRACE 2014/05/30 13:12:31 controller.go:375: Registered controller: Static
TRACE 2014/05/30 13:12:31 watcher.go:94: Watching: /home/user/workspace/src/app/app/views
TRACE 2014/05/30 13:12:31 watcher.go:94: Watching: /home/user/workspace/src/app/app/views/App
TRACE 2014/05/30 13:12:31 watcher.go:94: Watching: /home/user/workspace/src/app/app/views/errors
TRACE 2014/05/30 13:12:31 watcher.go:94: Watching: /home/user/workspace/src/github.com/revel/revel/templates
TRACE 2014/05/30 13:12:31 watcher.go:94: Watching: /home/user/workspace/src/github.com/revel/revel/templates/errors
TRACE 2014/05/30 13:12:31 i18n.go:119: Successfully loaded messages from file sample.en
TRACE 2014/05/30 13:12:31 watcher.go:72: Watching: /home/user/workspace/src/app/conf/routes
[New Thread 0x7fffe6ea3700 (LWP 5743)]
[New Thread 0x7fffe66a2700 (LWP 5744)]
[New Thread 0x7fffe5ea1700 (LWP 5745)]
[New Thread 0x7fffe56a0700 (LWP 5746)]
Listening on :9000...

此时,您的程序正在运行,但尚未调用您的控制器:只有在收到查询时才会执行代码,因此转到浏览器并访问:"localhost:9000"(或您在app.conf中配置的端口)以触发响应:

TRACE 2014/05/30 13:12:36 template.go:174: Refreshing templates from [/home/user/workspace/src/app/app/views /home/user/workspace/src/github.com/revel/revel/templates]
INFO 2014/05/30 13:12:36 router.go:293: Skipping routes for inactive module testrunner
TRACE 2014/05/30 13:12:36 i18n.go:183: Unable to read locale cookie with name 'REVEL_LANG': http: named cookie not present
TRACE 2014/05/30 13:12:36 i18n.go:149: Found Accept-Language header value: en-GB
[Switching to Thread 0x7fffe56a0700 (LWP 5746)]

Breakpoint 2, app/app/controllers.App.Index (c=..., ~anon0=...) at /home/user/workspace/src/app/app/controllers/app.go:9
9 func (c App) Index() revel.Result {
(gdb)

现在您已经在控制器的断点处。

然后,您可以继续使用GDB。(有很多与GDB的接口,从命令行(如CGDB)到图形界面,或者包含在IDE中(如GoClipse)的接口,但最终都归结为相同的过程。

祝您的代码好运!

英文:

Revel is simply a set of libraries on top of the standard Go libraries, plus some wrappers creating a nice structure for you.

You can debug it like any other go applications using GDB.

More specifically, you can build it first and then debug the corresponding executable (this assumes my GOPATH is ~/workspace):

[user@host ~]$ workspace/bin/revel new app
~
~ revel! http://revel.github.io
~
Your application is ready:
    /home/user/workspace/src/app

You can run it with:
   revel run app
[user@host ~]$ workspace/bin/revel build app app
~
~ revel! http://revel.github.io
~
INFO  2014/05/30 12:21:25 revel.go:320: Loaded module static
TRACE 2014/05/30 12:21:25 build.go:128: Exec: [/usr/bin/git --git-dir=/home/user/workspace/src/app/.git describe --always --dirty]
WARN  2014/05/30 12:21:25 build.go:132: Cannot determine git repository version: exit status 128
TRACE 2014/05/30 12:21:25 build.go:77: Exec: [/home/user/go/bin/go build -ldflags -X app/app.APP_VERSION "" -tags  -o /home/user/workspace/bin/app app/app/tmp]

You then have an self-contained executable app at /home/user/workspace/bin/app, which you can run independently from revel like so (note that cmd line arguments are mandatory, otherwise the program will crash):

[user@host ~]$ workspace/bin/app -importPath app revel -srcPath "workspace/src" -runMode dev
INFO  2014/05/30 12:51:40 revel.go:320: Loaded module static
INFO  2014/05/30 12:51:40 revel.go:320: Loaded module testrunner
INFO  2014/05/30 12:51:40 main.go:26: Running revel server
Listening on :9000...

You can then debug this executable using GDB as you would do any other program:

[user@host ~]$ gdb -silent --args workspace/bin/app -importPath app revel -srcPath "workspace/src" -runMode dev
Reading symbols from workspace/bin/app...done.
Loading Go Runtime support.

Then using the GDB commands to start the program and set breakpoints:

(gdb) start
Temporary breakpoint 1 at 0x400c00: file /home/user/workspace/src/app/app/tmp/main.go, line 23.
Starting program: /home/user/workspace/bin/app -importPath app revel -srcPath workspace/src -runMode dev
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[New Thread 0x7fffe7804700 (LWP 5742)]

Temporary breakpoint 1, main.main () at /home/user/workspace/src/app/app/tmp/main.go:23
23      func main() {
(gdb) b workspace/src/app/app/controllers/app.go:9
Breakpoint 2 at 0x465610: file /home/user/workspace/src/app/app/controllers/app.go, line 9.
(gdb) cont
Continuing.
INFO  2014/05/30 13:12:31 revel.go:320: Loaded module static
INFO  2014/05/30 13:12:31 main.go:26: Running revel server
TRACE 2014/05/30 13:12:31 controller.go:375: Registered controller: App
TRACE 2014/05/30 13:12:31 controller.go:375: Registered controller: Static
TRACE 2014/05/30 13:12:31 watcher.go:94: Watching: /home/user/workspace/src/app/app/views
TRACE 2014/05/30 13:12:31 watcher.go:94: Watching: /home/user/workspace/src/app/app/views/App
TRACE 2014/05/30 13:12:31 watcher.go:94: Watching: /home/user/workspace/src/app/app/views/errors
TRACE 2014/05/30 13:12:31 watcher.go:94: Watching: /home/user/workspace/src/github.com/revel/revel/templates
TRACE 2014/05/30 13:12:31 watcher.go:94: Watching: /home/user/workspace/src/github.com/revel/revel/templates/errors
TRACE 2014/05/30 13:12:31 i18n.go:119: Successfully loaded messages from file sample.en
TRACE 2014/05/30 13:12:31 watcher.go:72: Watching: /home/user/workspace/src/app/conf/routes
[New Thread 0x7fffe6ea3700 (LWP 5743)]
[New Thread 0x7fffe66a2700 (LWP 5744)]
[New Thread 0x7fffe5ea1700 (LWP 5745)]
[New Thread 0x7fffe56a0700 (LWP 5746)]
Listening on :9000...

At this point, your program is running, but your controllers are not (yet) called: the code will only be executed when a query comes in, so go to your browser and access: "localhost:9000" (or whatever port you configured in app.conf) to trigger the response:

TRACE 2014/05/30 13:12:36 template.go:174: Refreshing templates from [/home/user/workspace/src/app/app/views /home/user/workspace/src/github.com/revel/revel/templates]
INFO  2014/05/30 13:12:36 router.go:293: Skipping routes for inactive module testrunner
TRACE 2014/05/30 13:12:36 i18n.go:183: Unable to read locale cookie with name 'REVEL_LANG': http: named cookie not present
TRACE 2014/05/30 13:12:36 i18n.go:149: Found Accept-Language header value: en-GB
[Switching to Thread 0x7fffe56a0700 (LWP 5746)]

Breakpoint 2, app/app/controllers.App.Index (c=..., ~anon0=...) at /home/user/workspace/src/app/app/controllers/app.go:9
9       func (c App) Index() revel.Result {
(gdb) 

And you are now at your breakpoint in your controller.

And then proceed using GDB. (There are plenty of interface to GDB, from command line (like CGDB) to graphical ones, or ones included in IDEs (like in GoClipse), but it all boils down to the same process eventually.

Good luck with your code !

huangapple
  • 本文由 发表于 2014年5月30日 18:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/23952886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定