예제 #1
0
    def __init__(self, pid):
        self.pid = pid
        self.proc = Proc(pid)
        self.arch = self.proc.arch
        self.path = self.proc.path
        self.libc_path = self.proc.libc
        self.ld_path = self.proc.ld

        if self.arch == '32':
            self.size_t = 4
            self.pack = p32
            self.unpack = u32
        elif self.arch == '64':
            self.size_t = 8
            self.pack = p64
            self.unpack = u64
        else:
            raise NotImplementedError('invalid arch')

        libc_info = get_libc_info(self.libc_path, self.proc.ld)
        self.libc_version = libc_info['version']
        self.tcache_enable = libc_info['tcache_enable']
        self.main_arena_offset = libc_info['main_arena_offset']

        self.libc_base = self.proc.bases['libc']
        self.heap_base = self.proc.bases['heap']

        self.MallocState = malloc_state_generator(self.libc_version, self.arch)
        self.MallocChunk = malloc_chunk_generator(self.libc_version, self.arch)
        self.TcacheStruct = tcache_struct_generator(self.libc_version,
                                                    self.arch)
예제 #2
0
 def __init__(self, command, *args, **kwargs):
     self.command = command
     self.proc = Proc("LC_ALL=C gdb --args " + command, shell=True)
     self.debugf = open('debug.log', 'w')
     self.compilation_directory = ''
     self.sourcefile = ''
     self.sources = []
     self.breakpoints = {}
     self.lineno = -1
     self.cached_stdout = ''
     self.cached_stderr = ''
     while 1:
         time.sleep(0.01)
         if self.proc.canread(self.proc.stdout()): break
         if self.proc.canread(self.proc.stderr()): break
     s, t = self.read()
     # disable getting the output split into single pages and interacting
     self.send('set pagination off')
     self.read()
     # disable printing of strings interrupted by <repeats x times>
     self.send('set print repeats 0xfffffffe')
     self.read()
     master, slave = pty.openpty()
     self.send('set inferior-tty ' + os.ttyname(slave))
     self.read()
     self.pty_master = master
     self.pty_slave = slave
     self._reload_sources()
     self.cached_stdout = s
     self.cached_stderr = t
예제 #3
0
파일: app.py 프로젝트: yappie/noolt
 def add_proc(self):
     proc = Proc(self, self.run_args)
     proc.print_tracebacks = self.print_tracebacks
     new_hosts = proc.chan.get_locals('hosts') or []
     self.procs.append(proc)
     App.httpds.add_remove(self.on_wsgi, new_hosts, proc.hosts)
     proc.hosts = new_hosts
     self.set_reload_intervals()
예제 #4
0
파일: app.py 프로젝트: yappie/noolt
 def add_proc(self):
     proc = Proc(self, self.run_args)
     proc.print_tracebacks = self.print_tracebacks
     new_hosts = proc.chan.get_locals('hosts') or []
     self.procs.append(proc)
     App.httpds.add_remove(self.on_wsgi, new_hosts, proc.hosts)
     proc.hosts = new_hosts
     self.set_reload_intervals()
예제 #5
0
    def __init__(self):
        # Important variables, do not change the order
        self.app = QApplication(sys.argv)
        self.MainWindow = QMainWindow()
        self.setupUi(
            self.MainWindow
        )  # This needs to be called before we can reference self.tableView

        # Hide under development tabs
        for n in range(4):
            self.tabWidget.removeTab(2)

        self.proc = Proc()
        self.processListModel = QtGui.QStandardItemModel(1, 6)
        self.applicationListModel = QtGui.QStandardItemModel(1, 3)
        self.timer = QtCore.QTimer()
        # See if this is our first startup. If so, we need to make a bash script to launch this program and add it to unity's shortcuts.
        self.setupShortcuts()

        # It was too much of a hassle to create a custom class and working off generated code, so we're using this hackish fix for a small change
        self.tableView.contextMenuEvent = types.MethodType(
            self._hProcessListContextMenuEvent,
            self.tableView)  # Add a right click menu
        self.appTableView.contextMenuEvent = types.MethodType(
            self._hApplicationListContextMenuEvent,
            self.appTableView)  # Add a right click menu

        self.configProcessList()
        self.configApplicationsList()
        self.update()

        # This timer will act as timer for polling and for updating the GUI
        self.timer.timeout.connect(self.update)
        # Every second
        self.timer.start(1000)

        self.endProcessButton.clicked.connect(self._hEndProcessButton)
        self.endTaskButton.clicked.connect(self._hEndTaskButton)
        self.switchToButton.clicked.connect(self._hSwitchToButton)
        self.newTaskButton.clicked.connect(self._hNewTaskButton)
        self.newTaskButton.hide()  # Unfinished feature
예제 #6
0
파일: nadbg.py 프로젝트: HawaiiGuitar/nadbg
 def is_candidate(s, pid):
     try:
         p = Proc(pid)
         if os.path.exists(s):
             # s is path
             path = os.path.abspath(s)
             return path == p.path
         else:
             # s is filename
             return s == p.name
     except OSError:
         return False
예제 #7
0
파일: nadbg.py 프로젝트: HawaiiGuitar/nadbg
    def attach(self, s):
        self.s = s

        def is_candidate(s, pid):
            try:
                p = Proc(pid)
                if os.path.exists(s):
                    # s is path
                    path = os.path.abspath(s)
                    return path == p.path
                else:
                    # s is filename
                    return s == p.name
            except OSError:
                return False

        if s.isdigit():
            self.set_pid(int(s))
        else:
            candidates = []
            # assume s is str
            for pid in getpids():
                if is_candidate(s, pid):
                    candidates.append(pid)
            sz = len(candidates)
            if sz == 0:
                print('{} not found'.format(s))
            elif sz == 1:
                self.set_pid(candidates[0])
            else:
                # multiple candidates
                for pid in candidates:
                    p = Proc(pid)
                    print('{}: {} {}'.format(pid, p.username, p.path))
                choise = get_int(0, sz - 1,
                                 'choose from 0 to {}: '.format(sz - 1))
                self.set_pid(candidates[choise])
        return self.pid
예제 #8
0
파일: core.py 프로젝트: lybobob1/welpwn
 def proc(self):
     """Proc: Implemented in PwnContext/proc.py
     """
     return Proc(self.pid)
예제 #9
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from buf import Buf
from gen import Gen
from proc import Proc
from evenement import Evenement
import matplotlib.pyplot as plt

temps = 0
temps_fin = 10
liste_comp = [Buf([Evenement.JOB, Evenement.DONE], [Evenement.REQ]),
              Gen([Evenement.JOB]), Proc([Evenement.REQ], [Evenement.DONE])]
liste_temps = []
liste_q = []

while(temps <= temps_fin):
    ta_min = liste_comp[0].get_ta()
    for i in range(1, len(liste_comp)):
        tmp = liste_comp[i].get_ta()
        if ta_min > tmp:
            ta_min = tmp
                
    imminent = []
    for comp in liste_comp:
        if comp.get_ta()  == ta_min:
            imminent.append(comp)
    liste_ev_im = {}
    for im in imminent:
        evenement = im.f_lambda()
        for key in evenement:
