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 20 namespace FileDescriptorTracing { 21 22 23 class FDTraceEntry TRACE_ENTRY_SELECTOR(FILE_DESCRIPTOR_TRACING_STACK_TRACE)24 : public TRACE_ENTRY_SELECTOR(FILE_DESCRIPTOR_TRACING_STACK_TRACE) { 25 public: 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 34 protected: 35 file_descriptor* fDescriptor; 36 int32 fReferenceCount; 37 }; 38 39 40 class NewFD : public FDTraceEntry { 41 public: NewFD(io_context * context,int fd,file_descriptor * descriptor)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 AddDump(TraceOutput & out)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 57 private: 58 io_context* fContext; 59 int fFD; 60 }; 61 62 63 class PutFD : public FDTraceEntry { 64 public: PutFD(file_descriptor * descriptor)65 PutFD(file_descriptor* descriptor) 66 : 67 FDTraceEntry(descriptor) 68 { 69 Initialized(); 70 } 71 AddDump(TraceOutput & out)72 virtual void AddDump(TraceOutput& out) 73 { 74 out.Print("fd put: descriptor: %p (%" B_PRId32 ")", fDescriptor, 75 fReferenceCount); 76 } 77 }; 78 79 80 class GetFD : public FDTraceEntry { 81 public: GetFD(io_context * context,int fd,file_descriptor * descriptor)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 AddDump(TraceOutput & out)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 97 private: 98 io_context* fContext; 99 int fFD; 100 }; 101 102 103 class RemoveFD : public FDTraceEntry { 104 public: RemoveFD(io_context * context,int fd,file_descriptor * descriptor)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 AddDump(TraceOutput & out)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 120 private: 121 io_context* fContext; 122 int fFD; 123 }; 124 125 126 class Dup2FD : public FDTraceEntry { 127 public: Dup2FD(io_context * context,int oldFD,int newFD)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 AddDump(TraceOutput & out)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 149 private: 150 io_context* fContext; 151 file_descriptor* fEvictedDescriptor; 152 int32 fEvictedReferenceCount; 153 int fOldFD; 154 int fNewFD; 155 }; 156 157 158 class InheritFD : public FDTraceEntry { 159 public: InheritFD(io_context * context,int fd,file_descriptor * descriptor,io_context * parentContext)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 AddDump(TraceOutput & out)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 178 private: 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 200 namespace IOContextTracing { 201 202 203 class IOContextTraceEntry : public AbstractTraceEntry { 204 public: IOContextTraceEntry(io_context * context)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 DumpStackTrace(TraceOutput & out)216 virtual void DumpStackTrace(TraceOutput& out) 217 { 218 out.PrintStackTrace(fStackTrace); 219 } 220 #endif 221 222 protected: 223 io_context* fContext; 224 #if IO_CONTEXT_TRACING_STACK_TRACE 225 tracing_stack_trace* fStackTrace; 226 #endif 227 }; 228 229 230 class NewIOContext : public IOContextTraceEntry { 231 public: NewIOContext(io_context * context,io_context * parentContext)232 NewIOContext(io_context* context, io_context* parentContext) 233 : 234 IOContextTraceEntry(context), 235 fParentContext(parentContext) 236 { 237 Initialized(); 238 } 239 AddDump(TraceOutput & out)240 virtual void AddDump(TraceOutput& out) 241 { 242 out.Print("iocontext new: context: %p, parent: %p", fContext, 243 fParentContext); 244 } 245 246 private: 247 io_context* fParentContext; 248 }; 249 250 251 class FreeIOContext : public IOContextTraceEntry { 252 public: FreeIOContext(io_context * context)253 FreeIOContext(io_context* context) 254 : 255 IOContextTraceEntry(context) 256 { 257 Initialized(); 258 } 259 AddDump(TraceOutput & out)260 virtual void AddDump(TraceOutput& out) 261 { 262 out.Print("iocontext free: context: %p", fContext); 263 } 264 }; 265 266 267 class ResizeIOContext : public IOContextTraceEntry { 268 public: ResizeIOContext(io_context * context,uint32 newTableSize)269 ResizeIOContext(io_context* context, uint32 newTableSize) 270 : 271 IOContextTraceEntry(context), 272 fOldTableSize(context->table_size), 273 fNewTableSize(newTableSize) 274 { 275 Initialized(); 276 } 277 AddDump(TraceOutput & out)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 284 private: 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