]> sourceware.org Git - systemtap.git/commitdiff
More refactoring for multiple stap processes.
authorTim Moore <timoore@redhat.com>
Wed, 21 Oct 2009 15:05:59 +0000 (17:05 +0200)
committerTim Moore <timoore@redhat.com>
Tue, 27 Oct 2009 12:00:59 +0000 (13:00 +0100)
* grapher/StapParser.hxx (StapParser): Change _win and _widget from
  references to pointers.
* grapher/StapParser.cxx (ioCallback): Ditto.
* grapher/grapher.cxx (StapLauncher, GraphicalStapLauncher): Rewrite
  to make GraphicalStapLauncher a derived class of StapLauncher.
  (main): Accept graphing data from stdin with a "-" argument.

grapher/StapParser.cxx
grapher/StapParser.hxx
grapher/grapher.cxx

index eea85006759dc71044563f1f75b2de68b0033159..249836d31d444ce121b2f76401b07c191897840a 100644 (file)
@@ -63,7 +63,7 @@ vector<string> commaSplit(const boost::sub_range<Glib::ustring>& range)
       using namespace boost;
       if (ioCondition & Glib::IO_HUP)
         {
-          _win.hide();
+          _win->hide();
           return true;
         }
       if ((ioCondition & Glib::IO_IN) == 0)
@@ -73,7 +73,7 @@ vector<string> commaSplit(const boost::sub_range<Glib::ustring>& range)
       bytes_read = read(_inFd, buf, sizeof(buf) - 1);
       if (bytes_read <= 0)
         {
-          _win.hide();
+          _win->hide();
           return true;
         }
       buf[bytes_read] = '\0';
@@ -108,7 +108,7 @@ vector<string> commaSplit(const boost::sub_range<Glib::ustring>& range)
                       dataSet->color[2] = (hexColor & 0xff) / 255.0;
                       dataSet->scale = scale;
                       _dataSets.insert(std::make_pair(setName, dataSet));
-                      _widget.addGraphData(dataSet);
+                      _widget->addGraphData(dataSet);
                     }
                   else if (style == "discreet")
                     {
@@ -120,7 +120,7 @@ vector<string> commaSplit(const boost::sub_range<Glib::ustring>& range)
                       dataSet->color[2] = (hexColor & 0xff) / 255.0;
                       dataSet->scale = scale;
                       _dataSets.insert(std::make_pair(setName, dataSet));
-                      _widget.addGraphData(dataSet);
+                      _widget->addGraphData(dataSet);
                     }
                 }
               else if ((found = find_first(dataString, "%CSV:")))
@@ -219,7 +219,7 @@ vector<string> commaSplit(const boost::sub_range<Glib::ustring>& range)
     bytes_read = read(_errFd, buf, sizeof(buf) - 1);
     if (bytes_read <= 0)
       {
-        _win.hide();
+        _win->hide();
         return true;
       }
     if (write(STDOUT_FILENO, buf, bytes_read) < 0)
index eeebed632cb9048cfea65fbe6d9383b434270305..40add9fda4d36b65498dce01063347cd80da8cc1 100644 (file)
@@ -10,13 +10,13 @@ class StapParser
   typedef std::map<std::string, std::tr1::shared_ptr<GraphDataBase> > DataMap;
   DataMap _dataSets;
   CSVData _csv;
-  Gtk::Window& _win;
-  GraphWidget& _widget;
+  Gtk::Window* _win;
+  GraphWidget* _widget;
   int _errFd;
   int _inFd;
 public:
-  StapParser(Gtk::Window& win,
-             GraphWidget& widget) : _win(win), _widget(widget), _errFd(-1),
+  StapParser(Gtk::Window* win,
+             GraphWidget* widget) : _win(win), _widget(widget), _errFd(-1),
                                     _inFd(-1)
   {
   }
index 398b35f1b9713cb71bc7975e2a45904ce4e89a0d..598f389b51439c64e4885db54d4ad2b28bd88654 100644 (file)
@@ -10,6 +10,7 @@
 #include <sstream>
 #include <string>
 #include <map>
+#include <memory>
 #include <vector>
 
 #include <signal.h>
@@ -31,6 +32,20 @@ using namespace std;
 
 using namespace systemtap;
 
+// magic for noticing that the child stap process has died.
+int signalPipe[2] = {-1, -1};
+
+extern "C"
+{
+  void handleChild(int signum, siginfo_t* info, void* context)
+  {
+    char buf[1];
+    ssize_t err;
+    buf[0] = 1;
+    err = write(signalPipe[1], buf, 1);
+  }
+}
+
 // Waits for a gtk I/O signal, indicating that a child has died, then
 // performs an action
 