예제 #10
0
class OverseerMainWindow(Ui_MainWindow):
    def __init__(self):
        # Important variables, do not change the order
        self.app = QApplication(sys.argv)
        self.MainWindow = QMainWindow()
        self.setupUi(
            self.MainWindow
        )  # This needs to be called before we can reference self.tableView

        # Hide under development tabs
        for n in range(4):
            self.tabWidget.removeTab(2)

        self.proc = Proc()
        self.processListModel = QtGui.QStandardItemModel(1, 6)
        self.applicationListModel = QtGui.QStandardItemModel(1, 3)
        self.timer = QtCore.QTimer()
        # See if this is our first startup. If so, we need to make a bash script to launch this program and add it to unity's shortcuts.
        self.setupShortcuts()

        # It was too much of a hassle to create a custom class and working off generated code, so we're using this hackish fix for a small change
        self.tableView.contextMenuEvent = types.MethodType(
            self._hProcessListContextMenuEvent,
            self.tableView)  # Add a right click menu
        self.appTableView.contextMenuEvent = types.MethodType(
            self._hApplicationListContextMenuEvent,
            self.appTableView)  # Add a right click menu

        self.configProcessList()
        self.configApplicationsList()
        self.update()

        # This timer will act as timer for polling and for updating the GUI
        self.timer.timeout.connect(self.update)
        # Every second
        self.timer.start(1000)

        self.endProcessButton.clicked.connect(self._hEndProcessButton)
        self.endTaskButton.clicked.connect(self._hEndTaskButton)
        self.switchToButton.clicked.connect(self._hSwitchToButton)
        self.newTaskButton.clicked.connect(self._hNewTaskButton)
        self.newTaskButton.hide()  # Unfinished feature

    def show(self):
        self.MainWindow.show()

    # Update the data and view
    def update(self):
        self.proc.readData()
        self.updateView()

    # Update the view depending on what tab we are
    def updateView(self):
        self.readTotals()
        # if self.tabWidget.currentIndex() == 0:
        self.readApplicationsList()
        # elif self.tabWidget.currentIndex() == 1:
        self.readProcessList()

    def readTotals(self):

        str = "Total CPU usage: " + self.proc.cpuStr + "%:  " + "Total Memory in Use: " + self.proc.memStr + "%:"
        self.statusBar.showMessage(str)

    def configApplicationsList(self):
        # Set the labels for the columns
        self.applicationListModel.setHeaderData(0, 1, "Task")
        self.applicationListModel.setHeaderData(1, 1, "Status")
        self.applicationListModel.setHeaderData(2, 1, "PID")
        self.appTableView.setModel(self.applicationListModel)
        self.appTableView.verticalHeader().setVisible(False)
        self.appTableView.horizontalHeader().setHighlightSections(False)
        self.appTableView.setColumnHidden(2, True)
        self.appTableView.horizontalHeader().resizeSection(0, 650)

    def readApplicationsList(self):
        pid = self.getSelectedPID(self.appTableView, 2)
        # Erase all of the items in the model and re-add them
        self.applicationListModel.removeRows(
            0, self.applicationListModel.rowCount())
        self.appTableView.setSortingEnabled(
            False
        )  # This is a hack fix for getting sorting to stay when deleting all items and re-adding them
        for i, value in enumerate(self.proc.openWindows):
            item = QtGui.QStandardItem(value[1])
            self.applicationListModel.setItem(i, 0, item)
            item = QtGui.QStandardItem(value[2])
            self.applicationListModel.setItem(i, 1, item)
            item = QtGui.QStandardItem(value[0])
            self.applicationListModel.setItem(i, 2, item)
        self.appTableView.setSortingEnabled(
            True
        )  # This is a hack fix for getting sorting to stay when deleting all items and re-adding them

        # Another hack fix for keeping selection
        # It adds more strain to the thread
        if pid != -1:
            for index in range(0, self.appTableView.model().rowCount()):
                self.appTableView.selectRow(index)
                if self.appTableView.selectionModel().selection().indexes(
                )[2].data() == pid:
                    # If the previously selected item is found, leave
                    return
            # If we are still here, the old item wasn't found, so deselect all
            self.appTableView.clearSelection()

    def configProcessList(self):
        # Set the labels for the columns
        self.processListModel.setHeaderData(0, 1, "Image Name")
        self.processListModel.setHeaderData(1, 1, "PID")
        self.processListModel.setHeaderData(2, 1, "User Name")
        self.processListModel.setHeaderData(3, 1, "CPU (%)")
        self.processListModel.setHeaderData(4, 1, "Memory (%)")
        self.processListModel.setHeaderData(5, 1, "Description")
        self.tableView.setModel(self.processListModel)
        self.tableView.verticalHeader().setVisible(False)
        self.tableView.horizontalHeader().setHighlightSections(False)

    def readProcessList(self):
        pid = self.getSelectedPID(self.tableView, 1)
        # Erase all of the items in the model and re-add them
        self.processListModel.removeRows(0, self.processListModel.rowCount())
        self.tableView.setSortingEnabled(
            False
        )  # This is a hack fix for getting sorting to stay when deleting all items and re-adding them
        for i, (key, value) in enumerate(self.proc.processList.items()):
            item = QtGui.QStandardItem(value.name)
            self.processListModel.setItem(i, 0, item)
            item = QtGui.QStandardItem(value.pid)
            self.processListModel.setItem(i, 1, item)
            item = QtGui.QStandardItem(
                self.proc.userList.getLoginname(value.realUid))
            self.processListModel.setItem(i, 2, item)
            item = QtGui.QStandardItem(str(value.cpuPercentage))
            self.processListModel.setItem(i, 3, item)
            item = QtGui.QStandardItem(value.ramPercentage)
            self.processListModel.setItem(i, 4, item)
        self.tableView.setSortingEnabled(
            True
        )  # This is a hack fix for getting sorting to stay when deleting all items and re-adding them
        #causes deselecting problem when sorting.
        #self.processListModel.sort(4, 1)

        # Another hack fix for keeping selection
        # It adds more strain to the thread
        if pid != -1:
            for index in range(0, self.tableView.model().rowCount()):
                self.tableView.selectRow(index)
                if self.tableView.selectionModel().selection().indexes(
                )[1].data() == pid:
                    # If the previously selected item is found, leave
                    return
            # If we are still here, the old item wasn't found, so deselect all
            self.tableView.clearSelection()

    # Returns -1 if no selection or PID couldn't be found, or returns PID
    def getSelectedPID(self, tableView, column):
        pid = -1
        indexes = tableView.selectionModel().selection().indexes()
        if len(indexes) != 0:
            selection = tableView.selectionModel().selection().indexes(
            )[0]  # selection() seems to be our best bet on seeing what we right clicked on, could possibly be buggy
            pid = tableView.model().data(tableView.model().index(
                selection.row(), column))  # Get our PID
        return pid

    # Check if this is our first startup, if so, make the program start on startup
    # This is aimed for only the Ubuntu system
    def setupShortcuts(self):
        ret = self.setupLaunchScript()
        if ret == False:
            return
        self.setupgsettings(ret)

    # FIXME: Add exception handling to this just to be safe
    def setupLaunchScript(self):
        directory = "~"
        directory = os.path.expanduser(directory)
        launchScriptString = "~/openOverseer.sh"
        launchScriptString = os.path.expanduser(launchScriptString)
        launchScript = Path(launchScriptString)
        if not os.path.exists(directory):
            print("You do not have a home directory")
            return False
            # os.makedirs(directory) # Highly doubt we would need to make the $HOME directory
        if not launchScript.is_file():
            fd = open(launchScriptString, 'w')
            fd.write('#!/bin/bash\n')
            overseerLocation = os.path.realpath(__file__)
            fd.write("python3 '" + overseerLocation + "'\n")
            fd.close()
            st = os.stat(launchScriptString)
            os.chmod(launchScriptString, st.st_mode
                     | stat.S_IEXEC)  # Make the bash script executable
        return launchScriptString

    def setupgsettings(
        self, launchScriptString
    ):  # Setup unity's custom shortcut to launch our launch script
        if launchScriptString is None:
            print("launchScriptString is None")
            return
        ret = subprocess.run([
            'gsettings', 'get', 'org.gnome.settings-daemon.plugins.media-keys',
            'custom-keybindings'
        ],
                             stdout=subprocess.PIPE,
                             universal_newlines=True)
        # An example of red.stdout: ['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/']

        # Go through ret and see if any of the custom shortcuts already have an Overseer shortcut
        # Code generated by regex101.com
        regex = r"'([^']+)'"
        matches = re.finditer(regex, ret.stdout)
        found = False
        matchNum = 0
        for match in matches:
            matchNum = matchNum + 1
            # print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
            for groupNum in range(0, len(match.groups())):
                groupNum = groupNum + 1
                # print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
                groupMatch = match.group(groupNum)
                # print(groupMatch)
                # Command for asking name of the shortcut:
                # gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name
                ret2 = subprocess.run([
                    'gsettings', 'get',
                    'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:'
                    + groupMatch, 'name'
                ],
                                      stdout=subprocess.PIPE,
                                      universal_newlines=True)
                shortcutName = re.sub(
                    "['\n]", "",
                    ret2.stdout)  # Take out ' and new line characters
                if shortcutName == "Overseer":
                    found = True
        if not found:
            # Make a new list for custom-keybindings
            if not (ret.stdout == "@as []\n"):
                newList = re.sub(
                    "]\n$",
                    ", '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom"
                    + str(matchNum) + "/']", ret.stdout)
                newList = re.sub(
                    "]$",
                    ", '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom"
                    + str(matchNum + 1) + "/']", newList)
            else:
                newList = "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']"
                newList = re.sub(
                    "]$",
                    ", '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/']",
                    newList)
            # How it should look
            # gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "[<altered_list>]"
            ret2 = subprocess.run([
                'gsettings', 'set',
                'org.gnome.settings-daemon.plugins.media-keys',
                'custom-keybindings', newList
            ],
                                  stdout=subprocess.PIPE,
                                  universal_newlines=True)
            # Remove ctrl+shift+del as a shortcut to log out
            ret2 = subprocess.run([
                'gsettings', 'set',
                'org.gnome.settings-daemon.plugins.media-keys', 'logout', ''
            ],
                                  stdout=subprocess.PIPE,
                                  universal_newlines=True)

            # Example for setting
            # gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ name '<newname>'
            ret2 = subprocess.run([
                'gsettings', 'set',
                'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom'
                + str(matchNum) + '/', 'name', 'Overseer'
            ],
                                  stdout=subprocess.PIPE,
                                  universal_newlines=True)
            ret2 = subprocess.run([
                'gsettings', 'set',
                'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom'
                + str(matchNum) + '/', 'command', launchScriptString
            ],
                                  stdout=subprocess.PIPE,
                                  universal_newlines=True)
            ret2 = subprocess.run([
                'gsettings', 'set',
                'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom'
                + str(matchNum) + '/', 'binding', '<Primary><Shift>Escape'
            ],
                                  stdout=subprocess.PIPE,
                                  universal_newlines=True)

            ret2 = subprocess.run([
                'gsettings', 'set',
                'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom'
                + str(matchNum + 1) + '/', 'name', 'Overseer'
            ],
                                  stdout=subprocess.PIPE,
                                  universal_newlines=True)
            ret2 = subprocess.run([
                'gsettings', 'set',
                'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom'
                + str(matchNum + 1) + '/', 'command', launchScriptString
            ],
                                  stdout=subprocess.PIPE,
                                  universal_newlines=True)
            ret2 = subprocess.run([
                'gsettings', 'set',
                'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom'
                + str(matchNum + 1) + '/', 'binding', '<Primary><Alt>Delete'
            ],
                                  stdout=subprocess.PIPE,
                                  universal_newlines=True)

    # This should be a hidden function. Made it a member function so it could access proc
    def _hProcessListContextMenuEvent(self, tableView, event):
        # Define the menu
        tableView.menu = QtWidgets.QMenu(tableView)
        openFileLocationAction = QtWidgets.QAction("Open File Location",
                                                   tableView)
        # openFileLocationAction.triggered.connect(test) # another way to add functions onto actions
        tableView.menu.addAction(openFileLocationAction)
        tableView.menu.addSeparator()
        endProcessAction = QtWidgets.QAction("End Process", tableView)
        tableView.menu.addAction(endProcessAction)
        endProcessTreeAction = QtWidgets.QAction("End Process Tree", tableView)
        tableView.menu.addAction(endProcessTreeAction)

        # Summon the context menu
        action = tableView.menu.exec_(tableView.mapToGlobal(event.pos()))
        # tableView.menu.popup(QtGui.QCursor.pos()) # Another way of bringing up the menu
        pid = self.getSelectedPID(tableView, 1)
        if pid == -1:
            return
        if action == openFileLocationAction:
            if sys.platform == "linux":
                # We need to pass in /dev/null to stdout and stderr so we don't get spam in our main program's stdout and stderr
                if self.proc.processList[pid].path == "DENIED":
                    QtWidgets.QMessageBox.about(
                        self.MainWindow, "Warning",
                        "You have no permissions to access this file location."
                    )
                else:
                    subprocess.run(
                        ['xdg-open', self.proc.processList[pid].path],
                        stdout=subprocess.DEVNULL,
                        stderr=subprocess.DEVNULL)
            else:
                QtWidgets.QMessageBox.about(
                    self.MainWindow, "Warning",
                    "Your system is not running Linux, unable to open a file explorer."
                )
        elif action == endProcessAction:
            ret = self.proc.processList[pid].endProcess()
            if ret == 1:
                QtWidgets.QMessageBox.about(
                    self.MainWindow, "Warning",
                    "You do not have permission to end this process.")
        elif action == endProcessTreeAction:
            ret = self.proc.processList[pid].endProcessTree()
            if ret == 1:
                QtWidgets.QMessageBox.about(
                    self.MainWindow, "Warning",
                    "You do not have permission to end this process.")

    def _hApplicationListContextMenuEvent(self, tableView, event):
        # Define the menu
        tableView.menu = QtWidgets.QMenu(tableView)
        switchToAction = QtWidgets.QAction("Switch To", tableView)
        tableView.menu.addAction(switchToAction)
        endTaskAction = QtWidgets.QAction("End Task", tableView)
        tableView.menu.addAction(endTaskAction)

        # Summon the context menu
        action = tableView.menu.exec_(tableView.mapToGlobal(event.pos()))
        # tableView.menu.popup(QtGui.QCursor.pos()) # Another way of bringing up the menu
        pid = self.getSelectedPID(tableView, 2)
        if pid == -1:
            return
        if action == switchToAction:
            self.proc.processList[pid].switchTo()
        elif action == endTaskAction:
            ret = self.proc.processList[pid].endTask()
            if ret == 1:
                QtWidgets.QMessageBox.about(
                    self.MainWindow, "Warning",
                    "You do not have permission to end this process.")

    # This should be a hidden function. Made it a member function so it could access proc
    def _hEndProcessButton(self):
        pid = self.getSelectedPID(self.tableView, 1)
        ret = self.proc.processList[pid].endProcess()
        if ret == 1:
            QtWidgets.QMessageBox.about(
                self.MainWindow, "Warning",
                "You do not have permission to end this process.")

    def _hEndTaskButton(self):
        pid = self.getSelectedPID(self.appTableView, 2)
        ret = self.proc.processList[pid].endTask()
        if ret == 1:
            QtWidgets.QMessageBox.about(
                self.MainWindow, "Warning",
                "You do not have permission to end this process.")

    def _hSwitchToButton(self):
        pid = self.getSelectedPID(self.appTableView, 2)
        self.proc.processList[pid].switchTo()

    def _hNewTaskButton(self):
        1
