[PYTHON] Turn off optimization options in the Google App Engine Go development environment dev_appserver.py

Introduction

In Google App Engine, the development environment is started by executing the script called dev_appserver.py.

I want to use the debugger in the GAE / Go environment, but in dev_appserver.py, I can't pass the option for go build, and it always compiles in the optimized state. When debugging, I want to debug while properly matching the source code without optimizing it. (It is difficult to debug if the instruction order is changed by optimization)

This article describes how to build with optimization options off and how the debugger works.

If you just want to see the conclusion, see "Turn off optimization options".

Optimization options

If you check the help for go build, you will see that there is an option called --gcflags.

$ go build --help
Abbreviation
-gcflags 'arg list'
        arguments to pass on each go tool compile invocation.

I don't know the details with this alone, but since go build executes the go tool compile command when actually compiling, this [compile --The Go Programming Language](https: // golang) Check .org / cmd / compile /).

Then you can see that the following two are likely to be needed when debugging. By stopping optimization and inlining, the source code and the executable binary will be able to correspond correctly.

-N
	Disable optimizations.
-l
	Disable inlining.

In other words, when building with dev_appserver.py, it would be nice if you could do the following.

$ go build -gcflags '-N -l'

How to build with dev_appserver.py

Next, I would like to see how dev_appserver.py is building.

Starting dev_appserver.py launches a thread for the API server, each module created by the developer, and the admin console.

It is the module thread that builds the source code. It also monitors file updates and automatically builds when there are changes.

The build is realized using the tool $ (SDK_PATH) / goroot / bin / go-app-builder inside the SDK. It's basically like go build, but it's customized specifically for GAE / Go.

The internal implementation of go-app-builder is the source code of go that exists in $ (SDK_PATH) / goroot / src / cmd / go-app-builder.

Let's read the help of go-app-builder.

$ (SDK_PATH)/goroot/src/cmd/go-app-builder --help
Abbreviation
-gcflags 'arg list'
        arguments to pass on each go tool compile invocation.

It seems that you can pass -gcflags as well as go build. In other words, it turns out that there is no need to modify the source code of go-app-builder. So if you know how dev_appserver.py runs go-app-builder, you can handle it.

If you grep with go-app-builder, you can see that go-app-builder is running at $ (SDK_PATH) /google/appengine/tools/devappserver2/go_application.py.

Turn off optimization options

There is a go-app-builder option around line 86 of $ (SDK_PATH) /google/appengine/tools/devappserver2/go_application.py, so add -gcflags.

  gab_args = [
      _GAB_PATH,
      '-app_base', application_root,
      '-arch', arch,
      '-dynamic',
      '-goroot', GOROOT,
      '-gopath', os.environ.get('GOPATH', GOPATH),
      '-nobuild_files', '^' + str(nobuild_files),
      '-incremental_rebuild',
      '-unsafe',
+     '-gcflags', '-N -l',
  ]

If you actually move it with this, you can confirm that it has been built without problems. Also, when I output the log at build time, I was able to confirm that the option was passed at compile time.

But I didn't know how to check if the build was really unoptimized. If the binary is generated, I can check if it is optimized from the binary ...

Turn optimization on and off with command line options in dev_appserver.py

I forcibly modified the source code earlier, but ideally I would like to pass an option to dev_appserver.py to turn optimization on and off. GAE / PHP provides such an option.

$ dev_appserver.py --help
  --php_remote_debugging [PHP_REMOTE_DEBUGGING]
                        enable XDebug remote debugging (default: False)

I read the source code thinking that if I make the same changes as PHP, I could go with Go, but Protocol Buffers is used internally, and there is automatically generated code, but the important I'm stuck because the .proto file isn't in the SDK.

in conclusion

Gogland developers have suggested these to Google, but they haven't worked for a long time. Perhaps Google has its own development environment and doesn't attach much importance to dev_appserver.py ...

reference

Recommended Posts

Turn off optimization options in the Google App Engine Go development environment dev_appserver.py
Google App Engine / Python development environment construction procedure (late 2014)
Google App Engine development with Docker
Building a development environment with Maven on Google App Engine [Java]
[Google App Engine] Flow from development environment construction to application creation
Runtime version of Google App Engine / Python Standard Environment
Development environment in Python
Django + MongoDB development environment maintenance (in the middle of writing)
Deploy your Go app on Google App Engine with GitHub Actions