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; }