예제 #11
0
파일: nadbg.py 프로젝트: HawaiiGuitar/nadbg
 def proc(self):
     if self.pid == 0:
         return None
     else:
         return Proc(self.pid)
예제 #12
0
class HeapInspector(object):
    '''Core Class to parse heap and arena.

    Attributes:
        pid (int): pid of the monitored process.
        arch (str): '32' or '64'.
        libc_path (str): Path to libc.
        libc_version (str): Libc version like '2.23'.
        libc_base (int): Start address of libc.
        heap_base (int): Start address of heap. 
    Args:
        pid (int): pid of the target process.
    Raises:
        NotImplementedError: for none supported arch.
    '''
    def __init__(self, pid):
        self.pid = pid
        self.proc = Proc(pid)
        self.arch = self.proc.arch
        self.path = self.proc.path
        self.libc_path = self.proc.libc
        self.ld_path = self.proc.ld

        if self.arch == '32':
            self.size_t = 4
            self.pack = p32
            self.unpack = u32
        elif self.arch == '64':
            self.size_t = 8
            self.pack = p64
            self.unpack = u64
        else:
            raise NotImplementedError('invalid arch')

        libc_info = get_libc_info(self.libc_path, self.proc.ld)
        self.libc_version = libc_info['version']
        self.tcache_enable = libc_info['tcache_enable']
        self.main_arena_offset = libc_info['main_arena_offset']

        self.libc_base = self.proc.bases['libc']
        self.heap_base = self.proc.bases['heap']

        self.MallocState = malloc_state_generator(self.libc_version, self.arch)
        self.MallocChunk = malloc_chunk_generator(self.libc_version, self.arch)
        self.TcacheStruct = tcache_struct_generator(self.libc_version,
                                                    self.arch)

    @property
    def ranges(self):
        '''dict: vmmap ranges.
        '''
        return self.proc.ranges

    @property
    def bases(self):
        '''dict: vmmap start addresses.
        '''
        return self.proc.bases

    @property
    def heapmem(self):
        '''str: heap memory dump.
        '''
        try:
            start, end = self.proc.ranges['heap'][0]
        except IndexError:
            raise Exception("Heap not initialized")
        return self.proc.read(start, end - start)

    @property
    def arenamem(self):
        '''str: main_arena memory dump.
        '''
        arena_size = self.MallocState._size
        arena_addr = self.libc_base + self.main_arena_offset
        return self.proc.read(arena_addr, arena_size)

    @property
    def main_arena(self):
        '''C_Struct: main_arena
        '''
        if self.heap_base == 0:
            # just a refresh in case that heap_base = 0
            self.heap_base = self.proc.bases['heap']
        arena_addr = self.libc_base + self.main_arena_offset
        return self.MallocState._new(self.arenamem, arena_addr)

    @property
    def tcache(self):
        '''C_Struct: tcache perthread
        '''
        if self.tcache_enable:
            testmem = self.proc.read(self.proc.bases['heap'] + self.size_t,
                                     self.size_t)
            # this happens in some 32 bit libc heap
            if self.unpack(testmem) == 0:
                base_addr = 4 * self.size_t + self.proc.bases['heap']
            else:
                base_addr = 2 * self.size_t + self.proc.bases['heap']

            mem = self.proc.read(base_addr, self.TcacheStruct._size)
            return self.TcacheStruct._new(mem, base_addr)
        else:
            return None

    @property
    def heap_chunks(self):
        '''list(C_Struct): list of heap chunks.
        '''
        if self.heap_base == 0:
            # just a refresh in case that heap_base = 0
            self.heap_base = self.proc.bases['heap']
        heap_mem = self.heapmem
        cur_pos = 0
        # check if there is an alignment at the start of the heap.
        first_chunk_size = self.unpack(self.heapmem[self.size_t:self.size_t *
                                                    2])
        if first_chunk_size == 0:
            cur_pos += 2 * self.size_t
        result = []
        while cur_pos < len(heap_mem):
            cur_block_size = self.unpack(
                heap_mem[cur_pos + self.size_t:cur_pos +
                         2 * self.size_t]) & ~0b111
            memblock = heap_mem[cur_pos:cur_pos + cur_block_size]
            result.append(
                self.MallocChunk._new(memblock, cur_pos + self.heap_base))
            if self.arch == '64':
                cur_pos = (cur_pos + cur_block_size) & ~0b1111
            elif self.arch == '32':
                cur_pos = (cur_pos + cur_block_size) & ~0b111
            if cur_block_size < 2 * self.size_t:
                break
        return result

    @property
    def tcache_chunks(self):
        '''dict: dict of tcache_chunks.
        '''
        if not self.tcache_enable:
            return {}
        result = {}
        for index, entry_ptr in enumerate(self.tcache.entries):
            lst = []
            tranversed = []
            while entry_ptr:
                mem = self.proc.read(entry_ptr - 2 * self.size_t,
                                     4 * self.size_t)
                chunk = self.MallocChunk._new(mem, entry_ptr - 2 * self.size_t)
                lst.append(chunk)
                entry_ptr = chunk.fd
                if entry_ptr in tranversed:
                    break
                else:
                    tranversed.append(entry_ptr)
            if lst != []:
                result[index] = lst
        return result

    @property
    def fastbins(self):
        '''dict: dict of fastbins.
        '''
        result = {}
        for index, fastbin_head in enumerate(self.main_arena.fastbinsY):
            fastbin_ptr = fastbin_head
            lst = []
            tranversed = []
            while fastbin_ptr:
                mem = self.proc.read(fastbin_ptr, 4 * self.size_t)
                chunk = self.MallocChunk._new(mem, fastbin_ptr)
                lst.append(chunk)
                fd = chunk.fd
                fastbin_ptr = fd
                if fastbin_ptr in tranversed:
                    break
                else:
                    tranversed.append(fastbin_ptr)
            if lst != []:
                result[index] = lst
        return result

    @property
    def unsortedbins(self):
        '''list: list of unsortedbins.
        '''
        result = self.bins(0, 1)
        if result != {}:
            return result[0]
        else:
            return {}

    def bins(self, start=0, end=127, chunk_size=0x20):
        '''Generate bins of the arena.

        Note:
            malloc_state has 127 bins. bins[0] is unsortedbins,
            bins[1] - bins[62] are smallbins,
            bins[63] - bins[126] are largebins.
        Args:
            strat (:obj:`int`, optional): Start positions.
        '''
        result = {}
        for index in range(start, end):
            lst = []
            tranversed = []
            head_chunk_addr = self.main_arena._addrof('bins[{}]'.format(
                index * 2)) - 2 * self.size_t

            chunk_ptr = head_chunk_addr
            chunk = self.MallocChunk._new(
                self.proc.read(chunk_ptr, chunk_size), chunk_ptr)
            while chunk.bk != head_chunk_addr:
                chunk_ptr = chunk.bk
                chunk = self.MallocChunk._new(
                    self.proc.read(chunk_ptr, chunk_size), chunk_ptr)
                lst.append(chunk)
                if chunk.bk in tranversed:
                    break
                else:
                    tranversed.append(chunk.bk)
            if lst != []:
                result[index] = lst
        return result

    @property
    def smallbins(self):
        '''dict: dict of smallbins.
        '''
        return self.bins(1, 63)

    @property
    def largebins(self):
        '''dict: dict of largebins.
        '''
        return self.bins(63, 127, 0x30)

    @property
    def record(self):
        '''HeapRecord: A record.
        '''
        return HeapRecord(self)
