From nobody Sat Feb 7 21:15:42 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1570621404; cv=none; d=zoho.com; s=zohoarc; b=JT8UcODUP3AVjix1E8hytqIPEYbcupBRskTkulcxZmTowXsticooEGLoHwhOQ59h+1O/u0HPIBqInBL56J74kp0YPaEKuXKKIJK5rdmdQbNa3aEsw1oSZegjyetGuPSrNpJx8BkD6VIY6BASkPg176KpPulqzxZtpqe/IZnhnZI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1570621404; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=G9VMCpMVbOR+3joap0tOSs9hOw/Ye6xtEvEYuBXuQSk=; b=lMZLVLcet7e0ticbVSivsxhg/hq/Wv8xAdi/FuKZlR5C4UMcH1wfnCl5gsuEafutGMnlJtKzdAETYvhwyno4NPE5iD0eos5w+AYFt3Du8cnzhxRcDYxQ6K1og1PdjtV4yR1/8pOSzq0emSu15DJ71aWhX20GoTego5M9RGHfzlQ= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1570621404972852.6496475917065; Wed, 9 Oct 2019 04:43:24 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0C8BE81F11; Wed, 9 Oct 2019 11:43:23 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9EE5C60629; Wed, 9 Oct 2019 11:43:22 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 4C3D74EDA7; Wed, 9 Oct 2019 11:43:22 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x99BhHZr030061 for ; Wed, 9 Oct 2019 07:43:18 -0400 Received: by smtp.corp.redhat.com (Postfix) id F0F4C60466; Wed, 9 Oct 2019 11:43:17 +0000 (UTC) Received: from catbus.gsslab.fab.redhat.com (dhcp-32.gsslab.fab.redhat.com [10.33.9.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id 64925601B3; Wed, 9 Oct 2019 11:43:17 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Wed, 9 Oct 2019 12:37:27 +0100 Message-Id: <20191009113741.5002-13-berrange@redhat.com> In-Reply-To: <20191009113741.5002-1-berrange@redhat.com> References: <20191009113741.5002-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 12/26] src: rewrite systemtap function generator in Python X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 09 Oct 2019 11:43:23 +0000 (UTC) As part of an goal to eliminate Perl from libvirt build tools, rewrite the gensystemtap.pl tool in Python. This was a straight conversion, manually going line-by-line to change the syntax from Perl to Python. Thus the overall structure of the file and approach is the same. Signed-off-by: Daniel P. Berrang=C3=A9 --- Makefile.am | 1 + cfg.mk | 4 +- scripts/gensystemtap.py | 184 ++++++++++++++++++++++++++++++++++++++ src/Makefile.am | 5 +- src/rpc/Makefile.inc.am | 1 - src/rpc/gensystemtap.pl | 193 ---------------------------------------- 6 files changed, 189 insertions(+), 199 deletions(-) create mode 100755 scripts/gensystemtap.py delete mode 100755 src/rpc/gensystemtap.pl diff --git a/Makefile.am b/Makefile.am index 9dcaaecb6b..345ee2a5aa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -51,6 +51,7 @@ EXTRA_DIST =3D \ scripts/check-symfile.py \ scripts/check-symsorting.py \ scripts/dtrace2systemtap.py \ + scripts/gensystemtap.py \ scripts/header-ifdef.py \ scripts/minimize-po.py \ scripts/mock-noinline.py \ diff --git a/cfg.mk b/cfg.mk index 39b2faa527..39b97c54f6 100644 --- a/cfg.mk +++ b/cfg.mk @@ -407,6 +407,7 @@ sc_prohibit_risky_id_promotion: # since gnulib has more guarantees for snprintf portability sc_prohibit_sprintf: @prohibit=3D'\<[s]printf\>' \ + in_vc_files=3D'\.[ch]$$' \ halt=3D'use snprintf, not sprintf' \ $(_sc_search_regexp) =20 @@ -1272,9 +1273,6 @@ exclude_file_name_regexp--sc_prohibit_readlink =3D \ =20 exclude_file_name_regexp--sc_prohibit_setuid =3D ^src/util/virutil\.c|tool= s/virt-login-shell\.c$$ =20 -exclude_file_name_regexp--sc_prohibit_sprintf =3D \ - ^(cfg\.mk|docs/hacking\.html\.in|.*\.stp|.*\.pl)$$ - exclude_file_name_regexp--sc_prohibit_strncpy =3D ^src/util/virstring\.c$$ =20 exclude_file_name_regexp--sc_prohibit_strtol =3D ^examples/.*$$ diff --git a/scripts/gensystemtap.py b/scripts/gensystemtap.py new file mode 100755 index 0000000000..c4bc1620d0 --- /dev/null +++ b/scripts/gensystemtap.py @@ -0,0 +1,184 @@ +#!/usr/bin/env python +# +# Copyright (C) 2011-2019 Red Hat, Inc. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see +# . +# +# Generate a set of systemtap functions for translating various +# RPC enum values into strings +# +# python gensystemtap.py */*.x > libvirt_functions.stp +# + +from __future__ import print_function + +import re +import sys + +funcs =3D {} + +types =3D {} +status =3D {} +auth =3D {} + + +def load_file(fh): + instatus =3D False + intype =3D False + inauth =3D False + + for line in fh: + if re.search(r'''enum\s+virNetMessageType''', line): + intype =3D True + elif re.search(r'''enum\s+virNetMessageStatus''', line): + instatus =3D True + elif re.search(r'''enum remote_auth_type''', line): + inauth =3D True + elif line.find("}") !=3D -1: + intype =3D False + instatus =3D False + inauth =3D False + elif instatus: + m =3D re.search(r'''^\s+VIR_NET_(\w+)\s*=3D\s*(\d+),?$''', lin= e) + if m is not None: + status[m.group(2)] =3D m.group(1).lower() + elif intype: + m =3D re.search(r'''^\s+VIR_NET_(\w+)\s*=3D\s*(\d+),?$''', lin= e) + if m is not None: + types[m.group(2)] =3D m.group(1).lower() + elif inauth: + m =3D re.search(r'''^\s+REMOTE_AUTH_(\w+)\s*=3D\s*(\d+),?$''',= line) + if m is not None: + auth[m.group(2)] =3D m.group(1).lower() + else: + m =3D re.search(r'''(?:VIR_)?(\w+?)(?:_PROTOCOL)?''' + + r'''_PROGRAM\s*=3D\s*0x([a-fA-F0-9]+)\s*;''', li= ne) + if m is not None: + funcs[m.group(1).lower()] =3D { + "id": int(m.group(2), 16), + "version": None, + "progs": [] + } + continue + + m =3D re.search(r'''(?:VIR_)?(\w+?)(?:_PROTOCOL)?_''' + + r'''(?:PROGRAM|PROTOCOL)_VERSION\s*=3D\s*(\d+)\s= *;''', + line) + if m is not None: + funcs[m.group(1).lower()]["version"] =3D m.group(2) + continue + + m =3D re.search(r'''(?:VIR_)?(\w+?)(?:_PROTOCOL)?''' + + r'''_PROC_(.*?)\s+=3D\s+(\d+)''', line) + if m is not None: + funcs[m.group(1).lower()]["progs"].insert( + int(m.group(3)), m.group(2).lower()) + + +for file in sys.argv[1:]: + with open(file, "r") as fh: + load_file(fh) + + +def genfunc(name, varname, types): + print("function %s(%s, verbose)" % (name, varname)) + print("{") + + first =3D True + for typename in sorted(types.keys()): + cond =3D "} else if" + if first: + cond =3D "if" + first =3D False + + print(" %s (%s =3D=3D %s) {" % (cond, varname, typename)) + print(" %sstr =3D \"%s\"" % (varname, types[typename])) + + print(" } else {") + print(" %sstr =3D \"unknown\";" % varname) + print(" verbose =3D 1;") + print(" }") + print(" if (verbose) {") + print(" %sstr =3D %sstr . sprintf(\":%%d\", %s)" % + (varname, varname, varname)) + print(" }") + print(" return %sstr;" % varname) + print("}") + + +genfunc("libvirt_rpc_auth_name", "type", auth) +genfunc("libvirt_rpc_type_name", "type", types) +genfunc("libvirt_rpc_status_name", "status", status) + +print("function libvirt_rpc_program_name(program, verbose)") +print("{") + +first =3D True +for funcname in sorted(funcs.keys()): + cond =3D "} else if" + if first: + cond =3D "if" + first =3D False + + print(" %s (program =3D=3D %s) {" % (cond, funcs[funcname]["id"])) + print(" programstr =3D \"%s\"" % funcname) + +print(" } else {") +print(" programstr =3D \"unknown\";") +print(" verbose =3D 1;") +print(" }") +print(" if (verbose) {") +print(" programstr =3D programstr . sprintf(\":%d\", program)") +print(" }") +print(" return programstr;") +print("}") + +print("function libvirt_rpc_procedure_name(program, version, proc, verbose= )") +print("{") + +first =3D True +for prog in sorted(funcs.keys()): + cond =3D "} else if" + if first: + cond =3D "if" + first =3D False + + print(" %s (program =3D=3D %s && version =3D=3D %s) {" % + (cond, funcs[prog]["id"], funcs[prog]["version"])) + + pfirst =3D True + for id in range(len(funcs[prog]["progs"])): + pcond =3D "} else if" + if pfirst: + pcond =3D "if" + pfirst =3D False + + print(" %s (proc =3D=3D %s) {" % (pcond, id + 1)) + print(" procstr =3D \"%s\";" % funcs[prog]["progs"][id]) + + print(" } else {") + print(" procstr =3D \"unknown\";") + print(" verbose =3D 1;") + print(" }") + +print(" } else {") +print(" procstr =3D \"unknown\";") +print(" verbose =3D 1;") +print(" }") +print(" if (verbose) {") +print(" procstr =3D procstr . sprintf(\":%d\", proc)") +print(" }") +print(" return procstr;") +print("}") diff --git a/src/Makefile.am b/src/Makefile.am index fc155bb037..1f60aee625 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -633,8 +633,9 @@ RPC_PROBE_FILES +=3D $(srcdir)/rpc/virnetprotocol.x \ $(srcdir)/remote/qemu_protocol.x \ $(srcdir)/admin/admin_protocol.x =20 -libvirt_functions.stp: $(RPC_PROBE_FILES) $(srcdir)/rpc/gensystemtap.pl - $(AM_V_GEN)$(PERL) -w $(srcdir)/rpc/gensystemtap.pl $(RPC_PROBE_FILES) > = $@ +libvirt_functions.stp: $(RPC_PROBE_FILES) $(top_srcdir)/scripts/gensystemt= ap.py + $(AM_V_GEN)$(RUNUTF8) $(PYTHON) $(top_srcdir)/scripts/gensystemtap.py \ + $(RPC_PROBE_FILES) > $@ =20 %_probes.stp: %_probes.d $(top_srcdir)/scripts/dtrace2systemtap.py \ $(top_builddir)/config.status diff --git a/src/rpc/Makefile.inc.am b/src/rpc/Makefile.inc.am index b8ca53c69a..d6ccf8e346 100644 --- a/src/rpc/Makefile.inc.am +++ b/src/rpc/Makefile.inc.am @@ -3,7 +3,6 @@ EXTRA_DIST +=3D \ rpc/gendispatch.pl \ rpc/genprotocol.pl \ - rpc/gensystemtap.pl \ rpc/virnetprotocol.x \ rpc/virkeepaliveprotocol.x \ $(NULL) diff --git a/src/rpc/gensystemtap.pl b/src/rpc/gensystemtap.pl deleted file mode 100755 index 6693d4d6f5..0000000000 --- a/src/rpc/gensystemtap.pl +++ /dev/null @@ -1,193 +0,0 @@ -#!/usr/bin/env perl -# -# Copyright (C) 2011-2012 Red Hat, Inc. -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see -# . -# -# Generate a set of systemtap functions for translating various -# RPC enum values into strings -# -# perl gensystemtap.pl */*.x > libvirt_functions.stp -# - -use strict; - -my %funcs; - -my %type; -my %status; -my %auth; - -my $instatus =3D 0; -my $intype =3D 0; -my $inauth =3D 0; -while (<>) { - if (/enum\s+virNetMessageType/) { - $intype =3D 1; - } elsif (/enum\s+virNetMessageStatus/) { - $instatus =3D 1; - } elsif (/enum remote_auth_type/) { - $inauth =3D 1; - } elsif (/}/) { - $instatus =3D $intype =3D $inauth =3D 0; - } elsif ($instatus) { - if (/^\s+VIR_NET_(\w+)\s*=3D\s*(\d+),?$/) { - $status{$2} =3D lc $1; - } - } elsif ($intype) { - if (/^\s+VIR_NET_(\w+)\s*=3D\s*(\d+),?$/) { - $type{$2} =3D lc $1; - } - } elsif ($inauth) { - if (/^\s+REMOTE_AUTH_(\w+)\s*=3D\s*(\d+),?$/) { - $auth{$2} =3D lc $1; - } - } else { - if (/(?:VIR_)?(\w+?)(?:_PROTOCOL)?_PROGRAM\s*=3D\s*0x([a-fA-F0-9]+= )\s*;/) { - $funcs{lc $1} =3D { id =3D> hex($2), version =3D> undef, progs= =3D> [] }; - } elsif (/(?:VIR_)?(\w+?)(?:_PROTOCOL)?_(?:PROGRAM|PROTOCOL)_VERSI= ON\s*=3D\s*(\d+)\s*;/) { - $funcs{lc $1}->{version} =3D $2; - } elsif (/(?:VIR_)?(\w+?)(?:_PROTOCOL)?_PROC_(.*?)\s+=3D\s+(\d+)/)= { - $funcs{lc $1}->{progs}->[$3] =3D lc $2; - } - } -} - -print <{id}, ") {\n"; - print " programstr =3D \"", $prog, "\"\n"; -} -print <{id}, " && version = =3D=3D ", $funcs{$prog}->{version}, ") {\n"; - - my $pfirst =3D 1; - for (my $id =3D 1 ; $id <=3D $#{$funcs{$prog}->{progs}} ; $id++) { - my $cond =3D $pfirst ? "if" : "} else if"; - $pfirst =3D 0; - print " $cond (proc =3D=3D $id) {\n"; - print " procstr =3D \"", $funcs{$prog}->{progs}->[$id], "= \";\n"; - } - print " } else {\n"; - print " procstr =3D \"unknown\";\n"; - print " verbose =3D 1;\n"; - print " }\n"; -} -print <