|
| 1 | +package com.oracle.graal.python.builtins.modules; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import com.oracle.graal.python.PythonLanguage; |
| 8 | +import com.oracle.graal.python.builtins.Builtin; |
| 9 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 10 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 11 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 12 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 13 | +import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes; |
| 14 | +import com.oracle.graal.python.builtins.objects.common.SequenceNodes; |
| 15 | +import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes; |
| 16 | +import com.oracle.graal.python.builtins.objects.dict.PDict; |
| 17 | +import com.oracle.graal.python.builtins.objects.function.PArguments; |
| 18 | +import com.oracle.graal.python.builtins.objects.module.PythonModule; |
| 19 | +import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary; |
| 20 | +import com.oracle.graal.python.builtins.objects.tuple.PTuple; |
| 21 | +import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode; |
| 22 | +import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode; |
| 23 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 24 | +import com.oracle.graal.python.nodes.function.PythonBuiltinNode; |
| 25 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 26 | +import com.oracle.graal.python.nodes.util.CastToJavaStringNode; |
| 27 | +import com.oracle.graal.python.runtime.PythonContext; |
| 28 | +import com.oracle.graal.python.runtime.sequence.PSequence; |
| 29 | +import com.oracle.truffle.api.InstrumentInfo; |
| 30 | +import com.oracle.truffle.api.TruffleFile; |
| 31 | +import com.oracle.truffle.api.TruffleLanguage.Env; |
| 32 | +import com.oracle.truffle.api.dsl.Cached; |
| 33 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 34 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 35 | +import com.oracle.truffle.api.dsl.Specialization; |
| 36 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 37 | +import com.oracle.truffle.api.instrumentation.SourceSectionFilter; |
| 38 | +import com.oracle.truffle.api.library.CachedLibrary; |
| 39 | +import com.oracle.truffle.api.object.HiddenKey; |
| 40 | +import com.oracle.truffle.tools.coverage.CoverageTracker; |
| 41 | +import com.oracle.truffle.tools.coverage.RootCoverage; |
| 42 | +import com.oracle.truffle.tools.coverage.SectionCoverage; |
| 43 | +import com.oracle.truffle.tools.coverage.SourceCoverage; |
| 44 | +import com.oracle.truffle.tools.coverage.impl.CoverageInstrument; |
| 45 | + |
| 46 | +@CoreFunctions(defineModule = "_trace") |
| 47 | +public class TraceModuleBuiltins extends PythonBuiltins { |
| 48 | + @Override |
| 49 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 50 | + return TraceModuleBuiltinsFactory.getFactories(); |
| 51 | + } |
| 52 | + |
| 53 | + private static final HiddenKey TRACKER = new HiddenKey("coverageTracker"); |
| 54 | + private static final HiddenKey TRACK_FUNCS = new HiddenKey("trackCalledFuncs"); |
| 55 | + |
| 56 | + @Builtin(name = "start", parameterNames = {"mod", "count", "countfuncs", "ignoremods", "ignoredirs"}, minNumOfPositionalArgs = 1, declaresExplicitSelf = true) |
| 57 | + @GenerateNodeFactory |
| 58 | + abstract static class TraceNew extends PythonBuiltinNode { |
| 59 | + @Specialization(limit = "getCallSiteInlineCacheMaxDepth()") |
| 60 | + PNone doit(VirtualFrame frame, PythonModule mod, Object count, Object countfuncs, PSequence ignoremods, PSequence ignoredirs, |
| 61 | + @Cached SequenceNodes.GetSequenceStorageNode getStore, |
| 62 | + @Cached SequenceStorageNodes.ToArrayNode toArray, |
| 63 | + @Cached CastToJavaStringNode castStr, |
| 64 | + @CachedLibrary("count") PythonObjectLibrary lib, |
| 65 | + @Cached ReadAttributeFromObjectNode readNode, |
| 66 | + @Cached WriteAttributeToObjectNode writeNode) { |
| 67 | + Object currentTracker = readNode.execute(mod, TRACKER); |
| 68 | + CoverageTracker tracker = null; |
| 69 | + |
| 70 | + SourceSectionFilter.Builder filter = SourceSectionFilter.newBuilder(); |
| 71 | + filter.includeInternal(false).mimeTypeIs(PythonLanguage.MIME_TYPE); |
| 72 | + PythonContext context = getContext(); |
| 73 | + String stdLibHome = context.getStdlibHome(); |
| 74 | + filter.sourceIs((src) -> { |
| 75 | + String path = src.getPath(); |
| 76 | + return path != null && !path.contains(stdLibHome); |
| 77 | + }); |
| 78 | + |
| 79 | + Object[] ignoreMods = toArray.execute(getStore.execute(ignoremods)); |
| 80 | + Env env = context.getEnv(); |
| 81 | + for (Object moduleName : ignoreMods) { |
| 82 | + String modStr = castStr.execute(moduleName); |
| 83 | + if (modStr == null) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + String fileNameSeparator = env.getFileNameSeparator(); |
| 87 | + modStr = modStr.replace(".", fileNameSeparator); |
| 88 | + String modFile = modStr + ".py"; |
| 89 | + String modInit = modStr + fileNameSeparator + "__init__.py"; |
| 90 | + filter.sourceIs((src) -> { |
| 91 | + String path = src.getPath(); |
| 92 | + return path != null && !(path.endsWith(modFile) || path.endsWith(modInit)); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + Object[] ignoreDirs = toArray.execute(getStore.execute(ignoredirs)); |
| 97 | + for (Object dir : ignoreDirs) { |
| 98 | + String dirStr = castStr.execute(dir); |
| 99 | + if (dirStr == null) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + String absDir; |
| 103 | + try { |
| 104 | + absDir = env.getPublicTruffleFile(dirStr).getCanonicalFile().getPath(); |
| 105 | + } catch (SecurityException | IOException e) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + filter.sourceIs((src) -> { |
| 109 | + String path = src.getPath(); |
| 110 | + return path != null && !path.equals(absDir); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + if (currentTracker instanceof CoverageTracker) { |
| 115 | + tracker = (CoverageTracker) currentTracker; |
| 116 | + } else { |
| 117 | + Map<String, InstrumentInfo> instruments = env.getInstruments(); |
| 118 | + InstrumentInfo instrumentInfo = instruments.get(CoverageInstrument.ID); |
| 119 | + if (instrumentInfo != null) { |
| 120 | + tracker = env.lookup(instrumentInfo, CoverageTracker.class); |
| 121 | + if (tracker != null) { |
| 122 | + writeNode.execute(mod, TRACKER, tracker); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + if (tracker == null) { |
| 127 | + throw raise(PythonBuiltinClassType.NotImplementedError, "coverage tracker not available"); |
| 128 | + } |
| 129 | + writeNode.execute(mod, TRACK_FUNCS, countfuncs); |
| 130 | + |
| 131 | + tracker.start(new CoverageTracker.Config(filter.build(), lib.isTrueWithState(count, PArguments.getThreadState(frame)))); |
| 132 | + |
| 133 | + return PNone.NONE; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Builtin(name = "stop", minNumOfPositionalArgs = 1, declaresExplicitSelf = true) |
| 138 | + @GenerateNodeFactory |
| 139 | + abstract static class Stop extends PythonUnaryBuiltinNode { |
| 140 | + @Specialization |
| 141 | + PNone start(PythonModule mod, |
| 142 | + @Cached ReadAttributeFromObjectNode readNode) { |
| 143 | + Object currentTracker = readNode.execute(mod, TRACKER); |
| 144 | + if (currentTracker instanceof CoverageTracker) { |
| 145 | + ((CoverageTracker) currentTracker).close(); |
| 146 | + return PNone.NONE; |
| 147 | + } else { |
| 148 | + throw raise(PythonBuiltinClassType.TypeError, "coverage tracker not running"); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Builtin(name = "results", minNumOfPositionalArgs = 1, declaresExplicitSelf = true) |
| 154 | + @GenerateNodeFactory |
| 155 | + abstract static class Results extends PythonUnaryBuiltinNode { |
| 156 | + @Specialization |
| 157 | + PTuple start(VirtualFrame frame, PythonModule mod, |
| 158 | + @CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib, |
| 159 | + @Cached ReadAttributeFromObjectNode readNode, |
| 160 | + @Cached HashingCollectionNodes.SetItemNode setItemNode) { |
| 161 | + Object currentTracker = readNode.execute(mod, TRACKER); |
| 162 | + boolean countFuncs = lib.isTrue(readNode.execute(mod, TRACK_FUNCS)); |
| 163 | + |
| 164 | + CoverageTracker tracker; |
| 165 | + if (currentTracker instanceof CoverageTracker) { |
| 166 | + tracker = (CoverageTracker) currentTracker; |
| 167 | + } else { |
| 168 | + throw raise(PythonBuiltinClassType.TypeError, "coverage tracker not running"); |
| 169 | + } |
| 170 | + SourceCoverage[] coverage = tracker.getCoverage(); |
| 171 | + // callers -> not supported |
| 172 | + // calledfuncs -> {(filename, modulename, funcname) => 1} |
| 173 | + // counts -> {(filename, lineno) => count} |
| 174 | + PDict calledFuncs = factory().createDict(); |
| 175 | + PDict counts = factory().createDict(); |
| 176 | + for (SourceCoverage c : coverage) { |
| 177 | + String filename = c.getSource().getPath(); |
| 178 | + if (filename == null) { |
| 179 | + continue; |
| 180 | + } |
| 181 | + |
| 182 | + String modName; |
| 183 | + TruffleFile file = getContext().getEnv().getPublicTruffleFile(filename); |
| 184 | + String baseName = file.getName(); |
| 185 | + if (baseName != null) { |
| 186 | + if (baseName.endsWith("__init__.py")) { |
| 187 | + modName = file.getParent().getName(); |
| 188 | + } else { |
| 189 | + modName = baseName.replaceFirst("\\.py$", ""); |
| 190 | + } |
| 191 | + } else { |
| 192 | + continue; |
| 193 | + } |
| 194 | + |
| 195 | + for (RootCoverage r : c.getRoots()) { |
| 196 | + String name = r.getName(); |
| 197 | + if (name == null) { |
| 198 | + continue; |
| 199 | + } |
| 200 | + if (countFuncs) { |
| 201 | + PTuple tp = factory().createTuple(new Object[] {filename, modName, name}); |
| 202 | + setItemNode.execute(frame, calledFuncs, tp, 1); |
| 203 | + } |
| 204 | + for (SectionCoverage s : r.getSectionCoverage()) { |
| 205 | + if (s.getSourceSection().hasLines()) { |
| 206 | + int startLine = s.getSourceSection().getStartLine(); |
| 207 | + int endLine = startLine; // s.getSourceSection().getEndLine(); |
| 208 | + long cnt = s.getCount(); |
| 209 | + if (cnt < 0) { |
| 210 | + cnt = 1; |
| 211 | + } |
| 212 | + for (int i = startLine; i <= endLine; i++) { |
| 213 | + PTuple ctp = factory().createTuple(new Object[] {filename, i}); |
| 214 | + setItemNode.execute(frame, counts, ctp, cnt); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + return factory().createTuple(new Object[] { counts, calledFuncs }); |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments