Esempio n. 1
0
  /**
   * Submits a D-script from given file and receives the BTrace script argument array as DTrace
   * macro arguments. The events from DTrace are wrapped as BTrace commands and listener given is
   * notified.
   *
   * @param fileName path to D-script file to submit
   */
  public static void submitFile(String fileName) {
    init();
    if (fileName != null && !fileName.isEmpty()) {
      try {
        File dscript = new File(fileName);
        if (dscript.exists()) {
          Consumer cons = newConsumer();
          cons.compile(dscript, rt.$$());
          start(cons);
        }
      } catch (final IOException e) {
        rt.send(
            ErrorCommand.class,
            new AbstractCommand.Initializer<ErrorCommand>() {

              public void init(ErrorCommand cmd) {
                cmd.setCause(e);
              }
            });
      } catch (final DTraceException dEx) {
        rt.send(
            ErrorCommand.class,
            new AbstractCommand.Initializer<ErrorCommand>() {

              public void init(ErrorCommand cmd) {
                cmd.setCause(dEx);
              }
            });
      }
    }
  }
Esempio n. 2
0
 private static void start(Consumer cons, final CommandListener listener) throws DTraceException {
   cons.enable();
   cons.go(
       new ExceptionHandler() {
         @Override
         public void handleException(Throwable th) {
           try {
             listener.onCommand(new ErrorCommand(th));
           } catch (IOException ioexp) {
             ioexp.printStackTrace();
           }
         }
       });
 }
Esempio n. 3
0
  private static void start(Consumer cons) throws DTraceException {
    cons.enable();
    cons.go(
        new ExceptionHandler() {
          @Override
          public void handleException(final Throwable th) {
            rt.send(
                ErrorCommand.class,
                new AbstractCommand.Initializer<ErrorCommand>() {

                  public void init(ErrorCommand cmd) {
                    cmd.setCause(th);
                  }
                });
          }
        });
  }
Esempio n. 4
0
  /**
   * Submits a D-script string and receives the BTrace script argument array as DTrace macro
   * arguments. The events from DTrace are wrapped as BTrace commands and listener given is
   * notified.
   *
   * @param program D-script as a string
   */
  public static void submit(String oneliner) {
    init();
    if (oneliner != null && !oneliner.isEmpty()) {
      try {
        Consumer cons = newConsumer();
        cons.compile(oneliner, rt.$$());
        start(cons);
      } catch (final DTraceException dEx) {
        rt.send(
            ErrorCommand.class,
            new AbstractCommand.Initializer<ErrorCommand>() {

              public void init(ErrorCommand cmd) {
                cmd.setCause(dEx);
              }
            });
      }
    }
  }
Esempio n. 5
0
  private static Consumer newConsumer() throws DTraceException {
    Consumer cons = new LocalConsumer();
    cons.addConsumerListener(
        new ConsumerAdapter() {
          @Override
          public void consumerStarted(final ConsumerEvent ce) {
            rt.send(
                DTraceStartCommand.class,
                new AbstractCommand.Initializer<DTraceStartCommand>() {

                  public void init(DTraceStartCommand cmd) {
                    cmd.setConsumerEvent(ce);
                  }
                });
          }

          @Override
          public void consumerStopped(final ConsumerEvent ce) {
            Consumer cons = (Consumer) ce.getSource();
            Aggregate ag = null;
            try {
              ag = cons.getAggregate();
            } catch (final DTraceException dexp) {
              rt.send(
                  ErrorCommand.class,
                  new AbstractCommand.Initializer<ErrorCommand>() {

                    public void init(ErrorCommand cmd) {
                      cmd.setCause(dexp);
                    }
                  });
            }
            StringBuilder buf = new StringBuilder();
            if (ag != null) {
              for (Aggregation agg : ag.asMap().values()) {
                String name = agg.getName();
                if (name != null && name.length() > 0) {
                  buf.append(name);
                  buf.append('\n');
                }
                for (AggregationRecord rec : agg.asMap().values()) {
                  buf.append('\t');
                  buf.append(rec.getTuple());
                  buf.append(" ");
                  buf.append(rec.getValue());
                  buf.append('\n');
                }
              }
            }
            final String msg = buf.toString();
            if (msg.length() > 0) {
              rt.send(
                  MessageCommand.class,
                  new AbstractCommand.Initializer<MessageCommand>() {

                    public void init(MessageCommand cmd) {
                      cmd.setMessage(msg);
                    }
                  });
            }
            rt.send(
                DTraceStopCommand.class,
                new AbstractCommand.Initializer<DTraceStopCommand>() {

                  public void init(DTraceStopCommand cmd) {
                    cmd.setConsumerEvent(ce);
                  }
                });
            cons.close();
          }

          @Override
          public void dataReceived(final DataEvent de) {
            rt.send(
                DTraceDataCommand.class,
                new AbstractCommand.Initializer<DTraceDataCommand>() {

                  public void init(DTraceDataCommand cmd) {
                    cmd.setDataEvent(de);
                  }
                });
          }

          @Override
          public void dataDropped(final DropEvent de) {
            rt.send(
                DTraceDropCommand.class,
                new AbstractCommand.Initializer<DTraceDropCommand>() {

                  public void init(DTraceDropCommand cmd) {
                    cmd.setDropEvent(de);
                  }
                });
          }

          @Override
          public void errorEncountered(final ErrorEvent ee) throws ConsumerException {
            try {
              super.errorEncountered(ee);
            } catch (final ConsumerException ce) {
              rt.send(
                  DTraceErrorCommand.class,
                  new AbstractCommand.Initializer<DTraceErrorCommand>() {

                    public void init(DTraceErrorCommand cmd) {
                      cmd.setErrorEvent(ee);
                      cmd.setException(ce);
                    }
                  });
              throw ce;
            }
          }
        });

    // open DTrace Consumer
    cons.open();

    // unused macro arguments are fine
    cons.setOption(Option.argref, "");
    // if no macro arg passed use "" or NULL
    cons.setOption(Option.defaultargs, "");
    // allow empty D-scripts
    cons.setOption(Option.empty, "");
    // be quiet! equivalent to DTrace's -q
    cons.setOption(Option.quiet, "");
    // undefined user land symbols are fine
    cons.setOption(Option.unodefs, "");
    // allow zero matching of probes (needed for late loading)
    cons.setOption(Option.zdefs, "");
    try {
      int pid = Integer.parseInt(rt.$(0));
      cons.grabProcess(pid);
    } catch (Exception ignored) {
    }
    return cons;
  }
