16fbecdd97512bd7d9ccef130e99650d446b50444Alexey Samsonov//===-- tsan_trace.h --------------------------------------------*- C++ -*-===//
27ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
37ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//                     The LLVM Compiler Infrastructure
47ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
57ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// This file is distributed under the University of Illinois Open Source
67ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// License. See LICENSE.TXT for details.
77ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
87ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//===----------------------------------------------------------------------===//
97ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
107ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// This file is a part of ThreadSanitizer (TSan), a race detector.
117ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
127ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//===----------------------------------------------------------------------===//
137ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#ifndef TSAN_TRACE_H
147ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#define TSAN_TRACE_H
157ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
167ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_defs.h"
177ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_mutex.h"
185d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines#include "tsan_stack_trace.h"
19ad9da372f962495b3487685232d09390be841b1cDmitry Vyukov#include "tsan_mutexset.h"
207ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
217ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanynamespace __tsan {
227ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
23d698edc4f74a17048eef3342a9fa42b3ebba802aDmitry Vyukovconst int kTracePartSizeBits = 14;
24d698edc4f74a17048eef3342a9fa42b3ebba802aDmitry Vyukovconst int kTracePartSize = 1 << kTracePartSizeBits;
25d698edc4f74a17048eef3342a9fa42b3ebba802aDmitry Vyukovconst int kTraceParts = 4 * 1024 * 1024 / kTracePartSize;
26ffad0c4bc88056f5c9360af792ef2e6c1e8259bfDmitry Vyukovconst int kTraceSize = kTracePartSize * kTraceParts;
277ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
287ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// Must fit into 3 bits.
297ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyenum EventType {
307ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  EventTypeMop,
317ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  EventTypeFuncEnter,
327ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  EventTypeFuncExit,
337ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  EventTypeLock,
347ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  EventTypeUnlock,
357ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  EventTypeRLock,
362135d8a7f4ba30fe35ed02d5e6ffd59a95b26219Alexey Samsonov  EventTypeRUnlock
377ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany};
387ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
397ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// Represents a thread event (from most significant bit):
407ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// u64 typ  : 3;   // EventType.
417ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// u64 addr : 61;  // Associated pc.
427ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanytypedef u64 Event;
437ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
447ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanystruct TraceHeader {
457ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  StackTrace stack0;  // Start stack for the trace.
469ad7c32720dfa1287f8cfd481e5d583435178caeDmitry Vyukov  u64        epoch0;  // Start epoch for the trace.
47ad9da372f962495b3487685232d09390be841b1cDmitry Vyukov  MutexSet   mset0;
4825d1c799087af5757ab6efc4a77558565fb1744aDmitry Vyukov#ifndef TSAN_GO
490ab628c61594eb80612e5389d9c33da0e0d70c66Dmitry Vyukov  uptr       stack0buf[kTraceStackSize];
5025d1c799087af5757ab6efc4a77558565fb1744aDmitry Vyukov#endif
519ad7c32720dfa1287f8cfd481e5d583435178caeDmitry Vyukov
529ad7c32720dfa1287f8cfd481e5d583435178caeDmitry Vyukov  TraceHeader()
5325d1c799087af5757ab6efc4a77558565fb1744aDmitry Vyukov#ifndef TSAN_GO
540ab628c61594eb80612e5389d9c33da0e0d70c66Dmitry Vyukov      : stack0(stack0buf, kTraceStackSize)
5525d1c799087af5757ab6efc4a77558565fb1744aDmitry Vyukov#else
5625d1c799087af5757ab6efc4a77558565fb1744aDmitry Vyukov      : stack0()
5725d1c799087af5757ab6efc4a77558565fb1744aDmitry Vyukov#endif
589ad7c32720dfa1287f8cfd481e5d583435178caeDmitry Vyukov      , epoch0() {
599ad7c32720dfa1287f8cfd481e5d583435178caeDmitry Vyukov  }
607ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany};
617ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
627ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanystruct Trace {
637ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  TraceHeader headers[kTraceParts];
647ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  Mutex mtx;
6501a7ce809bf7cc627d73c045c70bcca9891f632cDmitry Vyukov#ifndef TSAN_GO
6601a7ce809bf7cc627d73c045c70bcca9891f632cDmitry Vyukov  // Must be last to catch overflow as paging fault.
6701a7ce809bf7cc627d73c045c70bcca9891f632cDmitry Vyukov  // Go shadow stack is dynamically allocated.
6801a7ce809bf7cc627d73c045c70bcca9891f632cDmitry Vyukov  uptr shadow_stack[kShadowStackSize];
6901a7ce809bf7cc627d73c045c70bcca9891f632cDmitry Vyukov#endif
707ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
717ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  Trace()
727ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    : mtx(MutexTypeTrace, StatMtxTrace) {
737ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
747ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany};
757ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
767ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}  // namespace __tsan
777ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
787ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#endif  // TSAN_TRACE_H
79