Skip to content

Commit

Permalink
tools/dbg: added adb like serial port debugger tool for rlxos
Browse files Browse the repository at this point in the history
  • Loading branch information
itsManjeet committed Dec 29, 2024
1 parent 4cefec6 commit eb39003
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 147 deletions.
4 changes: 0 additions & 4 deletions core/cmd/dbgd/dbgd.service

This file was deleted.

89 changes: 0 additions & 89 deletions core/cmd/dbgd/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion core/cmd/shell/[email protected]
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exec-start: /core/shell
exec-start: /bin/sh
restart: true
tty: /dev/%i
File renamed without changes.
9 changes: 8 additions & 1 deletion core/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ func (s *Service) Start(journal *os.File) error {
}

uid, gid := 0, 0
groups := []uint32{}
var groups []uint32

ttyOwner := false
if s.User != "" {
usr, err := user.Lookup(s.User)
if err != nil {
Expand Down Expand Up @@ -181,6 +183,10 @@ func (s *Service) Start(journal *os.File) error {

cmd := exec.Command(args[0], args[1:]...)
if s.TTY != "" {
ttyOwner = true
if err := os.Chown(s.TTY, uid, gid); err != nil {
return err
}
tty, err := os.OpenFile(s.TTY, os.O_RDWR, 0)
if err != nil {
return err
Expand All @@ -199,6 +205,7 @@ func (s *Service) Start(journal *os.File) error {
Gid: uint32(gid),
Groups: groups,
},
Setsid: ttyOwner,
}

if err := cmd.Start(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions devices/generic/x86_64/make-iso.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ set timeout=10
menuentry "RLXOS GNU/Linux" {
insmod all_video
linux /boot/bzImage root=LABEL=RLXOS rootfs-type=iso9660 live quiet console=tty0 console=ttyS0,115200
linux /boot/bzImage root=LABEL=RLXOS rootfs-type=iso9660 live quiet console=ttyS0
initrd /boot/initramfs.img
}
EOF
Expand All @@ -50,7 +50,7 @@ install -vDm0644 /dev/stdin $BINARIES_DIR/iso/isolinux/isolinux.cfg << EOF
DEFAULT RLXOS
LABEL RLXOS
KERNEL /boot/bzImage
APPEND initrd=/boot/initramfs.img root=LABEL=RLXOS rootfs-type=iso9660 live quiet console=tty0 console=ttyS0,115200
APPEND initrd=/boot/initramfs.img root=LABEL=RLXOS rootfs-type=iso9660 live quiet console=ttyS0
EOF

cp $BINARIES_DIR/bzImage \
Expand Down
2 changes: 2 additions & 0 deletions devices/generic/x86_64/toolchain.conf
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ BR2_PACKAGE_CA_CERTIFICATES=y
BR2_PACKAGE_WLROOTS=y
BR2_PACKAGE_WLROOTS_X11=y
BR2_PACKAGE_WLROOTS_XWAYLAND=y
BR2_PACKAGE_SEATD_BUILTIN=y
BR2_PACKAGE_SEATD_DAEMON=y
BR2_TARGET_ROOTFS_SQUASHFS=y
# BR2_TARGET_ROOTFS_TAR is not set
BR2_TARGET_GRUB2=y
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ go 1.22
use (
./devices/generic
./tools/ignite
./tools/dbg
./core
./desktop
./kernel
Expand Down
21 changes: 21 additions & 0 deletions tools/dbg/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"bufio"
"fmt"
"strings"
)

func exec(args []string) error {
_, err := conn.Write([]byte(strings.Join(args, " ") + "\n"))
if err != nil {
return err
}
reader := bufio.NewReader(conn)
response, err := reader.ReadString('\n')
if err != nil {
return err
}
fmt.Print(response)
return nil
}
96 changes: 46 additions & 50 deletions tools/dbg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,75 @@ import (
"bufio"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"strings"
)

var (
addr string
debug bool
addr string
conn net.Conn
commands = map[string]func([]string) error{
"exec": exec,
"push": push,
"pull": pull,
"shell": shell,
}
err error
)

func init() {
flag.StringVar(&addr, "addr", "", "the address to connect to")
flag.BoolVar(&debug, "debug", false, "debug mode")
flag.StringVar(&addr, "addr", "localhost:5555", "Serial Port")
}

func printHelp() {
flag.Usage()
}

func main() {
flag.Parse()
if !debug {
log.SetOutput(io.Discard)

if len(flag.Args()) == 0 {
printHelp()
os.Exit(1)
}

conn, err := net.Dial("tcp", addr)
cmd, ok := commands[flag.Arg(0)]
if !ok {
printHelp()
os.Exit(1)
}

conn, err = net.Dial("tcp", addr)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer conn.Close()

args := flag.Args()

if len(args) == 0 {
return
if err := cmd(flag.Args()[1:]); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

switch args[0] {
case "shell":
go func() {
_, _ = io.Copy(os.Stdout, conn)
_, _ = io.Copy(os.Stderr, conn)
}()
log.Println("Starting shell")
if _, err := conn.Write([]byte(args[0] + "\n")); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

_, err := io.Copy(conn, os.Stdin)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
default:
cmd := strings.Join(flag.Args(), " ")
log.Printf("Sending '%s' to %s\n", cmd, conn.RemoteAddr())
if _, err := conn.Write([]byte(cmd + "\n")); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

reader := bufio.NewReader(conn)
for {
out, err := reader.ReadString('\n')
if err != nil {
if err != io.EOF {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
break
}
fmt.Print(out)
}
func receive() (string, error) {
reader := bufio.NewReader(conn)
resp, err := reader.ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(resp), nil
}

func send(cmd string) (string, error) {
_, err := conn.Write([]byte(cmd + "\n"))
if err != nil {
return "", err
}
resp, err := receive()
if err != nil {
return "", err
}
return resp, nil
}
55 changes: 55 additions & 0 deletions tools/dbg/pull.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
"io"
"os"
"strconv"
"strings"
)

func pull(args []string) error {
if len(args) != 2 {
return fmt.Errorf("usage: dbg pull <source> <destination>")
}
source := args[0]
destination := args[1]

resp, err := send(fmt.Sprintf("download %s", source))
if err != nil {
return err
}

if !strings.HasPrefix(resp, "SIZE") {
return fmt.Errorf("bad response: %s", resp)
}

sizeStr := strings.TrimSpace(strings.Split(resp, " ")[1])
size, err := strconv.Atoi(sizeStr)
if err != nil {
return err
}

_, err = conn.Write([]byte("READY\n"))
if err != nil {
return err
}

file, err := os.Create(destination)
if err != nil {
return err
}
defer file.Close()

buffer := make([]byte, 1024)
received := 0
for received < size {
n, err := conn.Read(buffer)
if err != nil && err != io.EOF {
return err
}
received += n
_, _ = file.Write(buffer[:n])
}
return nil
}
Loading

0 comments on commit eb39003

Please sign in to comment.