From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:57087) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RYmmF-0004qV-M8 for qemu-devel@nongnu.org; Thu, 08 Dec 2011 17:49:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RYmmD-0008He-8b for qemu-devel@nongnu.org; Thu, 08 Dec 2011 17:49:19 -0500 Received: from gw.ac.upc.edu ([147.83.30.3]:37248) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RYmmC-0008HY-SZ for qemu-devel@nongnu.org; Thu, 08 Dec 2011 17:49:17 -0500 Received: from localhost (unknown [84.88.53.92]) by gw.ac.upc.edu (Postfix) with ESMTP id D6DB56B02C9 for ; Thu, 8 Dec 2011 23:49:15 +0100 (CET) From: =?utf-8?b?TGx1w61z?= Vilanova Date: Thu, 08 Dec 2011 23:49:08 +0100 Message-ID: <20111208224908.21668.42809.stgit@ginnungagap.bsc.es> In-Reply-To: <20111208224750.21668.26153.stgit@ginnungagap.bsc.es> References: <20111208224750.21668.26153.stgit@ginnungagap.bsc.es> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 05/10] trace: [tracetool] Common functions to manage event arguments List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org This adds: * zip_lists: Pair elements from two lists. * get_argtypes: Get the list of types of the arguments; can optionally tr= ansform the types in the process. The function 'get_args' is modified to use a combination of 'get_argnames= ', 'get_argtypes', 'zip_lists' and, optionally, use the type transformation capabilities of 'get_argtypes'. Signed-off-by: Llu=C3=ADs Vilanova --- scripts/tracetool | 117 +++++++++++++++++++++++++++++++++++++++++++++++= ++++++ 1 files changed, 116 insertions(+), 1 deletions(-) diff --git a/scripts/tracetool b/scripts/tracetool index 4c9951d..f2b6680 100755 --- a/scripts/tracetool +++ b/scripts/tracetool @@ -74,15 +74,130 @@ has_property() return 1 } =20 +# Convenience function to pair elements from two comma-separated lists o= f equal +# size. +# 1: first list +# 2: second list +# 3: printf format for each pair of elements (optional, default: "%s %s,= ") +zip_lists() +{ + [ -n "$1" -a -n "$2" ] || return + + local format + format=3D$3 + [ -n "$format" ] || format=3D"%s %s, " + + local i elem accum + i=3D1 + accum=3D"" + for elem in $1; do + if [ "$elem" =3D "${elem%,}" ]; then + accum=3D"$accum $elem" + else + accum=3D"$accum ${elem%,}" + eval __elem_$i=3D\"$accum\" + i=3D$(($i + 1)) + accum=3D"" + fi + done + eval __elem_$i=3D\"$accum\" + + local tmp res + accum=3D"" + res=3D"" + i=3D1 + for elem in $2; do + if [ "$elem" =3D "${elem%,}" ]; then + accum=3D"$accum $elem" + else + accum=3D"$accum ${elem%,}" + eval tmp=3D\$__elem_$i + tmp=3D$(printf "$format" "$tmp" "$accum") + res=3D"$res$tmp" + i=3D$(($i + 1)) + accum=3D"" + fi + done + eval tmp=3D\$__elem_$i + tmp=3D$(printf "$format" "$tmp" "$accum") + res=3D"$res$tmp" + res=3D"${res%, }" + echo $res +} + # Get the argument list of a trace event, including types and names +# 1: event name +# 2: passed as the second argument to 'get_argtypes' (optional) get_args() { local args args=3D${1#*\(} args=3D${args%%\)*} - echo "$args" + + if [ -z "$2" -o "$args" =3D "void" ]; then + echo "$args" + else + local argtypes argnames res + argtypes=3D$(get_argtypes "$1" $2) + argnames=3D$(get_argnames "$1" ",") + res=3D$(zip_lists "$argtypes" "$argnames") + echo "${res#, }" + fi } =20 +# Get the argument type list of a trace event +# 1: event name +# 2: function to translate each of the types (optional, default: nil_typ= e) +get_argtypes() +{ + local translate field type res + + translate=3D$2 + [ -n "$translate" ] || translate=3Dnil_type + + res=3D"" + type=3D"" + for field in $(get_args "$1"); do + if [ "${field%,}" !=3D "${field}" ]; then + # it's a name, but can have a star in it + [ "${field#\*}" =3D "$field" ] || type=3D"${type}*" + # trim spaces + type=3D"${type## }" + type=3D"${type%% }" + # translate + type=3D$($translate "$type") + res=3D"$res$type, " + type=3D"" + else + # types can have spaces + type=3D"$type $field" + fi + done + + # remove the accumulated name + field=3D"${type##* }" + type=3D"${type% *}" + + # trim spaces + type=3D"${type## }" + type=3D"${type%% }" + + # it's a name, but can have a star in it + [ "${field#\*}" =3D "$field" ] || type=3D"${type}*" + # translate + type=3D$($translate "$type") + res=3D"$res$type" + + echo $res +} + +# No-op type translator for get_argtypes +nil_type() +{ + echo "$1" +} + + # Get the argument name list of a trace event get_argnames() {