1/*
2 * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2010, Michael Lotz, mmlr@mlotz.ch.
4 * Distributed under the terms of the MIT License.
5 */
6#ifndef VFS_TRACING_H
7#define VFS_TRACING_H
8
9
10#include <fs/fd.h>
11#include <tracing.h>
12#include <vfs.h>
13
14
15// #pragma mark - File Descriptor Tracing
16
17
18#if FILE_DESCRIPTOR_TRACING
19
20namespace FileDescriptorTracing {
21
22
23class FDTraceEntry
24	: public TRACE_ENTRY_SELECTOR(FILE_DESCRIPTOR_TRACING_STACK_TRACE) {
25public:
26	FDTraceEntry(file_descriptor* descriptor)
27		:
28		TraceEntryBase(FILE_DESCRIPTOR_TRACING_STACK_TRACE, 0, true),
29		fDescriptor(descriptor),
30		fReferenceCount(descriptor->ref_count)
31	{
32	}
33
34protected:
35	file_descriptor*		fDescriptor;
36	int32					fReferenceCount;
37};
38
39
40class NewFD : public FDTraceEntry {
41public:
42	NewFD(io_context* context, int fd, file_descriptor* descriptor)
43		:
44		FDTraceEntry(descriptor),
45		fContext(context),
46		fFD(fd)
47	{
48		Initialized();
49	}
50
51	virtual void AddDump(TraceOutput& out)
52	{
53		out.Print("fd new:     descriptor: %p (%" B_PRId32 "), context: %p, "
54			"fd: %d", fDescriptor, fReferenceCount, fContext, fFD);
55	}
56
57private:
58	io_context*	fContext;
59	int			fFD;
60};
61
62
63class PutFD : public FDTraceEntry {
64public:
65	PutFD(file_descriptor* descriptor)
66		:
67		FDTraceEntry(descriptor)
68	{
69		Initialized();
70	}
71
72	virtual void AddDump(TraceOutput& out)
73	{
74		out.Print("fd put:     descriptor: %p (%" B_PRId32 ")", fDescriptor,
75			fReferenceCount);
76	}
77};
78
79
80class GetFD : public FDTraceEntry {
81public:
82	GetFD(io_context* context, int fd, file_descriptor* descriptor)
83		:
84		FDTraceEntry(descriptor),
85		fContext(context),
86		fFD(fd)
87	{
88		Initialized();
89	}
90
91	virtual void AddDump(TraceOutput& out)
92	{
93		out.Print("fd get:     descriptor: %p (%" B_PRId32 "), context: %p, "
94			"fd: %d", fDescriptor, fReferenceCount, fContext, fFD);
95	}
96
97private:
98	io_context*	fContext;
99	int			fFD;
100};
101
102
103class RemoveFD : public FDTraceEntry {
104public:
105	RemoveFD(io_context* context, int fd, file_descriptor* descriptor)
106		:
107		FDTraceEntry(descriptor),
108		fContext(context),
109		fFD(fd)
110	{
111		Initialized();
112	}
113
114	virtual void AddDump(TraceOutput& out)
115	{
116		out.Print("fd remove:  descriptor: %p (%" B_PRId32 "), context: %p, "
117			"fd: %d", fDescriptor, fReferenceCount, fContext, fFD);
118	}
119
120private:
121	io_context*	fContext;
122	int			fFD;
123};
124
125
126class Dup2FD : public FDTraceEntry {
127public:
128	Dup2FD(io_context* context, int oldFD, int newFD)
129		:
130		FDTraceEntry(context->fds[oldFD]),
131		fContext(context),
132		fEvictedDescriptor(context->fds[newFD]),
133		fEvictedReferenceCount(
134			fEvictedDescriptor != NULL ? fEvictedDescriptor->ref_count : 0),
135		fOldFD(oldFD),
136		fNewFD(newFD)
137	{
138		Initialized();
139	}
140
141	virtual void AddDump(TraceOutput& out)
142	{
143		out.Print("fd dup2:    descriptor: %p (%" B_PRId32 "), context: %p, "
144			"fd: %d -> %d, evicted: %p (%" B_PRId32 ")", fDescriptor,
145			fReferenceCount, fContext, fOldFD, fNewFD, fEvictedDescriptor,
146			fEvictedReferenceCount);
147	}
148
149private:
150	io_context*			fContext;
151	file_descriptor*	fEvictedDescriptor;
152	int32				fEvictedReferenceCount;
153	int					fOldFD;
154	int					fNewFD;
155};
156
157
158class InheritFD : public FDTraceEntry {
159public:
160	InheritFD(io_context* context, int fd, file_descriptor* descriptor,
161		io_context* parentContext)
162		:
163		FDTraceEntry(descriptor),
164		fContext(context),
165		fParentContext(parentContext),
166		fFD(fd)
167	{
168		Initialized();
169	}
170
171	virtual void AddDump(TraceOutput& out)
172	{
173		out.Print("fd inherit: descriptor: %p (%" B_PRId32 "), context: %p, "
174			"fd: %d, parentContext: %p", fDescriptor, fReferenceCount, fContext,
175			fFD, fParentContext);
176	}
177
178private:
179	io_context*	fContext;
180	io_context*	fParentContext;
181	int			fFD;
182};
183
184
185}	// namespace FileDescriptorTracing
186
187#	define TFD(x)	new(std::nothrow) FileDescriptorTracing::x
188
189#else
190#	define TFD(x)
191#endif	// FILE_DESCRIPTOR_TRACING
192
193
194// #pragma mark - IO Context Tracing
195
196
197
198#if IO_CONTEXT_TRACING
199
200namespace IOContextTracing {
201
202
203class IOContextTraceEntry : public AbstractTraceEntry {
204public:
205	IOContextTraceEntry(io_context* context)
206		:
207		fContext(context)
208	{
209#if IO_CONTEXT_TRACING_STACK_TRACE
210		fStackTrace = capture_tracing_stack_trace(
211			IO_CONTEXT_TRACING_STACK_TRACE, 0, false);
212#endif
213	}
214
215#if IO_CONTEXT_TRACING_STACK_TRACE
216	virtual void DumpStackTrace(TraceOutput& out)
217	{
218		out.PrintStackTrace(fStackTrace);
219	}
220#endif
221
222protected:
223	io_context*				fContext;
224#if IO_CONTEXT_TRACING_STACK_TRACE
225	tracing_stack_trace*	fStackTrace;
226#endif
227};
228
229
230class NewIOContext : public IOContextTraceEntry {
231public:
232	NewIOContext(io_context* context, io_context* parentContext)
233		:
234		IOContextTraceEntry(context),
235		fParentContext(parentContext)
236	{
237		Initialized();
238	}
239
240	virtual void AddDump(TraceOutput& out)
241	{
242		out.Print("iocontext new:    context: %p, parent: %p", fContext,
243			fParentContext);
244	}
245
246private:
247	io_context*	fParentContext;
248};
249
250
251class FreeIOContext : public IOContextTraceEntry {
252public:
253	FreeIOContext(io_context* context)
254		:
255		IOContextTraceEntry(context)
256	{
257		Initialized();
258	}
259
260	virtual void AddDump(TraceOutput& out)
261	{
262		out.Print("iocontext free:   context: %p", fContext);
263	}
264};
265
266
267class ResizeIOContext : public IOContextTraceEntry {
268public:
269	ResizeIOContext(io_context* context, uint32 newTableSize)
270		:
271		IOContextTraceEntry(context),
272		fOldTableSize(context->table_size),
273		fNewTableSize(newTableSize)
274	{
275		Initialized();
276	}
277
278	virtual void AddDump(TraceOutput& out)
279	{
280		out.Print("iocontext resize: context: %p, size: %" B_PRIu32	" -> %"
281			B_PRIu32, fContext, fOldTableSize, fNewTableSize);
282	}
283
284private:
285	uint32	fOldTableSize;
286	uint32	fNewTableSize;
287};
288
289
290}	// namespace IOContextTracing
291
292#	define TIOC(x)	new(std::nothrow) IOContextTracing::x
293
294#else
295#	define TIOC(x)
296#endif	// IO_CONTEXT_TRACING
297
298
299#endif // VFS_TRACING_H
300