-
Notifications
You must be signed in to change notification settings - Fork 10
/
FileAccessStateProvider.java
183 lines (159 loc) · 7.3 KB
/
FileAccessStateProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************************************************
* Copyright (c) 2018 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.incubator.internal.kernel.core.fileaccess;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout;
import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelTrace;
import org.eclipse.tracecompass.incubator.internal.kernel.core.Activator;
import org.eclipse.tracecompass.incubator.internal.kernel.core.filedescriptor.FileDescriptorStateProvider;
import org.eclipse.tracecompass.incubator.internal.kernel.core.filedescriptor.HandlerParameter;
import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
import org.eclipse.tracecompass.statesystem.core.StateSystemBuilderUtils;
import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import com.google.common.collect.ImmutableSet;
/**
* <p>
* File Access State Provider, handles file descriptors of files on a file
* system.
* </p>
* Handles
* <ul>
* <li>open</li>
* <li>openat</li>
* <li>dup(soon)</li>
* <li>dirstat(soon)</li></ul
*
* @author Matthew Khouzam
*/
public class FileAccessStateProvider extends FileDescriptorStateProvider {
private static final String PID = "pid"; //$NON-NLS-1$
private static final String STDERR = "stderr"; //$NON-NLS-1$
private static final String STDOUT = "stdout"; //$NON-NLS-1$
private static final String STDIN = "stdin"; //$NON-NLS-1$
private static final String ID = "org.eclipse.tracecompass.incubator.internal.kernel.core.fileacess"; //$NON-NLS-1$
private static final String OPENAT = "openat"; //$NON-NLS-1$
private static final String OPEN = "open"; //$NON-NLS-1$
private static final String LTTNG_STATEDUMP_FILE_DESCRIPTOR = "lttng_statedump_file_descriptor"; //$NON-NLS-1$
private static final Set<String> NO_NO_LIST = ImmutableSet.of("socket:", "pipe:"); //$NON-NLS-1$ //$NON-NLS-2$
private static final int VERSION = 1;
private final String fFileName;
private final Map<Integer, String> fOpenContexts = new HashMap<>();
/**
* Constructor
*
* @param trace
* the trace to handle
*/
public FileAccessStateProvider(IKernelTrace trace) {
super(trace, ID);
IKernelAnalysisEventLayout layout = getLayout();
addEventHandler(layout.eventSyscallEntryPrefix() + OPENAT, this::handleOpen);
addEventHandler(layout.eventSyscallExitPrefix() + OPENAT, this::handleOpenExit);
addEventHandler(layout.eventSyscallEntryPrefix() + OPEN, this::handleOpen);
addEventHandler(layout.eventSyscallExitPrefix() + OPEN, this::handleOpenExit);
addEventHandler(LTTNG_STATEDUMP_FILE_DESCRIPTOR, this::handleStateDump);
fFileName = layout.fieldFilename();
}
@Override
public int getVersion() {
return VERSION;
}
@Override
public @NonNull ITmfStateProvider getNewInstance() {
ITmfTrace trace = getTrace();
if (trace instanceof IKernelTrace) {
return new FileAccessStateProvider((IKernelTrace) trace);
}
throw new IllegalStateException("Trace " + trace + " is not a kernel trace"); //$NON-NLS-1$//$NON-NLS-2$
}
private void handleOpenExit(HandlerParameter hp) {
Integer tid = hp.getTid();
ITmfStateSystemBuilder ssb = hp.getSsb();
long time = hp.getTime();
ITmfEvent event = hp.getEvent();
String fileName = fOpenContexts.remove(tid);
Long ret = event.getContent().getFieldValue(Long.class, getLayout().fieldSyscallRet());
if (ret == null || fileName == null) {
return;
}
handleCommonOpen(ssb, time, tid, ret, fileName);
}
private static void handleCommonOpen(ITmfStateSystemBuilder ssb, long time, Integer tid, Long fd, String filename) {
// update the filename with fd
String fn = filename;
if (fd == 0) {
fn = STDIN;
} else if (fd == 1) {
fn = STDOUT;
} else if (fd == 2) {
fn = STDERR;
}
if (fn.isEmpty()) {
return;
}
int fileQuark = ssb.getQuarkAbsoluteAndAdd(RESOURCES, fn);
int fileTidQuark = ssb.getQuarkAbsoluteAndAdd(RESOURCES, fn, String.valueOf(tid));
// Failed read, put the error number in fd
if (fd < 0) {
ssb.updateOngoingState(fd, fileTidQuark);
ssb.modifyAttribute(time, (Object) null, fileTidQuark);
return;
}
// successful open, reset fd to null for before, and update the fd at current time
ssb.updateOngoingState((Object) null, fileTidQuark);
ssb.modifyAttribute(time, fd, fileTidQuark);
try {
// Update the thread's current files
int tidQuark = ssb.getQuarkAbsoluteAndAdd(TID, String.valueOf(tid));
StateSystemBuilderUtils.incrementAttributeInt(ssb, time, tidQuark, 1);
int tidFileQuark = ssb.getQuarkAbsoluteAndAdd(TID, String.valueOf(tid), String.valueOf(fd));
ssb.modifyAttribute(time, fn, tidFileQuark);
StateSystemBuilderUtils.incrementAttributeInt(ssb, time, fileQuark, 1);
} catch (StateValueTypeException e) {
Activator.getInstance().logError(e.getMessage(), e);
}
}
private void handleOpen(HandlerParameter param) {
ITmfEvent event = param.getEvent();
Integer tid = param.getTid();
String fileName = event.getContent().getFieldValue(String.class, fFileName);
if (fileName == null) {
return;
}
ITmfStateSystemBuilder ssb = param.getSsb();
long time = param.getTime();
// Prepare the file access quark and save a temporary value, to be
// udpated in case of failure
int fileTidQuark = ssb.getQuarkAbsoluteAndAdd(RESOURCES, fileName, String.valueOf(tid));
ssb.modifyAttribute(time, 0L, fileTidQuark);
fOpenContexts.put(tid, fileName);
}
private void handleStateDump(HandlerParameter params) {
ITmfEvent event = params.getEvent();
ITmfEventField content = event.getContent();
Long pid = content.getFieldValue(Long.class, PID);
Long fd = content.getFieldValue(Long.class, DESCRIPTOR);
String filename = content.getFieldValue(String.class, fFileName);
if (pid == null || fd == null || filename == null || NO_NO_LIST.stream().anyMatch(filename::startsWith)) {
return;
}
int tid = pid.intValue();
fOpenContexts.put(tid, filename);
handleCommonOpen(params.getSsb(), event.getTimestamp().toNanos(), tid, fd, filename);
}
}