Esempio n. 6
0
  private static Consumer newConsumer(String[] args, final CommandListener listener)
      throws DTraceException {
    Consumer cons = new LocalConsumer();
    cons.addConsumerListener(
        new ConsumerAdapter() {
          private void notify(Command cmd) {
            try {
              listener.onCommand(cmd);
            } catch (IOException ioexp) {
              ioexp.printStackTrace();
            }
          }

          @Override
          public void consumerStarted(ConsumerEvent ce) {
            notify(new DTraceStartCommand(ce));
          }

          @Override
          public void consumerStopped(ConsumerEvent ce) {
            Consumer cons = (Consumer) ce.getSource();
            Aggregate ag = null;
            try {
              ag = cons.getAggregate();
            } catch (DTraceException dexp) {
              notify(new ErrorCommand(dexp));
            }
            StringBuilder buf = new StringBuilder();
            if (ag != null) {
              for (Aggregation agg : ag.asMap().values()) {
                String name = agg.getName();
                if (name != null && name.length() > 0) {
                  buf.append(name);
                  buf.append('\n');
                }
                for (AggregationRecord rec : agg.asMap().values()) {
                  buf.append('\t');
                  buf.append(rec.getTuple());
                  buf.append(" ");
                  buf.append(rec.getValue());
                  buf.append('\n');
                }
              }
            }
            String msg = buf.toString();
            if (msg.length() > 0) {
              notify(new MessageCommand(msg));
            }
            notify(new DTraceStopCommand(ce));
            cons.close();
          }

          @Override
          public void dataReceived(DataEvent de) {
            notify(new DTraceDataCommand(de));
          }

          @Override
          public void dataDropped(DropEvent de) {
            notify(new DTraceDropCommand(de));
          }

          @Override
          public void errorEncountered(ErrorEvent ee) throws ConsumerException {
            try {
              super.errorEncountered(ee);
            } catch (ConsumerException ce) {
              notify(new DTraceErrorCommand(ce, ee));
              throw ce;
            }
          }
        });

    // open DTrace Consumer
    cons.open();

    // unused macro arguments are fine
    cons.setOption(Option.argref, "");
    // if no macro arg passed use "" or NULL
    cons.setOption(Option.defaultargs, "");
    // allow empty D-scripts
    cons.setOption(Option.empty, "");
    // be quiet! equivalent to DTrace's -q
    cons.setOption(Option.quiet, "");
    // undefined user land symbols are fine
    cons.setOption(Option.unodefs, "");
    // allow zero matching of probes (needed for late loading)
    cons.setOption(Option.zdefs, "");
    try {
      int pid = Integer.parseInt(args[0]);
      cons.grabProcess(pid);
    } catch (Exception ignored) {
    }
    return cons;
  }
Esempio n. 7
0
 /**
  * Submits a D-script string and passes the given argument array as DTrace macro arguments. The
  * events from DTrace are wrapped as BTrace commands and listener given is notified.
  *
  * @param program D-script as a string
  * @param args DTrace macro arguments
  * @param listener BTrace command listener that is notified
  */
 public static void submit(String program, String[] args, CommandListener listener)
     throws DTraceException {
   Consumer cons = newConsumer(args, listener);
   cons.compile(program, args);
   start(cons, listener);
 }