ilmari-h / dlvtui

TUI Go debugger, frontend for Delve. WIP
Apache License 2.0
47 stars 4 forks source link

osx #1

Open dataf3l opened 1 year ago

dataf3l commented 1 year ago

Tried to build, it shows an error:

./main.go:61:3: unknown field 'Pdeathsig' in struct literal of type syscall.SysProcAttr

what to do?

oderwat commented 1 year ago

I also would like to use that on OSX and tried some stuff but didn't make it work (yet).

zanezhub commented 1 year ago

I got the same error in Windows 11

 go build -ldflags="-s -w"
.\main.go:61:3: unknown field Pdeathsig in struct literal of type syscall.SysProcAttr
dataf3l commented 1 year ago

@zanezhub maybe change main.go and who knows what else, I asked ChatGPT, it did this:

The given code appears to be written for Linux, and it makes use of specific commands and file paths that might not be valid or exist on Windows and macOS.

Here's how to make this code platform-agnostic:

  1. Platform-specific logic:

    • For things like paths, commands, and system calls, you should differentiate the logic based on the OS. This is done using the runtime package's GOOS constant.
  2. File Paths:

    • Linux often uses paths like /home/username/.... This is not valid on Windows (which uses C:\Users\username\...) or macOS.
    • Use Go's os and filepath packages to work with paths to make it OS-agnostic.
  3. System Calls:

    • syscall.SysProcAttr and Pdeathsig are Linux-specific. These aren't applicable to macOS and Windows.
  4. Commands:

    • The command being executed (dlv) should be available on all platforms. This will only work if the dlv command is available on the PATH of each OS.

Here's a simplified version of your code to make it work on all platforms:

// ... [other imports]

import (
    "runtime"
)

// ...

func startDebugger(commandArgs []string) int {
    log.Printf("Starting dlv-backend: dlv %s", strings.Join(commandArgs, " "))
    cmd := exec.Command(
        "dlv",
        commandArgs...,
    )

    // Platform-specific logic for Linux
    if runtime.GOOS == "linux" {
        cmd.SysProcAttr = &syscall.SysProcAttr{
            Pdeathsig: syscall.SIGKILL,
        }
    }

    // ... [rest of the function]
}

// ...

func init() {
    // ...

    // Platform-specific default log path logic
    defaultLogFile := "$XDG_DATA_HOME/dlvtui.log"
    if runtime.GOOS == "windows" {
        defaultLogFile = "$APPDATA/dlvtui.log"
    } else if runtime.GOOS == "darwin" { // macOS
        defaultLogFile = "$HOME/Library/Application Support/dlvtui.log"
    }
    exFlags.StringVar(&logfile, "logfile", defaultLogFile, "Path to the log file.")

    // ... [rest of the function]
}

// ...

func main() {
    // ...

    // Platform-specific logic to resolve dir
    var defaultPrefix string
    switch runtime.GOOS {
    case "linux":
        defaultPrefix = "/home/"
    case "windows":
        defaultPrefix = "C:/Users/"
    case "darwin":
        defaultPrefix = "/Users/"
    default:
        defaultPrefix = ""
    }

    // Replace /home/ with the relevant prefix for the OS
    for _, f := range fileList {
        if strings.HasPrefix(f, defaultPrefix) && !strings.Contains(f, "/go/pkg") {
            dir = filepath.Dir(f)
            break
        }
    }

    // ... [rest of the function]
}

Changes made:

Keep in mind:

I hope this ChatGPT thing is useful, have a nice day