#!/bin/sh

# Script to use remote gdb on any process.  (Anything but leos server,
# that is.  It _will_ be of great utility for leos clients.)

# Simple-minded check for clashing of multiple gdbserver sessions.  By
# using multiple IP ports you can use multiple sessions, if you need
# that you can figure out how from this script and do it yourself.
# This is just a convenience script for the common case that tries to
# be somewhat foolproof.

if pidof gdbserver > /dev/null ; then
    echo "gdbserver already in use"
    exit 1
fi

# Numeric or symbolic process identifier?

case $1 in
    [0-9]* ) PID=$1;;
    *      ) PID=`pidof $1`;;
esac

if [ ! -d /proc/$PID ]; then
    echo "No such process $1"
fi

# Gotta open up the firewall too.
iptables -A INPUT -p tcp --dport 2159 -j ACCEPT

# Because leos -s provides network connectivity we must use a serial
# port to debug it; that can be done through the "krn debug" facility.
# For anything else (including leos clients) we can use the network,
# and that's what this script is for.  TCP Port 2159 is customarily
# gdbserver's.

gdbserver localhost:2159 --attach $PID &

# If we _were_ to use serial, this is how:
#   stty -F /dev/ttyS1 ixon ixoff 9600
#   gdbserver /dev/ttyS1 --attach $PID &
