source: trunk/libIGCM/libIGCM_debug/libIGCM_debug.ksh @ 264

Last change on this file since 264 was 264, checked in by mmaipsl, 14 years ago

Correct Stack overflow bug.
Now DEBUG_debug mode should work correctly.

  • Property licence set to
    The following licence information concerns ONLY the libIGCM tools
    ==================================================================

    Copyright © Centre National de la Recherche Scientifique CNRS
    Commissariat à l'Énergie Atomique CEA

    libIGCM : Library for Portable Models Computation of IGCM Group.

    IGCM Group is the french IPSL Global Climate Model Group.

    This library is a set of shell scripts and functions whose purpose is
    the management of the initialization, the launch, the transfer of
    output files, the post-processing and the monitoring of datas produce
    by any numerical program on any plateforme.

    This software is governed by the CeCILL license under French law and
    abiding by the rules of distribution of free software. You can use,
    modify and/ or redistribute the software under the terms of the CeCILL
    license as circulated by CEA, CNRS and INRIA at the following URL
    "http://www.cecill.info".

    As a counterpart to the access to the source code and rights to copy,
    modify and redistribute granted by the license, users are provided only
    with a limited warranty and the software's author, the holder of the
    economic rights, and the successive licensors have only limited
    liability.

    In this respect, the user's attention is drawn to the risks associated
    with loading, using, modifying and/or developing or reproducing the
    software by the user in light of its specific status of free software,
    that may mean that it is complicated to manipulate, and that also
    therefore means that it is reserved for developers and experienced
    professionals having in-depth computer knowledge. Users are therefore
    encouraged to load and test the software's suitability as regards their
    requirements in conditions enabling the security of their systems and/or
    data to be ensured and, more generally, to use and operate it in the
    same conditions as regards security.

    The fact that you are presently reading this means that you have had
    knowledge of the CeCILL license and that you accept its terms.
  • Property svn:keywords set to Date Author Revision
