From nobody Tue Nov 26 14:21:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.61 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=1573483567; cv=none; d=zoho.com; s=zohoarc; b=at55jwYYVAP5Fr8a2EzD5/EiX/V6jQphVWi4C6OvLZ40PIGsyuAIono8FivE7HbiU55gM3icdmnN7Nb/iGjbEjXUA+NZfEtnpuUDo6haQILODtM+2ImaprQSYCGqIPcTLUuLu3PXFS2wsVIFbYCRvr07WLTNEhwed5kAIEcvxQ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573483567; 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=lyq2/blq39jNc9wVFFN7LBUNxOpRxUnX0rcr9xRSuq4=; b=BYgjoEFM+PuGuLcoGRdpzCVxrBzmrL+Cn5O1SNpT/42YdojprLXVr8trbnB+ahJoxYf+/jFbt49t9eOXIad26HX9HEik7GGfP57oDMD+aqE2GJSzmR4Nbd9FuzfbwK1WQptSPiVbjcG+dwUvrMwTNi5CW8Pdrlr9lYvp0LMdM8o= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1573483567490284.4211407592377; Mon, 11 Nov 2019 06:46:07 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-219-UxU43hYUMpqk62okoMN2BA-1; Mon, 11 Nov 2019 09:45:15 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BF22C86A06F; Mon, 11 Nov 2019 14:45:06 +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 7D512798E; Mon, 11 Nov 2019 14:45:06 +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 078F94E571; Mon, 11 Nov 2019 14:45:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xABEivLk020413 for ; Mon, 11 Nov 2019 09:44:57 -0500 Received: by smtp.corp.redhat.com (Postfix) id 96AE417AD4; Mon, 11 Nov 2019 14:44:57 +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 070C95D9C9; Mon, 11 Nov 2019 14:44:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573483566; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=lyq2/blq39jNc9wVFFN7LBUNxOpRxUnX0rcr9xRSuq4=; b=Fqrt5CZf+yuxH5q2JqyQMphawjsS/y3/IhqPBk0EKRyFi7E9R4gi2w3I2hfqlcrPetkih2 saAsDTtduqAwB/HDvahI2bYnT7jZ33u/7fl8TXNeBSJEioOvRseNCBRB2h0m8lQJPvoyOX RCfYTu3yhCqRNA4sVQt0Ie9IkkwhLpg= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Mon, 11 Nov 2019 14:38:12 +0000 Message-Id: <20191111143826.16050-10-berrange@redhat.com> In-Reply-To: <20191111143826.16050-1-berrange@redhat.com> References: <20191111143826.16050-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v5 09/23] 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-MC-Unique: UxU43hYUMpqk62okoMN2BA-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: J=C3=A1n Tomko Tested-by: Cole Robinson --- Makefile.am | 1 + build-aux/syntax-check.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 e50a29635b..afed409c94 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/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index 016e05e7a2..26eb5b94d9 100644 --- a/build-aux/syntax-check.mk +++ b/build-aux/syntax-check.mk @@ -466,6 +466,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 @@ -2261,9 +2262,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 \ - ^(build-aux/syntax-check\.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 085c193013..b0ca9d3442 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -561,8 +561,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 a5bde92c9f..11920b3270 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 <