コード例 #1
0
ファイル: D.java プロジェクト: vongosling/btrace
  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;
  }
コード例 #2
0
ファイル: DTrace.java プロジェクト: sdgdsffdsfff/btrace
  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;
  }