From 453c34afa3c4fd380f59742771558a47ff2f0a3c Mon Sep 17 00:00:00 2001 From: Brady Dowling Date: Fri, 23 Feb 2018 15:07:51 -0800 Subject: [PATCH] Allow debugger args to be passed to via environment variable (#307) --- README.md | 12 ++++++++++++ runtime.go | 27 +++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 65cfa319ed..b751efc03d 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,18 @@ Unlike Node.JS and Java, Python requires you to enable remote debugging in your > Please note, due to a [open bug](https://github.com/Microsoft/vscode-python/issues/71) with Visual Studio Code, you may get a `Debug adapter process has terminated unexpectedly` error when attempting to debug Python applications with this IDE. Please track the [GitHub issue](https://github.com/Microsoft/vscode-python/issues/71) for updates. +#### Passing Additional Runtime Debug Arguments + +To pass additional runtime arguments when debugging your function, use the environment variable `DEBUGGER_ARGUMENTS`. This will pass a string of arguments directly into the run command SAM Local uses to start your function. + +For example, if you want to load a debugger like iKPdb at runtime of your Python function, you could pass the following as `DEBUGGER_ARGUMENTS`: `-m ikpdb --ikpdb-port=5858 --ikpdb-working-directory=/var/task/ --ikpdb-client-working-directory=/myApp --ikpdb-address=0.0.0.0`. This would load iKPdb at runtime with the other arguments you've specified. In this case, your full SAM local command would be: + +```bash +$ DEBUGGER_ARGUMENTS="-m ikpdb --ikpdb-port=5858 --ikpdb-working-directory=/var/task/ --ikpdb-client-working-directory=/myApp --ikpdb-address=0.0.0.0" echo {} | sam local invoke -d 5858 myFunction +``` + +You may pass debugger arguments to functions of all runtimes. + ### Connecting to docker network Both `sam local invoke` and `sam local start-api` support connecting the create lambda docker containers to an existing docker network. diff --git a/runtime.go b/runtime.go index 91cb37c7c4..3131c2fa98 100644 --- a/runtime.go +++ b/runtime.go @@ -432,12 +432,17 @@ func (r *Runtime) getDebugEntrypoint() (overrides []string) { if len(r.DebugPort) == 0 { return } + debuggerArgs := os.Getenv("DEBUGGER_ARGS") + debuggerArgsArray := strings.Split(debuggerArgs, " ") switch r.Name { // configs from: https://github.com/lambci/docker-lambda // to which we add the extra debug mode options case runtimeName.java8: overrides = []string{ "/usr/bin/java", + } + overrides = append(overrides, debuggerArgsArray...) + overrides = append(overrides, "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,quiet=y,address=" + r.DebugPort, "-XX:MaxHeapSize=1336935k", "-XX:MaxMetaspaceSize=157286k", @@ -447,10 +452,13 @@ func (r *Runtime) getDebugEntrypoint() (overrides []string) { "-XX:-TieredCompilation", "-jar", "/var/runtime/lib/LambdaJavaRTEntry-1.0.jar", - } + ) case runtimeName.nodejs: overrides = []string{ "/usr/bin/node", + } + overrides = append(overrides, debuggerArgsArray...) + overrides = append(overrides, "--debug-brk=" + r.DebugPort, "--nolazy", "--max-old-space-size=1229", @@ -458,10 +466,13 @@ func (r *Runtime) getDebugEntrypoint() (overrides []string) { "--max-executable-size=153", "--expose-gc", "/var/runtime/node_modules/awslambda/bin/awslambda", - } + ) case runtimeName.nodejs43: overrides = []string{ "/usr/local/lib64/node-v4.3.x/bin/node", + } + overrides = append(overrides, debuggerArgsArray...) + overrides = append(overrides, "--debug-brk=" + r.DebugPort, "--nolazy", "--max-old-space-size=1229", @@ -469,10 +480,13 @@ func (r *Runtime) getDebugEntrypoint() (overrides []string) { "--max-executable-size=153", "--expose-gc", "/var/runtime/node_modules/awslambda/index.js", - } + ) case runtimeName.nodejs610: overrides = []string{ "/var/lang/bin/node", + } + overrides = append(overrides, debuggerArgsArray...) + overrides = append(overrides, "--debug-brk=" + r.DebugPort, "--nolazy", "--max-old-space-size=1229", @@ -480,8 +494,13 @@ func (r *Runtime) getDebugEntrypoint() (overrides []string) { "--max-executable-size=153", "--expose-gc", "/var/runtime/node_modules/awslambda/index.js", + ) + case runtimeName.python27: + overrides = []string{ + "/usr/bin/python2.7", } - // TODO: also add debug mode for Python runtimes + overrides = append(overrides, debuggerArgsArray...) + overrides = append(overrides, "/var/runtime/awslambda/bootstrap.py") } return }