@@ -63,143 +78,19 @@ private:
   int sigfd;
 };
 
-class StapLauncher;
-
-class GraphicalStapLauncher
-{
-public:
-  GraphicalStapLauncher(StapLauncher* launcher);
-  bool runDialog();
-  void onLaunch();
-  void onLaunchCancel();
-private:
-  Glib::RefPtr<Gnome::Glade::Xml> _launchStapDialog;
-  Gtk::Window* _scriptWindow;
-  Gtk::FileChooserButton* _chooserButton;
-  Gtk::Entry* _stapArgEntry;
-  Gtk::Entry* _scriptArgEntry;
-  StapLauncher* _launcher;
-};
-
-class GrapherWindow : public Gtk::Window, public ChildDeathReader::Callback
-{
-public:
-  GrapherWindow();
-  virtual ~GrapherWindow() {}
-  Gtk::VBox m_Box;
-  Gtk::ScrolledWindow scrolled;
-  GraphWidget w;
-  void childDied(int pid);
-  void setGraphicalLauncher(GraphicalStapLauncher* launcher)
-  {
-    _graphicalLauncher = launcher;
-  }
-  GraphicalStapLauncher* getGraphicalLauncher() { return _graphicalLauncher; }
-protected:
-  virtual void on_menu_file_quit();
-  virtual void on_menu_script_start();
-  void addGraph();
-  // menu support
-  Glib::RefPtr<Gtk::UIManager> m_refUIManager;
-  Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup;
-  GraphicalStapLauncher* _graphicalLauncher;
-
-};
-
-GrapherWindow::GrapherWindow()
-{
-  set_title("systemtap grapher");
-  add(m_Box);
-
-
-  //Create actions for menus and toolbars:
-  m_refActionGroup = Gtk::ActionGroup::create();
-  //File menu:
-  m_refActionGroup->add(Gtk::Action::create("FileMenu", "File"));
-  m_refActionGroup->add(Gtk::Action::create("StartScript", "Start script"),
-                        sigc::mem_fun(*this,
-                                      &GrapherWindow::on_menu_script_start));
-  m_refActionGroup->add(Gtk::Action::create("AddGraph", "Add graph"),
-                        sigc::mem_fun(*this, &GrapherWindow::addGraph));
-  m_refActionGroup->add(Gtk::Action::create("FileQuit", Gtk::Stock::QUIT),
-                        sigc::mem_fun(*this,
-                                      &GrapherWindow::on_menu_file_quit));
-  m_refUIManager = Gtk::UIManager::create();
-  m_refUIManager->insert_action_group(m_refActionGroup);
-
-  add_accel_group(m_refUIManager->get_accel_group());
-  //Layout the actions in a menubar and toolbar:
-  Glib::ustring ui_info =
-    "<ui>"
-    "  <menubar name='MenuBar'>"
-    "    <menu action='FileMenu'>"
-    "      <menuitem action='StartScript'/>"
-    "      <menuitem action='AddGraph'/>"
-    "      <menuitem action='FileQuit'/>"
-    "    </menu>"
-    "  </menubar>"
-    "</ui>";
-  try
-    {
-      m_refUIManager->add_ui_from_string(ui_info);
-    }
-  catch(const Glib::Error& ex)
-    {
-      std::cerr << "building menus failed: " <<  ex.what();
-    }
-  Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar");
-  scrolled.add(w);
-  if(pMenubar)
-    m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK);
-  m_Box.pack_start(scrolled, Gtk::PACK_EXPAND_WIDGET);
-  scrolled.show();
-
-  show_all_children();
-
-}
-
-void GrapherWindow::on_menu_file_quit()
-{
-  hide();
-}
-
-void GrapherWindow::on_menu_script_start()
-{
-  _graphicalLauncher->runDialog();
-}
-
-void GrapherWindow::childDied(int pid)
-{
-  hide();
-}
-
-// magic for noticing that the child stap process has died.
-int signalPipe[2] = {-1, -1};
-
-extern "C"
-{
-  void handleChild(int signum, siginfo_t* info, void* context)
-  {
-    char buf[1];
-    ssize_t err;
-    buf[0] = 1;
-    err = write(signalPipe[1], buf, 1);
-  }
-}
-
 // Depending on how args are passed, either launch stap directly or
 // use the shell to parse arguments
 class StapLauncher : public ChildDeathReader
 {
 public:
-  StapLauncher() : _argv(0), _childPid(-1), _deathCallback(0), _stapParser(0) {}
+  StapLauncher() : _argv(0), _childPid(-1), _deathCallback(0) {}
   StapLauncher(char** argv)
-    : _argv(argv), _childPid(-1), _deathCallback(0), _stapParser(0)
+    : _argv(argv), _childPid(-1), _deathCallback(0)
   {
   }
   StapLauncher(const string& stapArgs, const string& script,
                const string& scriptArgs)
-    : _childPid(-1), _deathCallback(0), _stapParser(0)
+    : _childPid(-1), _deathCallback(0), _win(0), _widget(0)
   {
     setArgs(stapArgs, script, scriptArgs);
   }
@@ -239,12 +130,19 @@ public:
   {
     _deathCallback = callback;
   }
-  void setStapParser(StapParser *parser)
+  void setWinParams(Gtk::Window* win, GraphWidget* widget)
   {
-    _stapParser = parser;
+    _win = win;
+    _widget = widget;
   }
   int launch();
   void cleanUp();
+  tr1::shared_ptr<StapParser> makeStapParser()
+  {
+    tr1::shared_ptr<StapParser> result(new StapParser(_win, _widget));
+    _stapParsers.push_back(result);
+    return result;
+  }
 protected:
   char** _argv;
   string _stapArgs;
@@ -252,7 +150,9 @@ protected:
   string _scriptArgs;
   int _childPid;
   ChildDeathReader::Callback* _deathCallback;
-  StapParser* _stapParser;
+  Gtk::Window* _win;
+  GraphWidget* _widget;
+  vector<tr1::shared_ptr<StapParser> > _stapParsers;
 };
 
 int StapLauncher::launch()
