3 ###########################################################################
4 # Copyright (C) 2015-2020 IoT.bzh
6 # Author: Stephane Desneux <sdx@iot.bzh>
7 # Sebastien Douheret <sebastien@iot.bzh>
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 ###########################################################################
22 # This script should be invoked by gdb client through a ssh connection.
23 # It relays gdbmi protocol from gdbserver to gdb client
25 # WARNING: nothing should be sent to stdout except gdbserver output
27 # FIXME: add support of --debugger option to support tcf or gdb-remote
38 # setup debug dir (shared with service file)
39 DBGDIR=@afm_platform_rundir@/debug
42 # check application name passed as first arg by gdb
44 [[ -z "$APP" ]] && error "Invalid application name"
46 # redirect to log file
47 exec 2>$DBGDIR/$APP.dbgclt.log
49 # activate DEBUG in environment file sourced in systemd service
50 AFB_WAIT_POINT="start-start"
51 echo "AFB_DEBUG_WAIT=$AFB_WAIT_POINT" >$DBGDIR/$APP.env
53 # remove debug env file on exit
54 trap "rm $DBGDIR/$APP.*" STOP INT QUIT EXIT
56 # ask appfw to start application
57 pid=$(afm-util start $APP)
58 [[ -z "$pid" || ! -e "/proc/$pid" ]] && error "Failed to start application $APP"
59 info "$APP started with pid=$pid"
61 # wait debugging process is stop/waiting at start-start point
62 AFB_FILE=/tmp/afb-debug-$pid
64 info "Waiting for process stopped..."
65 while [[ ! -e "$AFB_FILE" ]]; do
68 [[ "$tmo" == "0" ]] && error "Timeout waiting for process $pid stopped"
71 info "Waiting for process stopped..."
72 AFB_WAIT_FILE=/tmp/afb-debug-$pid
75 while [[ "$res" != "0" ]]; do
78 [[ "$tmo" == "0" ]] && error "Timeout waiting for process $pid stopped"
79 grep $AFB_WAIT_POINT $AFB_WAIT_FILE > /dev/null 2>&1
84 info "Attaching gdbserver to pid $pid ..."
85 gdbserver --attach - $pid
87 # end of debug session
88 info "proxy connection terminated - killing $APP (pid $pid)"
89 afm-util kill $pid >&2