aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/capdebuginfo
blob: 72b96b2f7c7a776c327266117610a34d94a55df9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/bash

# ---------------------------------------------------------------------------------------------------------------------
# SPDX-License-Identifier: GPL-2.0
# ---------------------------------------------------------------------------------------------------------------------

###############################################################################
# Name: Capture/Debug Information Tool
# Description:
#  Script to capture debug info from a MEL user's host
#
#
# Copyright 2022 Siemens
#
# This file is licensed under the terms of the GNU General Public License
# version 2.  This program  is licensed "as is" without any warranty of any
# kind, whether express or implied.

# Variables
unset BUILDERR   # Path and filename of the MEL build error log file
unset MELDIR     # Root path of the MEL installation directory

HOMEDIR=${HOME}
HOSTINFOFILE="hostinfo.txt"    # File containing info captured from user's host
OUTPUTDIRNAME="capdebuginfo"   # Directory containing all info captured by script
SCRIPT_NAME="$(basename ${BASH_SOURCE})"
MGLS_LICENSE="MGLS_LICENSE_FILE"
LM_LICENSE="LM_LICENSE_FILE"


usage () {
   cat >&2 <<END

Description:
   Capture debug info from a MEL user's host such as host environment and project log files.
   The Script runs only if environment of the shell is already set using setup-environment script.

Modes of operation:
   With input arguments: ${SCRIPT_NAME} -e <PN>
     
   Without input arguments: ${SCRIPT_NAME}
   
Usage: ${SCRIPT_NAME} -e <PN>
   or  ${SCRIPT_NAME}
   
   -e <PN>   Specify the name of package causing build failure
   -h        Show this usage information

END
}


abspath () {
    readlink -e "${1}"
}

# Process input arguments to the script
#   Input:  $@
process_cmdline_args () {
   while getopts ":e:h" opt; do
      case "${opt}" in
         e)
            # Handle getops with invalid input
            if [[ "${OPTARG}" == -* ]] ; then
               echo "Invalid input for option -"${opt}""
               shift $((OPTIND-1))
               break
            else
               BUILDERR=$(find "${BUILDDIR}/tmp/work/" -maxdepth 2 -type d -name "${OPTARG}")
            fi
            ;;
         :)
            echo "Option -"${OPTARG}" requires an argument." >&2
            ;;
         \?)
            echo "Invalid option: -"${OPTARG}"" >&2
            ;;
         h)
            usage
            exit 1
            ;;
      esac
   done
}


# Collect information from host 
#    Input: $1=Output directory name 
#           $2=Filename for host info collection
get_hostinfo () {
   local __output="${1}/host/${2}"
   
   echo -en "\\nHost Distro Info:\\n" >> "${__output}"
   lsb_release -a >> "${__output}" 2> /dev/null
   echo -en "\\nHost System Info:\\n" >> "${__output}"
   uname -a >> "${__output}"
   echo -en "\\nHost env Info:\\n" >> "${__output}"
   printenv | sort >> "${__output}"
   echo -en "\\nHost disk space usage:\\n" >> "${__output}"
   df -h >> "${__output}"
   echo -en "\\nHost memory usage:\\n" >> "${__output}"
   free -ht >> "${__output}"
   echo -en "\\n" >> "${__output}"
}


# Collect license info from host 
#    Input: $1=Output directory name 
#           $2=Filename for host info collection
#           $3=License info variable name
get_hostlicenseinfo () {
   local __outputdir="${1}"
   local __output=""${1}"/host/"${2}""
   local __licensename="${3}"
   local __license=$(printenv "${3}")

   echo -en "\\n\"${__licensename}\":\\n" >> "${__output}"
   # Check if license variable is set in shell environment
   if [ -n "${__license}" ]; then
      echo -en "\"${__license}\"\\n" >> "${__output}"
      # Process license variable string
      for i in $(echo "${__license//:/$'\\n'}"); do
         # Check if entry is a license file and exists
         if [ -f "${i}" ]; then
            # Make a copy of the license file
            cp "${i}" "${__outputdir}"/host/.
         # Check if entry is a license server
         elif [[ "${i}" == *"@"* ]]; then
            echo -en "\\n\"${i}\" is a license server\\n" >> "${__output}"
         # Entry is invalid
         else
            echo -en "\\n\"${i}\" does not seem to be a valid entry\\n" >> "${__output}"
         fi
      done
   else
      echo -en "\\n\"${__licensename}\" is not set!\\n" >> "${__output}"
   fi
}


# Collect prerequisite packages/libraries from host
#    Input: $1=Output directory name 
#           $2=Filename for host info collection

get_hostprereqsinfo () {
   local __outputdir="${1}"
   local __output="${1}/host/${2}"
   local __hostos_distid=`lsb_release -i -s`
   local __hostos_distvers=`lsb_release -r -s`
   local __pkgs
   
   echo -en "\nHost OS is ${__hostos_distid} ${__hostos_distvers}:\n" >> ${__output}
   case "${__hostos_distid}" in
      Ubuntu)
         __pkgs="bmap-tools bzip2 chrpath cvs diffstat gawk gcc git-core gzip g++ \
                 libgl1-mesa-dev libglu1-mesa-dev libncurses5-dev libsdl1.2-dev \
                 lsb-release make patch python python3 subversion tar texinfo \
                 texi2html ubuntu-minimal ubuntu-standard zip"
         dpkg-query -W -f='${binary:Package} ${Version} ${db:Status-Status}\n' ${__pkgs} >> ${__output}
         ;;      
      CentOS|RedHat*)
         __pkgs="bzip2 chrpath cvs diffstat gawk gcc gcc-c++ git glibc-devel \ 
                 glibc-static gzip make ncurses-devel patch perl-Thread-Queue \
                 python python34 python-virtualenv redhat-lsb SDL-devel sqlite \
                 subversion tar tetex texinfo texi2html wget"
         rpm -q ${__pkgs} >> ${__output}
         ;;
   esac
}