@@ -313,9 +213,11 @@ int StapLauncher::launch()
         }
       _exit(1);
     }
-  _stapParser->setErrFd(pipefd[2]);
-  _stapParser->setInFd(pipefd[0]);
-  Glib::signal_io().connect(sigc::mem_fun(*_stapParser,
+  tr1::shared_ptr<StapParser> sp(new StapParser(_win, _widget));
+  _stapParsers.push_back(sp);
+  sp->setErrFd(pipefd[2]);
+  sp->setInFd(pipefd[0]);
+  Glib::signal_io().connect(sigc::mem_fun(sp.get(),
                                           &StapParser::errIoCallback),
                             pipefd[2],
                             Glib::IO_IN);
@@ -326,7 +228,7 @@ int StapLauncher::launch()
                                               &ChildDeathReader::ioCallback),
                                 signalPipe[0], Glib::IO_IN);
     }
-  Glib::signal_io().connect(sigc::mem_fun(*_stapParser,
+  Glib::signal_io().connect(sigc::mem_fun(sp.get(),
                                           &StapParser::ioCallback),
                             pipefd[0],
                             Glib::IO_IN | Glib::IO_HUP);
@@ -347,34 +249,144 @@ void StapLauncher::cleanUp()
     }
 }
 
-StapLauncher launcher;
+class GraphicalStapLauncher : public StapLauncher
+{
+public:
+  GraphicalStapLauncher();
+  bool runDialog();
+  void onLaunch();
+  void onLaunchCancel();
+private:
+  Glib::RefPtr<Gnome::Glade::Xml> _launchStapDialog;
+  Gtk::Window* _scriptWindow;
+  Gtk::FileChooserButton* _chooserButton;
+  Gtk::Entry* _stapArgEntry;
+  Gtk::Entry* _scriptArgEntry;
+  StapLauncher* _launcher;
+};
+
+class GrapherWindow : public Gtk::Window, public ChildDeathReader::Callback
+{
+public:
+  GrapherWindow();
+  virtual ~GrapherWindow() {}
+  Gtk::VBox m_Box;
+  Gtk::ScrolledWindow scrolled;
+  GraphWidget w;
+  void childDied(int pid);
+  void setGraphicalLauncher(GraphicalStapLauncher* launcher)
+  {
+    _graphicalLauncher = launcher;
+  }
+  GraphicalStapLauncher* getGraphicalLauncher() { return _graphicalLauncher; }
+protected:
+  virtual void on_menu_file_quit();
+  virtual void on_menu_script_start();
+  void addGraph();
+  // menu support
+  Glib::RefPtr<Gtk::UIManager> m_refUIManager;
+  Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup;
+  GraphicalStapLauncher* _graphicalLauncher;
+
+};
+
+GrapherWindow::GrapherWindow()
+{
+  set_title("systemtap grapher");
+  add(m_Box);
+
+
+  //Create actions for menus and toolbars:
+  m_refActionGroup = Gtk::ActionGroup::create();
+  //File menu:
+  m_refActionGroup->add(Gtk::Action::create("FileMenu", "File"));
+  m_refActionGroup->add(Gtk::Action::create("StartScript", "Start script"),
+                        sigc::mem_fun(*this,
+                                      &GrapherWindow::on_menu_script_start));
+  m_refActionGroup->add(Gtk::Action::create("AddGraph", "Add graph"),
+                        sigc::mem_fun(*this, &GrapherWindow::addGraph));
+  m_refActionGroup->add(Gtk::Action::create("FileQuit", Gtk::Stock::QUIT),
+                        sigc::mem_fun(*this,
+                                      &GrapherWindow::on_menu_file_quit));
+  m_refUIManager = Gtk::UIManager::create();
+  m_refUIManager->insert_action_group(m_refActionGroup);
+
+  add_accel_group(m_refUIManager->get_accel_group());
+  //Layout the actions in a menubar and toolbar:
+  Glib::ustring ui_info =
+    "<ui>"
+    "  <menubar name='MenuBar'>"
+    "    <menu action='FileMenu'>"
+    "      <menuitem action='StartScript'/>"
+    "      <menuitem action='AddGraph'/>"
+    "      <menuitem action='FileQuit'/>"
+    "    </menu>"
+    "  </menubar>"
+    "</ui>";
+  try
+    {
+      m_refUIManager->add_ui_from_string(ui_info);
+    }
+  catch(const Glib::Error& ex)
+    {
+      std::cerr << "building menus failed: " <<  ex.what();
+    }
+  Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar");
+  scrolled.add(w);
+  if(pMenubar)
+    m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK);
+  m_Box.pack_start(scrolled, Gtk::PACK_EXPAND_WIDGET);
+  scrolled.show();
+
+  show_all_children();
+
+}
+
+void GrapherWindow::on_menu_file_quit()
+{
+  hide();
+}
+
+void GrapherWindow::on_menu_script_start()
+{
+  _graphicalLauncher->runDialog();
+}
+
+void GrapherWindow::childDied(int pid)
+{
+  hide();
+}
+
+
+
 
 int main(int argc, char** argv)
 {
   Gtk::Main app(argc, argv);
-
+  GraphicalStapLauncher launcher;
   GrapherWindow win;
 
   win.set_title("Grapher");
   win.set_default_size(600, 200);
+  launcher.setWinParams(&win, &win.w);
 
-  StapParser stapParser(win, win.w);
-  launcher.setStapParser(&stapParser);
-  GraphicalStapLauncher graphicalLauncher(&launcher);
-  win.setGraphicalLauncher(&graphicalLauncher);
-  if (argc > 1)
-    {
-      launcher.setArgv(argv + 1);
-      launcher.setDeathCallback(&win);
-      launcher.launch();
-    }
-  else
+  win.setGraphicalLauncher(&launcher);
+  
+  if (argc == 2 && !std::strcmp(argv[1], "-"))
     {
-      Glib::signal_io().connect(sigc::mem_fun(stapParser,
+      tr1::shared_ptr<StapParser> sp = launcher.makeStapParser();
+      sp->setInFd(STDIN_FILENO);
+      Glib::signal_io().connect(sigc::mem_fun(sp.get(),
                                               &StapParser::ioCallback),
                                 STDIN_FILENO,
                                 Glib::IO_IN | Glib::IO_HUP);
     }
+  else if (argc > 1)
+    {
+      launcher.setArgv(argv + 1);
+      launcher.setDeathCallback(&win);
+      launcher.launch();
+    }
   Gtk::Main::run(win);
   launcher.cleanUp();
   return 0;
@@ -386,8 +398,7 @@ void GrapherWindow::addGraph()
   
 }
 
-GraphicalStapLauncher::GraphicalStapLauncher(StapLauncher* launcher)
- : _launcher(launcher)
+GraphicalStapLauncher::GraphicalStapLauncher()
 {
   try
     {
@@ -421,10 +432,10 @@ bool GraphicalStapLauncher::runDialog()
 
 void GraphicalStapLauncher::onLaunch()
 {
-  _launcher->setArgs(_stapArgEntry->get_text(), _chooserButton->get_filename(),
-                     _scriptArgEntry->get_text());
+  setArgs(_stapArgEntry->get_text(), _chooserButton->get_filename(),
+          _scriptArgEntry->get_text());
   _scriptWindow->hide();
-  _launcher->launch();
+  launch();
 }
 
 void GraphicalStapLauncher::onLaunchCancel()
This page took 0.036843 seconds and 5 git commands to generate.