7
  #   Iˆl–ßi~”*j"TåÀ†à„¦-ÿ×„ p?ÅÕ ?÷     I#!/usr/bin/python2
# vim: et sta sts=4 sw=4 ts=8

# This handles the systemtap equivalent of
# $(DTRACE) $(DTRACEFLAGS) -G -s $^ -o $@
# $(DTRACE) $(DTRACEFLAGS) -h -s $^ -o $@
# which is a step that builds DTrace provider and probe definitions

# Copyright (C) 2009-2018 Red Hat Inc.
#
# This file is part of systemtap, and is free software.  You can
# redistribute it and/or modify it under the terms of the GNU General
# Public License (GPL); either version 2, or (at your option) any
# later version.

# ignore line too long, missing docstring, method could be a function,
#        too many public methods
# pylint: disable=C0301
# pylint: disable=C0111
# pylint: disable=R0201
# pylint: disable=R0904

import os
import sys
from shlex import split
from subprocess import call
from tempfile import mkstemp
try:
    from pyparsing import alphas, cStyleComment, delimitedList, Group, \
        Keyword, lineno, Literal, nestedExpr, nums, oneOf, OneOrMore, \
        Optional, ParseException, ParserElement, restOfLine, restOfLine, \
        Suppress, SkipTo, Word, ZeroOrMore
    HAVE_PYP = True
except ImportError:
    HAVE_PYP = False


# Common file creation methods for pyparsing and string pattern matching

class _HeaderCreator(object):
    def init_semaphores(self, fdesc):
        # dummy declaration just to make the object file non-empty
        fdesc.write("/* Generated by the Systemtap dtrace wrapper */\n\n")
        fdesc.write("static void __dtrace (void) __attribute__((unused));\n")
        fdesc.write("static void __dtrace (void) {}\n")
        fdesc.write("\n#include <sys/sdt.h>\n\n")

    def init_probes(self, fdesc):
        fdesc.write("/* Generated by the Systemtap dtrace wrapper */\n\n")
        fdesc.write("\n#define _SDT_HAS_SEMAPHORES 1\n\n")
        fdesc.write("\n#define STAP_HAS_SEMAPHORES 1 /* deprecated */\n\n")
        fdesc.write("\n#include <sys/sdt.h>\n\n")

    def add_semaphore(self, this_provider, this_probe):
        # NB: unsigned short is fixed in ABI
        semaphores_def = '\n#if defined STAP_SDT_V1\n'
        semaphores_def += '#define %s_%s_semaphore %s_semaphore\n' % \
                          (this_provider, this_probe, this_probe)
        semaphores_def += '#endif\n'
        semaphores_def += '#if defined STAP_SDT_V1 || defined STAP_SDT_V2 \n'
        semaphores_def += "__extension__ unsigned short %s_%s_semaphore __attribute__ ((unused)) __attribute__ ((section (\".probes\")));\n" % \
            (this_provider, this_probe)
        semaphores_def += '#else\n'
        semaphores_def += "__extension__ unsigned short %s_%s_semaphore __attribute__ ((unused)) __attribute__ ((section (\".probes\"))) __attribute__ ((visibility (\"hidden\")));\n" % \
            (this_provider, this_probe)
        semaphores_def += '#endif\n'
        return semaphores_def

    def add_probe(self, this_provider, this_probe, args):
        stap_str = ""
        this_probe_canon = this_provider.upper() + "_" + this_probe.replace("__", "_").upper()
        define_str = "#define %s(" % (this_probe_canon)
        comment_str = "/* %s (" % (this_probe_canon)

        if len(args) == 0:
            stap_str += "DTRACE_PROBE ("
        else:
            stap_str += "DTRACE_PROBE%d (" % len(args)
        stap_str += "%s, %s" % (this_provider, this_probe)
        i = 0
        while i < len(args):
            if i != 0:
                define_str += ", "
                comment_str += ","
            define_str = define_str + "arg%s" % (i + 1)
            stap_str = stap_str + ", arg%s" % (i + 1)
            for argi in args[i]:
                if len(argi) > 0:
                    comment_str += " %s" % argi
            i += 1
        stap_str += ")"
        comment_str += " ) */"
        define_str += ") \\\n"
        probe_def = '%s\n' % (comment_str)
        probe_def += ('#if defined STAP_SDT_V1\n')
        probe_def += ('#define %s_ENABLED() __builtin_expect (%s_semaphore, 0)\n' % \
                      (this_probe_canon, this_probe))
        probe_def += ('#define %