comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/Instrumenter.java @ 22352:7d9b7365b675

Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 04 Nov 2015 16:54:36 +0100
parents 260e3cdf11ec
children
comparison
equal deleted inserted replaced
22335:906a5f6e07cc 22352:7d9b7365b675
44 import com.oracle.truffle.api.instrument.TagInstrument.BeforeTagInstrument; 44 import com.oracle.truffle.api.instrument.TagInstrument.BeforeTagInstrument;
45 import com.oracle.truffle.api.nodes.Node; 45 import com.oracle.truffle.api.nodes.Node;
46 import com.oracle.truffle.api.nodes.RootNode; 46 import com.oracle.truffle.api.nodes.RootNode;
47 import com.oracle.truffle.api.source.Source; 47 import com.oracle.truffle.api.source.Source;
48 import com.oracle.truffle.api.source.SourceSection; 48 import com.oracle.truffle.api.source.SourceSection;
49 import java.util.Map;
49 50
50 /** 51 /**
51 * Client access to instrumentation services in a Truffle execution environment. 52 * Client access to instrumentation services in a Truffle execution environment.
52 * <p> 53 * <p>
53 * Services include: 54 * Services include:
476 * @param languageClass the language in which the source text is to be executed 477 * @param languageClass the language in which the source text is to be executed
477 * @param source the source code to be evaluated, non-null and non-empty 478 * @param source the source code to be evaluated, non-null and non-empty
478 * @param listener optional client callback for results/failure notification 479 * @param listener optional client callback for results/failure notification
479 * @param instrumentInfo instrumentInfo optional documentation about the Instrument 480 * @param instrumentInfo instrumentInfo optional documentation about the Instrument
480 * @return a handle for access to the binding 481 * @return a handle for access to the binding
481 */ 482 * @deprecated
483 */
484 @Deprecated
482 @SuppressWarnings("rawtypes") 485 @SuppressWarnings("rawtypes")
483 public ProbeInstrument attach(Probe probe, Class<? extends TruffleLanguage> languageClass, Source source, EvalInstrumentListener listener, String instrumentInfo) { 486 public ProbeInstrument attach(Probe probe, Class<? extends TruffleLanguage> languageClass, Source source, EvalInstrumentListener listener, String instrumentInfo) {
487 return attach(probe, languageClass, source, listener, instrumentInfo, new String[0], new Object[0]);
488 }
489
490 /**
491 * <em>Attaches</em> a fragment of source text that is to be evaluated just before execution
492 * enters the location of a {@link Probe}, creating a <em>binding</em> called an
493 * {@link ProbeInstrument}. The outcome of the evaluation is reported to an optional
494 * {@link EvalInstrumentListener listener}, but the outcome does not affect the flow of guest
495 * language execution, even if the evaluation produces an exception.
496 * <p>
497 * The source text is assumed to be expressed in the language identified by its associated
498 * {@linkplain Source#getMimeType() MIME type}, if specified, otherwise by the language
499 * associated with the AST location associated with the {@link Probe}.
500 * <p>
501 * The source text is parsed in the lexical context of the AST location associated with the
502 * {@link Probe}.
503 * <p>
504 * The source text executes subject to full Truffle optimization.
505 *
506 * @param probe source of AST execution events, non-null
507 * @param source the source code to be evaluated, non-null and non-empty, preferably with
508 * {@link Source#withMimeType(java.lang.String) specified mime type} that determines
509 * the {@link TruffleLanguage} to use when processing the source
510 * @param listener optional client callback for results/failure notification
511 * @param instrumentInfo instrumentInfo optional documentation about the Instrument
512 * @param parameters keys are the parameter names to pass to
513 * {@link TruffleLanguage#parse(com.oracle.truffle.api.source.Source, com.oracle.truffle.api.nodes.Node, java.lang.String...)
514 * parse} method; values will be passed to
515 * {@link CallTarget#call(java.lang.Object...)} returned from the <code>parse</code>
516 * method; the value can be <code>null</code>
517 * @return a handle for access to the binding
518 */
519 public ProbeInstrument attach(Probe probe, Source source, EvalInstrumentListener listener, String instrumentInfo, Map<String, Object> parameters) {
520 final int size = parameters == null ? 0 : parameters.size();
521 String[] names = new String[size];
522 Object[] params = new Object[size];
523 if (parameters != null) {
524 int index = 0;
525 for (Map.Entry<String, Object> entry : parameters.entrySet()) {
526 names[index] = entry.getKey();
527 params[index] = entry.getValue();
528 index++;
529 }
530 }
531 return attach(probe, null, source, listener, instrumentInfo, names, params);
532 }
533
534 @SuppressWarnings("rawtypes")
535 private ProbeInstrument attach(Probe probe, Class<? extends TruffleLanguage> languageClass, Source source, EvalInstrumentListener listener, String instrumentInfo, String[] argumentNames,
536 Object[] parameters) {
484 assert probe.getInstrumenter() == this; 537 assert probe.getInstrumenter() == this;
485 final EvalInstrument instrument = new EvalInstrument(languageClass, source, listener, instrumentInfo); 538 Class<? extends TruffleLanguage> foundLanguageClass = null;
539 if (languageClass == null) {
540 if (source.getMimeType() == null) {
541 foundLanguageClass = ACCESSOR.findLanguage(probe);
542 }
543 } else {
544 foundLanguageClass = languageClass;
545 }
546 final EvalInstrument instrument = new EvalInstrument(foundLanguageClass, source, listener, instrumentInfo, argumentNames, parameters);
486 probe.attach(instrument); 547 probe.attach(instrument);
487 return instrument; 548 return instrument;
488 } 549 }
489 550
490 // TODO (mlvdv) allow multiple <em>before</em> instruments without performance hit? 551 // TODO (mlvdv) allow multiple <em>before</em> instruments without performance hit?