Update date in copyrights
[src/app-framework-main.git] / scripts / afm-debug.in
1 #!/bin/bash
2
3 ###########################################################################
4 # Copyright (C) 2017-2019 IoT.bzh
5 #
6 # Author: Stephane Desneux <sdx@iot.bzh>
7 #         Sebastien Douheret <sebastien@iot.bzh>
8 #
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
12 #
13 #     http://www.apache.org/licenses/LICENSE-2.0
14 #
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 ###########################################################################
21
22 # This script should be invoked by gdb client through a ssh connection.
23 # It relays gdbmi protocol from gdbserver to gdb client
24 #
25 # WARNING: nothing should be sent to stdout except gdbserver output
26
27 # FIXME: add support of --debugger option to support tcf or gdb-remote
28
29
30 function error() {
31         echo "ERR: $@" >&2
32         exit 1
33 }
34 function info() {
35         echo "INF: $@" >&2
36 }
37
38 # setup debug dir (shared with service file)
39 DBGDIR=@afm_platform_rundir@/debug
40 mkdir -p $DBGDIR
41
42 # check application name passed as first arg by gdb
43 APP=$1
44 [[ -z "$APP" ]] && error "Invalid application name"
45
46 # redirect to log file
47 exec 2>$DBGDIR/$APP.dbgclt.log
48
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
52
53 # remove debug env file on exit
54 trap "rm $DBGDIR/$APP.*" STOP INT QUIT EXIT
55
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"
60
61 # wait debugging process is stop/waiting at start-start point
62 AFB_FILE=/tmp/afb-debug-$pid
63 tmo=100
64 info "Waiting for process stopped..."
65 while [[ ! -e "$AFB_FILE" ]]; do
66     sleep 0.1
67     tmo=$(expr $tmo - 1)
68     [[ "$tmo" == "0" ]] && error "Timeout waiting for process $pid stopped"
69 done
70
71 info "Waiting for process stopped..."
72 AFB_WAIT_FILE=/tmp/afb-debug-$pid
73 tmo=100
74 res=1
75 while [[ "$res" != "0" ]]; do
76     sleep 0.1
77     tmo=$(expr $tmo - 1)
78     [[ "$tmo" == "0" ]] && error "Timeout waiting for process $pid stopped"
79     grep $AFB_WAIT_POINT $AFB_WAIT_FILE > /dev/null 2>&1
80     res=$?
81 done
82
83 # debugging
84 info "Attaching gdbserver to pid $pid ..."
85 gdbserver --attach - $pid
86
87 # end of debug session
88 info "proxy connection terminated - killing $APP (pid $pid)"
89 afm-util kill $pid >&2
90 info "$APP killed"