Oct 13

When debuging is stopped while the application on the remote target is not finished I want the remote application and gdbserver to be stopped for several reasons. For one, debugging means finding a problem in an application so execution is apparently not correct and is must be stopped. Secondly, the next time that the executable must be copied it will fail since the executable is still locked.

The Execution | Kill command in KDbg does this, it send a kill command to gdb-cris, this stops execution of the remote program and gdbserver. So perfect, problem is that this is not done automatically when you close KDbg so you always need to do this first before returning to the code. As an option, a script can be launched when Kdbg is stopped that investigates if the application and/or gdbserver is still running. If so, they can be stopped remotely.

Killing gdbserver is done using the kill command, open a terminal and run the command ssh root@FOXBoard “gdbserver :1234 /mnt/flash/bin/HelloWorld/Hello_World”. Next open open a remote shell to the FB and type the command ps. This will list all processes running including their process ID. If gdbserver is running you will see three processes running:

  • 834 root  536 S   sh -c gdbserver :1234 /mnt/flash/bin/HelloWorld/Hello
  • 835 root  512 S   gdbserver :1234 /mnt/flash/bin/HelloWorld/Hello_World
  • 836 root  32 T   /mnt/flash/bin/HelloWorld/Hello_World

The first one is the secure shell used for remotly starting gdbserver, second one is gdbserver and the last is the application being executed. The number in front of it is the process ID of the application, this must be pass to the kill command. If you would kill gdbserver please note that this prevents the application from being executed. After gdbserver is killed this means that the application starts (or continues) executing since it is no longer being kept on hold. In parallel the remote shell sees that gdbserver is finished and wants to close the shell but this is not allowed since a second process (the application) is also using the shell to execute. As a result, process 834 and 835 are killed using the kill 835 command but the prompt is not released in the terminal that was used for the ssh command. Only when the second kill 836 command is executed all applications in the shell are finished and the command prompt is released in the terminal.

This is not the behavior I’m looking for since this means the application continues running out of control after gdbserver is stopped half way the debugging process. In most cases this means the issue has been located and the application must be stopped as well since the application is not working correctly.

The correct order to prevent the application from running without gdbserver is to kill the application first and than kill gdbserver. Giving the command kill 836 will the application although it remains visible when showing all running processes. I guess this is because gdbserver also has a lock to it since giving the command kill 835 will kill gdbserver and the application at the same time.

I’m not sure how but this all should be possible in shell script that is kept universal so that it only needs to be written ones and can be used for all future FB projects. The first step is to create the basic script. Start gedit StopRemote.sh to create a new file, copy below text, save and close.

#
# (c)2008 J.P. van de Kamer
#
echo “Stop gdbserver and target application on the FOXBoard…”
executable=$1
echo $executable

This script will do nothing else than printing the first parameter that is passed to the script when started. In order to make the script executable, enter the command chmod 0755 StopRemote.sh, this will change the attributes of the script so anybody that starts this script in a shell can execute it. Enter the command ./StopRemote Hello_World, this will show below output:

jan@ITX-Development:~/FOXBoard/HelloWorld$ ./StopRemote.sh Hello_World
Starting gdbserver with target application on the FOXBoard…
Hello_World

The kill command requires the process ID of the process. In order to find the PID of the application, the command pidof Hello_World can be given, there is no need to pass a full path to the application, only the executable filename will do. The result is the PID, this must be passed to the kill command. In order to do this, we first assign it to a variable using the command

pid=`ssh root@FOXBoard “pidof $executable”`

Note the ` character, everything between two of them is execute and the output is seen as the value in the equation. In this case the variable pid is assigned the output of the remote command.

It’s time to make a small change, executing the commands remotely takes a long time. So it’s better to make this script a local script that is stored on the FB and executed with a single remote call. As a result, the script so far should be:

echo “Stopping the target application and gdbserver on the FOXBoard…”
executable=$1
pid=`pidof $executable`
echo The PID of $executable is $pid

When an executable name is passed to the script that is not executed the value of PID is empty. Before killing the PID, a check should be done to see if a value is assigned to PID, if not, the kill command can be skipped.

if [ “$pid” != “” ]
then
echo The PID of $executable is $pid
else
echo $executable is not running
fi

Couple of this to note here:

  • The spaces after the [ and before the ] are not optional
  • The $pid must be between “”, this will result in a string compare between the pid and an empty string
  • The then must be on a new line

And now convert it so it actually kill something:

if [ “$pid” != “” ]
then
echo Stopping $executable…
kill $pid
else
echo $executable is not running
fi

Next is to kill gdbserver, a simple copy/paste/modify action will do this. Below the whole script:

#
# (c)2008 J.P. van de Kamer
#
echo “Stopping the target application and gdbserver on the FOXBoard…”

executable=$1
pid=`pidof $executable`

if [ “$pid” != “” ]
then
echo Stopping $executable…
kill $pid
else
echo $executable is not running
fi

pid=`pidof gdbserver`

if [ “$pid” != “” ]
then
echo Stopping gdbserver…
kill $pid
else
echo gdbserver is not running
fi

Copy this script on the FB, in my case in /mnt/flash/bin using the scp StopRemote.sh root@FOXBoard:/mnt/flash/bin command.

Now a single command ssh root@FOXBoard “/mnt/flash/bin/StopRemote.sh Hello_World” will stop the application and gdbserver.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.