# Collect MEL installation info from host 
#    Input: $1=Output directory name 
#           $2=Filename for host info collection
get_melinstallinfo () {
   local __outputdir="${1}"
   local __output="${1}/host/${2}"
   
   if [ -d "${HOMEDIR}"/.mentor ]; then
      cp -a "${HOMEDIR}"/.mentor/.registry "${__outputdir}"/host/mentor-registry
   else
      echo -en "\\nNo MEL install info found!\\n" >> ${__output}
   fi
}


# Collect MEL project build and error logs 
#    Input: $1=Output directory name 
#           $2=BUILDERR
#           $3=BUILDDIR
get_melbuildinfo () {
   local __outputdir="${1}"
   local __builderr="${2}"
   local __builddir="${3}"
   
   local __arch="$(echo "${__builderr#/*/work/}" | cut -d"/" -f1)"
   local __pkgname="$(echo "${__builderr#/*/work/}" | cut -d"/" -f2)"
   local __pkgdir="${__builddir}"/tmp/work/"${__arch}"/"${__pkgname}"

   cp -a "${__builddir}"/buildhistory/images/ "${__outputdir}"/mel/.
   cp -a "${__builddir}"/tmp/log/ "${__outputdir}"/mel/.
   cp -a "${__builddir}"/conf/ "${__outputdir}"/mel/.

   mkdir "${__outputdir}"/mel/"${__arch}"
   cp -a "${__pkgdir}" "${__outputdir}"/mel/"${__arch}"/.
}


########################################################################################################
#
# Start execution of script
#

# Check if the build environment is set or not.
if [ ! -d "${BUILDDIR}" ]; then
    echo "Please setup the build environment first, and run the script again."
    exit 1
else
	# Check 1st cmdline argument
	if [[ "${1}" == -* ]]; then
		# Process script input arguments 
		process_cmdline_args "${@}"
		
		if [ ! -d "${BUILDERR}" ]; then
		   echo -en "\\nERROR:  Input argument was invalid!!!\\n\\n"
		fi
	elif [ -z "${@}" ]; then
		echo -en "\\nWARNING:  No input argument was given on command line.\\n\\n"
	fi

	# Check for valid BUILDERR
	while [ ! -d "${BUILDERR}" ]; do
		echo "Please enter package name in which you are having error (option -e):"
		read IN1
        
		BUILDERR=$(find "${BUILDDIR}/tmp/work/" -maxdepth 2 -type d -name "${IN1}")
		 
		if [ ! -d "${BUILDERR}" ]; then
		   echo -en "\\nERROR (option -e):  Invalid package name!!!\\n\\n"
		fi
	done

	for i in $(echo "$PATH" | tr ":" "\\n")
	do
		 a="$(echo "$i" | grep "/bitbake/bin")"
		 if [ -n "$a" ]
		 then
		   break
		 fi
	done

	MELDIR="${a%%/bitbake/bin}"

	# Clean up artifacts from previous run of the script
	if [ -d "${OUTPUTDIRNAME}" ]; then
		rm -rf "${OUTPUTDIRNAME}"
	fi

	if [ -a "${OUTPUTDIRNAME}".tar.bz2 ]; then
		rm "${OUTPUTDIRNAME}".tar.bz2
	fi

	# Create directories for saving script outputs
	mkdir -p "${OUTPUTDIRNAME}"/host "${OUTPUTDIRNAME}"/mel

	# Collect host information
	get_hostinfo "${OUTPUTDIRNAME}" "${HOSTINFOFILE}"
	get_hostlicenseinfo "${OUTPUTDIRNAME}" "${HOSTINFOFILE}" "${MGLS_LICENSE}"
	get_hostlicenseinfo "${OUTPUTDIRNAME}" "${HOSTINFOFILE}" "${LM_LICENSE}"
	get_hostprereqsinfo "${OUTPUTDIRNAME}" "${HOSTINFOFILE}"
	get_melinstallinfo "${OUTPUTDIRNAME}" "${HOSTINFOFILE}"
	get_melbuildinfo "${OUTPUTDIRNAME}" "${BUILDERR}" "${BUILDDIR}"

	# Create tarball with info collected in output directory
	echo "Creating \"${OUTPUTDIRNAME}\".tar.bz2..."
	tar cjpf "${OUTPUTDIRNAME}".tar.bz2 "${OUTPUTDIRNAME}" 2>> "${OUTPUTDIRNAME}"/host/"${HOSTINFOFILE}"

	# Need to check exit status of tar and handle appropriately
	if [[ ${?} -ne 0 ]]; then
		echo -en "\\ntar errno: ${?}\\n" >> "${OUTPUTDIRNAME}"/host/"${HOSTINFOFILE}"
		cp "${OUTPUTDIRNAME}"/host/"${HOSTINFOFILE}" .
		echo "An error occurred during tar."
		echo "Please attach "${HOSTINFOFILE}" to your Support Request ticket."
		exit 1
	else
		# Remove output directory after creating tarball archive file
		if [ -d "${OUTPUTDIRNAME}" ]; then
		   rm -rf "${OUTPUTDIRNAME}"
		fi
		echo "Done!!!"
		echo -en "\\nPlease attach "${OUTPUTDIRNAME}".tar.bz2 to your Support Request ticket.\\n\\n"
	fi
fi