blob: 9df937d3c3187348e081bbcb80346391d502b4a1 [file] [log] [blame]
Lorenz Brun70f65b22020-07-08 17:02:47 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package main
18
19import (
20 "context"
21 "fmt"
22 "os/exec"
23
Serge Bazanskic3ad8462021-01-08 16:45:51 +010024 "source.monogon.dev/metropolis/node"
Serge Bazanski31370b02021-01-07 16:31:14 +010025 "source.monogon.dev/metropolis/node/core/network"
Lorenz Brun70f65b22020-07-08 17:02:47 +020026)
27
Serge Bazanski216fe7b2021-05-21 18:36:16 +020028// initializeDebugger attaches Delve to ourselves and exposes it on
29// common.DebuggerPort
30// This is coupled to compilation_mode=dbg because otherwise Delve doesn't have
31// the necessary DWARF debug info
Lorenz Brun70f65b22020-07-08 17:02:47 +020032func initializeDebugger(networkSvc *network.Service) {
33 go func() {
Serge Bazanski216fe7b2021-05-21 18:36:16 +020034 // This is intentionally delayed until network becomes available since
35 // Delve for some reason connects to itself and in early-boot no
36 // network interface is available to do that through. Also external
37 // access isn't possible early on anyways.
Serge Bazanskid8af5bf2021-03-16 13:38:29 +010038 watcher := networkSvc.Watch()
39 _, err := watcher.Get(context.Background())
40 if err != nil {
41 panic(err)
42 }
Lorenz Brun313816f2020-12-22 16:52:26 +010043 dlvCmd := exec.Command("/dlv", "--headless=true", fmt.Sprintf("--listen=:%v", node.DebuggerPort),
Lorenz Brun70f65b22020-07-08 17:02:47 +020044 "--accept-multiclient", "--only-same-user=false", "attach", "--continue", "1", "/init")
45 if err := dlvCmd.Start(); err != nil {
46 panic(err)
47 }
48 }()
49}