Skip to content

Commit

Permalink
add gdbme flag that starting erigon under gdb (for debug) (#13874)
Browse files Browse the repository at this point in the history
closes #13586

---------

Co-authored-by: JkLondon <ilya@mikheev.fun>
  • Loading branch information
JkLondon and JkLondon authored Mar 1, 2025
1 parent 56dc96e commit afb69b5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,11 @@ var (
Name: "polygon.pos.ssf.block",
Usage: "Enabling Polygon PoS Single Slot Finality since block",
}
GDBMeFlag = cli.BoolFlag{
Name: "gdbme",
Usage: "restart erigon under gdb for debug purposes",
Value: false,
}
)

var MetricFlags = []cli.Flag{&MetricsEnabledFlag, &MetricsHTTPFlag, &MetricsPortFlag, &DiagDisabledFlag, &DiagEndpointAddrFlag, &DiagEndpointPortFlag, &DiagSpeedTestFlag}
Expand Down
52 changes: 52 additions & 0 deletions core/gdbme/gdbme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package gdbme

import (
"fmt"
"github.com/erigontech/erigon/cmd/utils"
"os"
"os/exec"
)

const gdbPath = "/usr/bin/gdb"

// restartUnderGDB relaunches the current process under GDB for debugging purposes.
func RestartUnderGDB() {
exePath, err := os.Executable()
if err != nil {
fmt.Fprintln(os.Stderr, "Error: could not determine executable path:", err)
os.Exit(1)
}

args := os.Args[1:]
filteredArgs := []string{}
for _, arg := range args {
if arg != "--"+utils.GDBMeFlag.Name {
filteredArgs = append(filteredArgs, arg)
}
}

gdbArgs := []string{
"-q",
"-batch",
"-nx",
"-nh",
"-return-child-result",
"-ex", "run",
"-ex", "bt full",
"--args",
exePath,
}
gdbArgs = append(gdbArgs, filteredArgs...)

fmt.Fprintln(os.Stderr, "Restarting under GDB for crash diagnostics...")
cmd := exec.Command(gdbPath, gdbArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin

err = cmd.Run()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to restart under GDB:", err)
os.Exit(1)
}
}
1 change: 1 addition & 0 deletions turbo/cli/default_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,5 @@ var DefaultFlags = []cli.Flag{

&utils.PolygonPosSingleSlotFinalityFlag,
&utils.PolygonPosSingleSlotFinalityBlockAtFlag,
&utils.GDBMeFlag,
}
15 changes: 12 additions & 3 deletions turbo/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@
// Package node contains classes for running an Erigon node.
package node

/*
#include <stdlib.h>
*/
import "C"

import (
"context"

"github.com/urfave/cli/v2"

"github.com/erigontech/erigon-lib/chain/networkname"
"github.com/erigontech/erigon-lib/kv"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon/core/gdbme"
"github.com/urfave/cli/v2"

"github.com/erigontech/erigon/cmd/utils"
"github.com/erigontech/erigon/eth"
Expand Down Expand Up @@ -114,6 +118,11 @@ func NewNodConfigUrfave(ctx *cli.Context, logger log.Logger) (*nodecfg.Config, e
return nil, err
}
erigoncli.ApplyFlagsForNodeConfig(ctx, nodeConfig, logger)

if ctx.Bool(utils.GDBMeFlag.Name) {
gdbme.RestartUnderGDB()
}

return nodeConfig, nil
}
func NewEthConfigUrfave(ctx *cli.Context, nodeConfig *nodecfg.Config, logger log.Logger) *ethconfig.Config {
Expand Down

0 comments on commit afb69b5

Please sign in to comment.