File size: 9.0 KB
Line 
1#!/bin/ksh
2
3#**************************************************************
4# Author: Patrick Brockmann, Martial Mancip
5# Contact: Patrick.Brockmann@cea.fr Martial.Mancip@ipsl.jussieu.fr
6# $Date$
7# $Author$
8# $Revision$
9# IPSL (2006)
10#  This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC
11# History:
12# Modification:
13#
14#**************************************************************
15
16#==================================================
17# The documentation of this file can be automatically generated
18# if you use the prefix #D- for comments to be extracted.
19# Extract with command: cat lib* | grep "^#D-" | cut -c "4-"
20#==================================================
21
22#==================================================
23# Add high level verbosity
24typeset -i Verbosity=${Verbosity:=3}
25
26#==================================================
27# DEBUG_debug
28# Add low level verbosity
29typeset DEBUG_debug=${DEBUG_debug:=false}
30
31if ( $DEBUG_debug ) ; then
32    if [ -f stack ] ;
33    then
34        echo "Stack of an libIGCM job :" >> stack
35    else
36        echo "Stack of an libIGCM job :" >  stack
37    fi
38fi
39
40#==================================================
41# NULL_STR
42# Default null string
43typeset -r NULL_STR="_0_" 
44
45#==================================================
46# libIGCM_CurrentTag
47# Current libIGCM tag, check compatibilty with *.card
48typeset -r libIGCM_CurrentTag="1.0" 
49
50#==================================================
51# Exit Flag (internal debug)
52# When true, end the master loop AFTER SAVES FILES
53ExitFlag=false
54
55#==================================================
56# Declare a stack of functions calls
57
58# insert last argument of the Stack
59#set -A IGCM_debug_Stack ${NULL_STR}
60#set -A IGCM_debug_StackArgs ${NULL_STR}
61unset IGCM_debug_Stack
62unset IGCM_debug_StackArgs
63IGCM_debug_Stack[0]=${NULL_STR}
64IGCM_debug_StackArgs[0]=${NULL_STR}
65IGCM_debug_LenStack=0
66
67#D-#==================================================================
68#D-function IGCM_debug_CallStack
69#D-* Purpose: Echo the Stack
70#D-
71function IGCM_debug_CallStack {
72    echo
73    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!"
74    echo "!! IGCM_debug_CallStack !!"
75    echo "!------------------------!"
76    echo
77    if ( $DEBUG_debug ) ; then
78       # La pile d'appels est affichée de la plus vieille à la plus récente
79       # (c'est donc l'inverse de la norme d'affichage).
80        typeset i decal
81        (( i=0 ))
82        until [ $i -eq ${IGCM_debug_LenStack} ]; do
83            decal=0
84            until [ $decal -eq ${i} ]; do
85                printf -- ' '
86                let $(( decal=decal+1 ))
87            done
88            echo "$i - ${IGCM_debug_Stack[$(( $IGCM_debug_LenStack-$i-1 ))]}"\
89                "(${IGCM_debug_StackArgs[$(( $IGCM_debug_LenStack-$i-1 ))]})"
90            let $((i=i+1))
91        done
92        echo "!------------------------!"
93    fi
94}
95
96#D-#==================================================================
97#D-function IGCM_debug_PushStack
98#D-* Purpose: Push a function name in the stack
99#D-
100function IGCM_debug_PushStack {
101    if ( $DEBUG_debug ) ; then
102       typeset decal
103       echo >> stack
104       decal=0
105       while [ $decal -lt ${IGCM_debug_LenStack} ]; do
106#          printf -- ' ' >> stack
107           printf ' ' >> stack
108           let $(( decal=decal+1 ))
109       done
110       echo "> ${IGCM_debug_LenStack} : ${@}" >> stack
111
112       # We add function call name on beginning of the stack
113       set +A IGCM_debug_Stack -- ${1} ${IGCM_debug_Stack[*]}
114
115       # We include the "null" Args in the beginning of the StackArgs
116       set +A IGCM_debug_StackArgs ${NULL_STR} ${IGCM_debug_StackArgs[*]} 
117       # Then, we shift StackArgs tabular
118       if [ $# -gt 1 ]; then
119           shift;
120           IGCM_debug_StackArgs[0]=$(echo ${@} | sed -e "s/\ /,/g") >&2
121       fi
122       (( IGCM_debug_LenStack = IGCM_debug_LenStack + 1 ))
123
124       # If you want to print CallStack each time :
125       # IGCM_debug_CallStack
126    fi
127}
128
129#D-#==================================================================
130#D-function IGCM_debug_PopStack
131#D-* Purpose: Pop a function name in the stack
132#D-
133function IGCM_debug_PopStack {
134    if ( $DEBUG_debug ) ; then
135        if ( ${ExitFlag} ) ; then
136            echo '!!! ExitFlag has been activated !!!' >> stack
137        fi
138        if [ "${IGCM_debug_Stack[0]}" = "${1}" ]; then
139            let $(( IGCM_debug_LenStack = IGCM_debug_LenStack - 1 ))
140
141            IGCM_debug_Stack[0]="" 
142            IGCM_debug_StackArgs[0]=""
143           
144            set +A tmpStack -- ${IGCM_debug_Stack[*]}
145            unset IGCM_debug_Stack
146            set +A IGCM_debug_Stack -- ${tmpStack[*]} 
147
148            set +A tmpStack -- ${IGCM_debug_StackArgs[*]}
149            unset IGCM_debug_StackArgs
150            set +A IGCM_debug_StackArgs -- ${tmpStack[*]}
151            unset tmpStack
152        else
153            echo 'IGCM_debug_Exit : stack is corrupted ! LenStack =' ${IGCM_debug_LenStack}
154            IGCM_debug_Exit $@
155        fi
156        typeset decal=0
157        while [ $decal -lt ${IGCM_debug_LenStack} ]; do
158          # printf -- ' ' >> stack
159          printf ' ' >> stack
160          (( decal=decal+1 ))
161        done
162      echo "< ${IGCM_debug_LenStack} : ${@}" >> stack
163
164      if [ ${IGCM_debug_LenStack} = 0 ]; then
165          # Reset array only when necessary
166          #echo
167          #IGCM_debug_Print 3 "Clean stack array"
168          #echo
169          #set -A IGCM_debug_Stack ${NULL_STR}
170          #set -A IGCM_debug_StackArgs ${NULL_STR}
171          unset IGCM_debug_Stack
172          unset IGCM_debug_StackArgs
173          IGCM_debug_Stack[0]=${NULL_STR}
174          IGCM_debug_StackArgs[0]=${NULL_STR}
175      fi
176
177    fi
178
179    #IGCM_debug_CallStack
180}
181
182#D-#==================================================================
183#D-function IGCM_debug_Exit
184#D-* Purpose: Print Call Stack and set ExitFlag to true
185#D-
186function IGCM_debug_Exit {
187    IGCM_debug_PushStack "IGCM_debug_Exit"
188    echo "IGCM_debug_Exit : " "${@}"
189    IGCM_debug_CallStack
190    ExitFlag=true
191    IGCM_debug_PopStack "IGCM_debug_Exit"
192}
193
194#D-#==================================================
195#D-function IGCM_debug_Verif_Exit
196#D-* Purpose: exit with number 1 if ExitFlag is true
197#D-
198function IGCM_debug_Verif_Exit {
199    if ( ${ExitFlag} ) ; then
200        IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration PeriodState "Fatal"
201        echo "IGCM_debug_Verif_Exit : Something wrong append."
202        echo "                        EXIT THE JOB."
203        echo
204        if ( $DEBUG_debug ) ; then
205            IGCM_sys_Cp stack ${SUBMIT_DIR}/stack_error
206            echo "Your files on ${R_OUT} :"
207            IGCM_sys_Tree ${R_SAVE}
208            echo
209        fi
210        date
211        exit 1
212    fi
213}
214
215#D-#==================================================
216#D-function IGCM_debug_Verif_Exit_Post
217#D-* Purpose: exit with number 1 if ExitFlag is true for Post-treatment
218#D-
219function IGCM_debug_Verif_Exit_Post {
220    if ( ${ExitFlag} ) ; then
221        echo "IGCM_debug_Verif_Exit_Post : Something wrong append."
222        #echo "                        EXIT THE JOB."
223        echo "         BUT WE DO NOT EXIT THE JOB."
224        echo
225        date
226        #exit 1
227    fi
228}
229
230#D-#==================================================================
231#D-function IGCM_debug_Print
232#D-* Purpose: Print arguments according to a level of verbosity.
233#D-
234function IGCM_debug_Print
235{
236    typeset level=$1
237    shift
238
239    if [ ${level} -le ${Verbosity} ] ; then
240        typeset i
241        case "${level}" in
242            1) for i in "$@" ; do
243                    echo "--Debug1-->" ${i}
244              done ;;
245            2) for i in "$@" ; do
246                    echo "--------Debug2-->" ${i}
247              done ;;
248            3) for i in "$@" ; do
249                    echo "--------------Debug3-->" ${i}
250              done ;;
251        esac
252    fi
253}
254
255#D-#==================================================================
256#D-function IGCM_debug_PrintVariables
257#D-* Purpose: Print arguments when match a pattern
258#D-           according to a level of verbosity.
259function IGCM_debug_PrintVariables
260{
261    typeset level=$1
262    shift
263
264    list=$( set | grep $1 | sed -e "s/'//g" )
265
266    if [ "X${list}" != X ]  ; then
267        IGCM_debug_Print ${level} ${list}
268    fi
269}
270
271#D-#==================================================================
272#D-function IGCM_debug_Check
273#D- * Purpose: Check the present file by comparison with a reference file
274function IGCM_debug_Check
275{
276    #---------------------
277    if [ ! -n "${libIGCM}" ] ; then
278        echo "Check libIGCM_debug ..........................................[ FAILED ]"
279        echo "--Error--> libIGCM variable is not defined"
280        exit 2
281    fi
282
283    #---------------------
284    if [ ! -n "${Verbosity}" ] ; then
285        echo "Check libIGCM_debug ..........................................[ FAILED ]"
286        echo "--Error--> Verbosity variable is not defined"
287        exit 3
288    fi
289
290    #---------------------
291    ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ksh > IGCM_debug_Test.ref.failed 2>&1
292    sleep 2
293
294    if diff IGCM_debug_Test.ref.failed ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ref > /dev/null 2>&1 ; then
295        echo "Check libIGCM_debug ..............................................[ OK ]"
296        rm -f IGCM_debug_Test.ref.failed
297    else
298        echo "Check libIGCM_debug ..........................................[ FAILED ]"
299        echo "--Error--> Execution of ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ksh"
300        echo "           has produced the file IGCM_debug_Test.ref.failed"
301        echo "           Please analyse differences with the reference file by typing:"
302        echo "           diff IGCM_debug_Test.ref.failed ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ref"
303        echo "           Report errors to the author: Patrick.Brockmann@cea.fr"
304        exit 4
305    fi
306    #---------------------
307}
Note: See TracBrowser for help on using the repository browser.