예제 #13
0
def do_():
    path = easygui.fileopenbox()
    p = Parser2(path)
    p.parse()
    print("Данные загружены. ")
    # p.d_print()
    print("--------")

    THETA = int(path[len(path) - 11:len(path) - 9])
    theta = (math.pi / 180) * THETA
    '''
    model = Model(Temperature=15, Pressure=1013, Rho=7.5)
    print("Q и W для условий стандартной атмосферы (зенит), по встроенной модели:")
    q, w = model.get_QW_model_zenith(freq1=21, freq2=27, T_obl=-2)
    print("Q = ", q, "\tW = ", w, "\n--------\n")
    print("Q и W для условий стандартной атмосферы (theta = ", theta, "рад.), по встроенной модели:")
    q, w = model.get_QW_model_(freq1=21, freq2=27, T_obl=-2, theta=theta)
    print("Q = ", q, "\tW = ", w, "\n--------\n")
    '''

    model_ = Model(Temperature=15, Pressure=768 * 1.33322, Rho=7.5)
    pc = Proc(p.data,
              model_,
              T_avg=2.3,
              T_obl=-2,
              v_wind=None,
              time_interval=None)

    print("Температура ", model_.T, "цельс.,\tДавление ", model_.P,
          "гПа,\tАбс. влажность ", model_.rho, "г/м^3")
    print("T_avg ", pc.T_avg, "цельс.,\tТемпература облака ", pc.T_obl,
          "цельс.")
    print("Скорость ветра ", pc.wind, "км/ч,\tВременной интервал (период) ",
          pc.time, "ч")
    print("Начальная координата ", pc.start, "км")
    print("\nTHETA = ", THETA, "град.,\t", theta, "рад.")

    print("cos(Theta) = ", math.cos(theta))
    print("int_rho = ", model_.get_Q())

    # pc.set_T_t0_model_Tb(theta)
    pc.set_T_t0_Tb_avg()

    plt.figure(1)
    plt.title(u"Поглощение в кислороде")
    Freqs, Tbs = [], []
    for f in np.arange(18.0, 27.2, 0.2):
        Freqs.append(f)
        Tbs.append(model_.tauO_theory(f, theta))
    plt.plot(Freqs, Tbs, "r+")

    plt.figure(2)
    plt.title(u"Поглощение в водяном паре")
    Freqs, Tbs = [], []
    for f in np.arange(18.0, 27.2, 0.2):
        Freqs.append(f)
        Tbs.append(model_.tauRho_theory(f, theta))
    plt.plot(Freqs, Tbs, "r+")

    plt.figure(3)
    plt.title(
        u"$T_{b}$ - модельные значения (безоблачная атм.)\n Температура " +
        str(model_.T) + u" °C, Давление " + str(int(model_.P / 1.33322)) +
        u" мм.рт.ст., Абс. влажность " + str(model_.rho) + u" $g/m^{3}$")
    Freqs, Tbs = [], []
    for f in np.arange(18.0, 27.2, 0.2):
        Freqs.append(f)
        Tbs.append(model_.get_Tb(f, pc.T_avg, theta=theta))
    plt.plot(Freqs, Tbs, "r+")
    plt.show()
    plt.close()

    print("\n==== Средние за период яркостные температуры: ====")
    print("Здесь и далее точки {T_ярк. = 0} пропускаются")
    A = pc.get_Tb_avg()
    B = pc.get_calibr_Tb_avg(theta)
    print("18.7 ГГц:", A[18.7])
    print("Calibr.  :", B[18.7])
    print("19.1 ГГц:", A[19.1])
    print("Calibr.  :", B[19.1])
    print("21.1 ГГц:", A[21.1])
    print("Calib.  :", B[21.1])
    print("21.7 ГГц:", A[21.7])
    print("Calibr.  :", B[21.7])
    print("23.9 ГГц:", A[23.9])
    print("Calibr.  :", B[23.9])
    print("24.1 ГГц:", A[24.1])
    print("Calibr.  :", B[24.1])
    print("26.9 ГГц:", A[26.9])
    print("Calibr.  :", B[26.9])
    Freqs = [18.7, 19.1, 21.1, 21.7, 23.9, 24.1, 26.9]
    Tbs_model, Tbs_given, Tbs_calibr = [], [], []
    for freq in Freqs:
        Tbs_model.append(model_.get_Tb(freq, pc.T_avg, theta))
        Tbs_given.append(A[freq])
        Tbs_calibr.append(B[freq])
    plt.plot(Freqs, Tbs_model, "b+", label='Model')
    plt.plot(Freqs, Tbs_given, "g+", label='Data')
    plt.plot(Freqs, Tbs_calibr, "r+", label='Calibr')
    plt.legend()
    plt.show()

    print("\n----Двухчастотный метод определения Q и W----")
    print("(По средним за период ярк. температурам)")
    print("freq1 = 18.7 ГГц, \tfreq2 = 21.7 ГГц")
    q, w = pc.get_QW4Tb_avg(freq1=18.7, freq2=21.7, theta=theta)
    print("Q = ", q, "\t W = ", w)

    print("freq1 = 18.7 ГГц, \tfreq2 = 23.9 ГГц")
    q, w = pc.get_QW4Tb_avg(freq1=18.7, freq2=23.9, theta=theta)
    print("Q = ", q, "\t W = ", w)

    print("freq1 = 21.7 ГГц, \tfreq2 = 26.9 ГГц")
    q, w = pc.get_QW4Tb_avg(freq1=21.7, freq2=26.9, theta=theta)
    print("Q = ", q, "\t W = ", w)

    print("freq1 = 23.9 ГГц, \tfreq2 = 26.9 ГГц")
    q, w = pc.get_QW4Tb_avg(freq1=23.9, freq2=26.9, theta=theta)
    print("Q = ", q, "\t W = ", w)

    print("\n----Оптимизационный метод----")
    print("(По средним за период ярк. температурам)")
    print("4 частоты: 18.7, 21.7, 23.9, 26.9 ГГц")
    frequencies = [18.7, 21.7, 23.9, 26.9]
    q, w = pc.get_opt_QW4Tb_avg(frequencies, theta=theta)
    print("\nQ = ", q, "\t W = ", w)

    print("\n==== Средние за период Q и W ====")
    print("----Двухчастотный метод----")
    print("(По всем ярк. температурам)")
    print("freq1 = 18.7 ГГц, \tfreq2 = 21.7 ГГц")
    q, w = pc.get_QW_avg(freq1=18.7, freq2=21.7, theta=theta)
    print("Q = ", q, "\t W = ", w)

    print("freq1 = 18.7 ГГц, \tfreq2 = 23.9 ГГц")
    q, w = pc.get_QW_avg(freq1=18.7, freq2=23.9, theta=theta)
    print("Q = ", q, "\t W = ", w)

    print("freq1 = 21.7 ГГц, \tfreq2 = 26.9 ГГц")
    q, w = pc.get_QW_avg(freq1=21.7, freq2=26.9, theta=theta)
    print("Q = ", q, "\t W = ", w)

    print("freq1 = 23.9 ГГц, \tfreq2 = 26.9 ГГц")
    q, w = pc.get_QW_avg(freq1=23.9, freq2=26.9, theta=theta)
    print("Q = ", q, "\t W = ", w)
    '''
예제 #14
0
class GDB():
    def __init__(self, command, *args, **kwargs):
        self.command = command
        self.proc = Proc("LC_ALL=C gdb --args " + command, shell=True)
        self.debugf = open('debug.log', 'w')
        self.compilation_directory = ''
        self.sourcefile = ''
        self.sources = []
        self.breakpoints = {}
        self.lineno = -1
        self.cached_stdout = ''
        self.cached_stderr = ''
        while 1:
            time.sleep(0.01)
            if self.proc.canread(self.proc.stdout()): break
            if self.proc.canread(self.proc.stderr()): break
        s, t = self.read()
        # disable getting the output split into single pages and interacting
        self.send('set pagination off')
        self.read()
        # disable printing of strings interrupted by <repeats x times>
        self.send('set print repeats 0xfffffffe')
        self.read()
        master, slave = pty.openpty()
        self.send('set inferior-tty ' + os.ttyname(slave))
        self.read()
        self.pty_master = master
        self.pty_slave = slave
        self._reload_sources()
        self.cached_stdout = s
        self.cached_stderr = t

    def add_bp(self, file, line):
        file = self.find_sourcefile(file)
        if not file in self.breakpoints: self.breakpoints[file] = []
        self.breakpoints[file].append(line)

    def _set_exit_code(self, ec):
        self.proc.exitcode = ec

    def _reload_sources(self):
        self.send('info sources')
        s = self.raw_read('stdout')
        lines = s.split('\n')
        self.sources = []
        for line in lines:
            if line.startswith('Source files for'): continue
            line = line.rstrip('\n')
            if line == '': continue
            items = line.split(', ')
            for item in items:
                self.sources.append(item)

    def set_sourcefile(self, file):
        if len(file) and not file.startswith('/'):
            file = self.compilation_directory + '/' + file
        self.debug("sourcefile " + file)
        self.sourcefile = file

    def find_sourcefile_single(self, file):
        full = None
        if len(file) and not file.startswith('/'):
            full = self.compilation_directory + '/' + file
        for s in self.sources:
            if s == file or (full and s == full): return s
        if full:
            tmp = '/' + file
            for s in self.sources:
                if s.endswith(tmp): return s
        return None

    def find_sourcefile(self, file):
        if self.sourcefile == file:
            return file
        if not file.startswith(
                '/'
        ) and self.compilation_directory + '/' + file == self.sourcefile:
            return self.sourcefile
        s = self.find_sourcefile_single(file)
        if s: return s
        self._reload_sources()
        s = self.find_sourcefile_single(file)
        return s

    def _consume_cached(self, which):
        if which == 'stdout':
            res = self.cached_stdout
            self.cached_stdout = ''
        else:
            res = self.cached_stderr
            self.cached_stderr = ''
        return res

    def istdout(self):
        return self.pty_master

    def istdout_canread(self):
        return self.proc.canread(self.pty_master)

    def debug(self, text):
        self.debugf.write(text + '\n')
        self.debugf.flush()

    def raw_read(self, filename):
        if self.get_exitcode() is not None: return ''
        s = self._consume_cached(filename)
        if filename == 'stdout':
            f = self.proc.stdout()
        else:
            f = self.proc.stderr()
        while self.proc.canread(f):
            c = f.read(1)
            if not c: break
            s += c
        return s

    def read(self):
        s = self.raw_read('stdout')
        #if '[Inferior 1 (process 19071) exited normally]
        # [Inferior 1 (process 19224) exited with code 01]
        lines = s.split('\n')
        for l in lines:
            if l.startswith('The program is not being run.'):
                self._set_exit_code(-3)
            if l.startswith('[Inferior 1 (process '):
                if l.endswith('exited normally]'): self.proc.exitcode = 0
                if ') exited with code ' in l and l.endswith(']'):
                    self._set_exit_code(int(l.split(' ')[-1].rstrip(']'), 10))
        if not self.proc.exitcode and lines[-1] != '(gdb) ':
            s += self.proc.read_until(self.proc.stdout(), '(gdb) ')
        lines = s.split('\n')
        for l in lines:
            self.debug('L ' + l)
            if l.startswith('Breakpoint ') or l.startswith(
                    'Temporary breakpoint '):
                a = l.split(' ')
                le = a[-1]
                self.debug(repr(a))
                file = None
                if le.find(':') == -1:
                    # dont have a file:lineno tuple at the end, it's the confirmation a breakpoint has been set
                    if len(a) > 4 and a[-4] == u'file' and a[-2] == u'line':
                        file = a[-3][:-1]
                        lineno = a[-1][:-1]
                    if not l.startswith('Temporary'):
                        self.add_bp(file, int(lineno))
                    file = None
                else:
                    file, lineno = le.split(':')
                if file is not None:
                    self.set_sourcefile(self.find_sourcefile(file))
                    self.lineno = int(lineno)

        t = self.raw_read('stderr')
        lines = t.split('\n')
        for l in lines:
            if len(l): self.debug('E ' + l)
            if l.startswith('During symbol reading, incomplete CFI data'):
                self._set_exit_code(-1)
            if l.startswith('Cannot find bounds of current function'):
                self._set_exit_code(-2)
        return s, t

    def get_exitcode(self):
        return self.proc.exitcode

    def send(self, command):
        if self.get_exitcode() is not None: return
        self.proc.stdin().write(command + '\n')
        time.sleep(0.0001)

    def set_source_from_ouput(self, s):
        a = s.split('\n')
        for x in a:
            if x.startswith('Located in '):
                self.sourcefile = x[len('Located in '):]
            elif x.startswith('Compilation directory is '):
                self.compilation_directory = x[len('Compilation directory is '
                                                   ):]
        return self.sourcefile
예제 #15
0
 def spawn(self, env, addr):
     proc = Proc(self)
     self.procs.register(proc)
     proc.frame(env, addr)
     return proc
예제 #16
0
def do_():
    rc('font', **{'family': 'serif'})
    rc('text', usetex=True)
    rc('text.latex', unicode=True)
    rc('text.latex', preamble=r"\usepackage[T2A]{fontenc}")
    rc('text.latex', preamble=r"\usepackage[utf8]{inputenc}")
    rc('text.latex', preamble=r"\usepackage[russian]{babel}")

    path = easygui.fileopenbox()
    p = Parser1("tb_timeP", path)
    DATA = p.parse()

    p.shift_tb(DATA)

    QW_freqs = [18, 21, 22, 27]
    freqs2show = [18, 19.2, 20.4, 21.6, 22.2, 22.4, 23.2, 24.4, 25.6, 26.8]

    Drawer().draw(DATA,
                  freqs2show,
                  title=u"$T_{b}$ - без калибровки (экспериментальные данные)",
                  xlabel=u"время (сек.)",
                  ylabel="")

    config_path = easygui.fileopenbox()
    info = Configer(config_path)
    info.get_info()

    # ==================================================================
    theta = info.theta
    model_ = Model(Temperature=info.T, Pressure=info.P, Rho=info.rho)
    proc = Proc(DATA,
                model_,
                T_avg=info.Tavg,
                T_obl=info.Tobl,
                start_coordinate=info.start,
                stop_coordinate=info.stop,
                is_data_calibred=False)
    # ==================================================================

    plt.title("")
    Freqs, TauO, TauH2O = [], [], []
    for f in np.arange(5, 350, 0.5):
        Freqs.append(f)
        TauO.append(model_.tauO_theory(f, theta=51) / model_.dB2np)
        TauH2O.append(model_.tauRho_theory(f, theta=51) / model_.dB2np)
    plt.plot(Freqs, TauO, label=u"Кислород", color="red")
    plt.plot(Freqs, TauH2O, label=u"Водяной пар", color="blue")
    ax = plt.axes()
    ax.set_xscale("log", nonposx='clip')
    ax.set_yscale("log", nonposy='clip')
    plt.xlabel(u"ГГц")
    plt.ylabel(u"Дб")
    plt.legend(loc="best")
    plt.show()
    plt.close()

    plt.title(
        u"$T_{b}$ - модельные значения (безоблачная атм.)\n Температура " +
        str(model_.T) + u" °C, Давление " + str(int(model_.P / 1.33322)) +
        u" мм.рт.ст., Абс. влажность " + str(model_.rho) + u" $g/m^{3}$")
    FREQS, TBR, TBRnQ = [], [], []
    for freq in np.arange(18, 27.2, 0.2):
        T_br = model_.get_Tb_Q(freq, proc.T_avg, theta)
        Tbrnq = model_.get_Tb(freq, proc.T_avg, theta)
        FREQS.append(freq)
        TBR.append(T_br)
        TBRnQ.append(Tbrnq)
    Q = model_.get_Q()
    plt.plot(FREQS, TBRnQ, "b+", label=u'$T_{b}$ : $T_{avg}(1-e^{-tau})$')
    plt.plot(FREQS,
             TBR,
             "r+",
             label=u'$T_{b}$ :  Q = ' + str(Q) + u' $g/m^{2}$')
    print("Q_model = ", Q)
    plt.xlabel(u'ГГц')
    plt.ylabel(u'K')
    plt.legend()
    plt.show()
    plt.close()

    plt.title(u"Поглощение в кислороде - модель (безобл. атм.)")
    Freqs, Tbs = [], []
    for f in np.arange(18.0, 27.2, 0.2):
        Freqs.append(f)
        Tbs.append(model_.tauO_theory(f, theta))
    plt.plot(Freqs, Tbs, "r+")
    plt.xlabel(u"ГГц")
    plt.ylabel(u"непер")
    plt.show()
    plt.close()

    plt.title(u"Поглощение в водяном паре - модель (безобл. атм.)")
    Freqs, Tbs = [], []
    for f in np.arange(18.0, 27.2, 0.2):
        Freqs.append(f)
        Tbs.append(model_.tauRho_theory(f, theta))
    plt.plot(Freqs, Tbs, "r+")
    plt.xlabel(u"ГГц")
    plt.ylabel(u"непер")
    plt.show()
    plt.close()

    plt.title("$k_{w}$")
    Freqs, K = [], []
    for f in np.arange(18.0, 27.2, 0.01):
        Freqs.append(f)
        K.append(model_.kw(f, -2))
    plt.plot(Freqs, K)
    plt.xlabel(u"ГГц")
    plt.ylabel(u"Значение")
    plt.show()
    plt.close()

    print(info.nclbeg, info.nclend)
    proc.set_T_t0_nocluds_interval(time_begin=info.nclbeg,
                                   time_end=info.nclend)
    proc.calibr_Tb(theta=theta)

    Drawer().draw(proc.data,
                  freqs2show,
                  title=u"$T_{b}$ - яркостные температуры",
                  xlabel=u"время",
                  ylabel=u"температура [K]")

    # ==================================================================== #
    # ======================= Generate DataSet =========================== #
    # ==================================================================== #
    proc.generate_dataset("QW__f1_18GHz__f2_21GHz.txt",
                          freq1=18,
                          freq2=21,
                          theta=theta)
    proc.generate_dataset("QW__f1_18GHz__f2_22GHz.txt",
                          freq1=18,
                          freq2=22,
                          theta=theta)
    proc.generate_dataset("QW__f1_21GHz__f2_27GHz.txt",
                          freq1=21,
                          freq2=27,
                          theta=theta)
    proc.generate_dataset("QW__f1_22GHz__f2_27GHz.txt",
                          freq1=22,
                          freq2=27,
                          theta=theta)
    # ==================================================================== #

    QW = defaultdict(list)
    QW["18, 21"] = proc.get_resolved_QW(freq1=18, freq2=21, theta=theta)
    QW["18, 22"] = proc.get_resolved_QW(freq1=18, freq2=22, theta=theta)
    QW["21, 27"] = proc.get_resolved_QW(freq1=21, freq2=27, theta=theta)
    QW["22, 27"] = proc.get_resolved_QW(freq1=22, freq2=27, theta=theta)
    # QW["18, 27"] = proc.get_resolved_QW(freq1=18, freq2=27, theta=theta)
    # QW["18, 27"] = proc.get_QW(freq1=18, freq2=27, theta=theta)

    QW_err = defaultdict(list)
    QW_err["18, 21"] = proc.get_QW_errors(freq1=18,
                                          freq2=21,
                                          theta=theta,
                                          Tbr_error=1,
                                          Tavg_error=2)
    QW_err["18, 22"] = proc.get_QW_errors(freq1=18,
                                          freq2=22,
                                          theta=theta,
                                          Tbr_error=1,
                                          Tavg_error=2)
    QW_err["21, 27"] = proc.get_QW_errors(freq1=21,
                                          freq2=27,
                                          theta=theta,
                                          Tbr_error=1,
                                          Tavg_error=2)
    QW_err["22, 27"] = proc.get_QW_errors(freq1=22,
                                          freq2=27,
                                          theta=theta,
                                          Tbr_error=1,
                                          Tavg_error=2)
    # QW_err["18, 27"] = proc.get_QW_errors(freq1=18, freq2=27, theta=theta, Tbr_error=1, Tavg_error=2)

    plt.figure(1)
    plt.title(u"Q - полная масса водяного пара")
    k = 0
    for key in QW.keys():
        TIME, Q = [], []
        dTIME, dQ, dQ_err = [], [], []
        i = 0
        for time, q, _ in QW[key]:
            TIME.append(time)
            Q.append(q)
            if (i - k) % 100 == 0:
                dTIME.append(time)
                _, dq, _ = QW_err[key][i]
                dQ.append(q)
                dq = dq / 10  #!!!
                dQ_err.append(dq)
            i += 1
        plt.plot(TIME, Q, label=key + u" ГГц")
        ecolor = ""
        if k == 0: ecolor = "blue"
        if k == 25: ecolor = "orange"
        if k == 50: ecolor = "green"
        if k == 75: ecolor = "red"
        if k == 100: ecolor = "purple"
        # plt.errorbar(dTIME, dQ, yerr=dQ_err, fmt='o', ecolor="black", mfc=ecolor, mec=ecolor, ms=2, mew=3, capsize=2, capthick=3, elinewidth=3)
        k += 25
    plt.xlabel(u"время")
    plt.ylabel("$g/cm^{2}$")
    axes = plt.gca()
    # axes.set_ylim([1., 2.5])
    plt.legend()

    plt.figure(2)
    plt.title(u"W - водозапас облаков")
    k = 0
    for key in QW.keys():
        TIME, W = [], []
        dTIME, dW, dW_err = [], [], []
        i = 0
        for time, _, w in QW[key]:
            TIME.append(time)
            w = w * 10
            W.append(w)
            if (i - k) % 100 == 0:
                dTIME.append(time)
                _, _, dw = QW_err[key][i]
                dW.append(w)
                dW_err.append(dw)
            i += 1
        plt.plot(TIME, W, label=key + u" ГГц")
        ecolor = ""
        if k == 0: ecolor = "blue"
        if k == 25: ecolor = "orange"
        if k == 50: ecolor = "green"
        if k == 75: ecolor = "red"
        if k == 100: ecolor = "purple"
        # plt.errorbar(dTIME, dW, yerr=dW_err, fmt='o', ecolor="black", mfc=ecolor, mec=ecolor, ms=2, mew=3, capsize=2, capthick=3, elinewidth=3)
        k += 25
    axes = plt.gca()
    # axes.set_ylim([-0.5, 1])
    plt.xlabel(u"время")
    plt.ylabel("$kg/m^{2}$")
    plt.legend()

    plt.show()

    QW_opt = proc.get_opt_QW(QW_freqs, theta)

    plt.figure(1)
    TIME, Q, W = [], [], []
    for time, q, w in QW_opt:
        TIME.append(time)
        Q.append(q)
        W.append(w * 10)
    plt.plot(TIME, Q, color="red", label="Q")
    plt.plot(TIME, W, color="blue", label="W")
    plt.xlabel(u"время")
    plt.ylabel("Q [$g/cm^{2}$], W [$kg/m^{2}$]")
    plt.legend()
    '''
    plt.figure(3)
    plt.title("dQ")
    for key in QW_err:
        TIME, dQ = [], []
        for time, dq, _ in QW_err[key]:
            TIME.append(time)
            dQ.append(dq)
        plt.plot(TIME, dQ, label=key + " GHz")
    plt.xlabel("time")
    plt.ylabel("$value$")
    plt.legend()

    plt.figure(4)
    plt.title("dW")
    for key in QW_err:
        TIME, dW = [], []
        for time, _, dw in QW_err[key]:
            TIME.append(time)
            dW.append(dw)
        plt.plot(TIME, dW, label=key + " GHz")
    plt.xlabel("time")
    plt.ylabel("$value$")
    plt.legend()
    '''

    plt.show()
    '''
    plt.title("")
    TIME, TAU1821, TAU1822, TAU2721, TAU2722, TAU1827 = [], [], [], [], [], []
    min_len = len(proc.data[18])
    for f in [18, 21, 22, 27]:
        if len(proc.data[f]) < min_len: min_len = len(proc.data[f])
    for i in range(min_len):
        t18, Tb18 = proc.data[18][i]
        t21, Tb21 = proc.data[21][i]
        t22, Tb22 = proc.data[22][i]
        t27, Tb27 = proc.data[27][i]
        
        tau18 = model_.tau_experiment(Tb18, proc.T_avg + 273, theta)
        tau21 = model_.tau_experiment(Tb21, proc.T_avg + 273, theta)
        tau22 = model_.tau_experiment(Tb22, proc.T_avg + 273, theta)
        tau27 = model_.tau_experiment(Tb27, proc.T_avg + 273, theta)
        
        tau18 = model_.tauRho_theory(18, theta)
        tau21 = model_.tauRho_theory(21, theta)
        tau22 = model_.tauRho_theory(22, theta)
        tau27 = model_.tauRho_theory(27, theta)
        
        
        theta = 0
        tau1821 = math.fabs(model_.krho(18, theta) * model_.kw(21, -2) - model_.krho(21, theta) * model_.kw(18, -2))
        tau1822 = math.fabs(model_.krho(18, theta) * model_.kw(22, -2) - model_.krho(22, theta) * model_.kw(18, -2))
        tau2721 = math.fabs(model_.krho(21, theta) * model_.kw(27, -2) - model_.krho(27, theta) * model_.kw(21, -2))
        tau2722 = math.fabs(model_.krho(22, theta) * model_.kw(27, -2) - model_.krho(27, theta) * model_.kw(22, -2))
        tau1827 = math.fabs(model_.krho(18, theta) * model_.kw(27, -2) - model_.krho(27, theta) * model_.kw(18, -2))

        t = (t18 + t21 + t22 +t27) / 4
        TIME.append(t)
        TAU1821.append(tau1821)
        TAU1822.append(tau1822)
        TAU1827.append(tau1827)
        TAU2721.append(tau2721)
        TAU2722.append(tau2722)
    plt.plot(TIME, TAU1821, label="18 - 21")
    plt.plot(TIME, TAU1822, label="18 - 22")
    plt.plot(TIME, TAU2721, label="21 - 27")
    plt.plot(TIME, TAU2722, label="22 - 27")
    plt.plot(TIME, TAU1827, label="18 - 27")
    plt.legend()
    plt.show()

    print("18-21", TAU1821[0])
    print("18-22", TAU1822[0])
    print("21-27", TAU2721[0])
    print("22-27", TAU2722[0])
    print("18-27", TAU1827[0])
    '''

    print("QW avg's - no clouds interval")
    avgQ, avgW = 0, 0
    qavg, wavg = proc.get_QW_err_avg_noclouds(18, 21, theta, 1, 2)
    avgQ += math.fabs(qavg / 10)
    avgW += math.fabs(wavg)
    print("18-21:\tQ = ", qavg / 10, "\tW = ", wavg)
    qavg, wavg = proc.get_QW_err_avg_noclouds(18, 22, theta, 1, 2)
    avgQ += math.fabs(qavg / 10)
    avgW += math.fabs(wavg)
    print("18-22:\tQ = ", qavg / 10, "\tW = ", wavg)
    qavg, wavg = proc.get_QW_err_avg_noclouds(21, 27, theta, 1, 2)
    avgQ += math.fabs(qavg / 10)
    avgW += math.fabs(wavg)
    print("21-27:\tQ = ", qavg / 10, "\tW = ", wavg)
    qavg, wavg = proc.get_QW_err_avg_noclouds(22, 27, theta, 1, 2)
    avgQ += math.fabs(qavg / 10)
    avgW += math.fabs(wavg)
    print("22-27:\tQ = ", qavg / 10, "\tW = ", wavg)
    avgQ /= 4
    avgW /= 4
    print("AVG:\tQ = ", avgQ, "\tW = ", avgW)

    print("QW avg's - clouds interval")
    avgQ, avgW = 0, 0
    qavg, wavg = proc.get_QW_err_avg_clouds(18, 21, theta, 1, 2)
    avgQ += math.fabs(qavg / 10)
    avgW += math.fabs(wavg)
    print("18-21:\tQ = ", qavg / 10, "\tW = ", wavg)
    qavg, wavg = proc.get_QW_err_avg_clouds(18, 22, theta, 1, 2)
    avgQ += math.fabs(qavg / 10)
    avgW += math.fabs(wavg)
    print("18-22:\tQ = ", qavg / 10, "\tW = ", wavg)
    qavg, wavg = proc.get_QW_err_avg_clouds(21, 27, theta, 1, 2)
    avgQ += math.fabs(qavg / 10)
    avgW += math.fabs(wavg)
    print("21-27:\tQ = ", qavg / 10, "\tW = ", wavg)
    qavg, wavg = proc.get_QW_err_avg_clouds(22, 27, theta, 1, 2)
    avgQ += math.fabs(qavg / 10)
    avgW += math.fabs(wavg)
    print("22-27:\tQ = ", qavg / 10, "\tW = ", wavg)
    avgQ /= 4
    avgW /= 4
    print("AVG:\tQ = ", avgQ, "\tW = ", avgW)