Home | History | Annotate | Download | only in tsan
      1 /* Copyright (c) 2008-2010, Google Inc.
      2  * All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Neither the name of Google Inc. nor the names of its
     11  * contributors may be used to endorse or promote products derived from
     12  * this software without specific prior written permission.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     17  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     18  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     20  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 // This file is part of ThreadSanitizer, a dynamic data race detector.
     28 // Author: Konstantin Serebryany.
     29 //--------- Head ------------------- {{{1
     30 #ifndef THREAD_SANITIZER_H_
     31 #define THREAD_SANITIZER_H_
     32 
     33 #include "ts_util.h"
     34 
     35 //--------- Utils ------------------- {{{1
     36 #include "ts_util.h"
     37 
     38 void Report(const char *format, ...);
     39 void PcToStrings(uintptr_t pc, bool demangle,
     40                 string *img_name, string *rtn_name,
     41                 string *file_name, int *line_no);
     42 string PcToRtnNameAndFilePos(uintptr_t pc);
     43 string PcToRtnName(uintptr_t pc, bool demangle);
     44 string Demangle(const char *str);
     45 
     46 
     47 //--------- FLAGS ---------------------------------- {{{1
     48 struct FLAGS {
     49   string           input_type; // for ts_offline.
     50                                // Possible values: str, bin, decode.
     51   bool             ignore_stack;
     52   intptr_t         verbosity;
     53   intptr_t         show_stats;  // 0 -- no stats; 1 -- some stats; 2 more stats.
     54   bool             trace_profile;
     55   bool             show_expected_races;
     56   uintptr_t        trace_addr;
     57   uintptr_t        segment_set_recycle_queue_size;
     58   uintptr_t        recent_segments_cache_size;
     59   vector<string>   file_prefix_to_cut;
     60   vector<string>   ignore;
     61   vector<string>   whitelist;
     62   bool             ignore_unknown_pcs;  // Ignore PCs with no debug info.
     63   vector<string>   cut_stack_below;
     64   string           summary_file;
     65   string           log_file;
     66   bool             offline;
     67   intptr_t         max_n_threads;
     68   bool             compress_cache_lines;
     69   bool             unlock_on_mutex_destroy;
     70 
     71   intptr_t         sample_events;
     72   intptr_t         sample_events_depth;
     73 
     74   intptr_t         num_callers;
     75 
     76   intptr_t    keep_history;
     77   bool        pure_happens_before;
     78   bool        free_is_write;
     79   bool        exit_after_main;
     80   bool        demangle;
     81   bool        announce_threads;
     82   bool        full_output;
     83   bool        show_states;
     84   bool        show_proc_self_status;
     85   bool        show_valgrind_context;  // debug-only
     86   bool        suggest_happens_before_arcs;
     87   bool        show_pc;
     88   bool        full_stack_frames;
     89   bool        color;  // Colorify terminal output.
     90   bool        html;  // Output in html format.
     91   bool        show_pid;
     92 
     93   intptr_t  debug_level;
     94   bool        save_ignore_context;  // print stack if ignore_end was forgotten.
     95   vector<string> debug_phase;
     96   intptr_t  trace_level;
     97 
     98   intptr_t     dry_run;
     99   intptr_t     max_sid;
    100   intptr_t     max_sid_before_flush;
    101   intptr_t     max_mem_in_mb;
    102   intptr_t     num_callers_in_history;
    103   intptr_t     flush_period;
    104 
    105   intptr_t     literace_sampling;
    106   bool         start_with_global_ignore_on;
    107 
    108   intptr_t     locking_scheme;  // Used for internal experiments with locking.
    109 
    110   bool         report_races;
    111   bool         thread_coverage;
    112   bool         atomicity;
    113   bool         call_coverage;
    114   string       dump_events;  // The name of log file. Debug mode only.
    115   bool         symbolize;
    116   bool         attach_mode;
    117 
    118   string       tsan_program_name;
    119   string       tsan_url;
    120 
    121   vector<string> suppressions;
    122   bool           generate_suppressions;
    123 
    124   intptr_t     error_exitcode;
    125   bool         trace_children;
    126 
    127   vector<string> race_verifier;
    128   vector<string> race_verifier_extra;
    129   intptr_t       race_verifier_sleep_ms;
    130 
    131   bool nacl_untrusted;
    132 
    133   bool threaded_analysis;
    134 };
    135 
    136 extern FLAGS *G_flags;
    137 
    138 extern bool g_race_verifier_active;
    139 
    140 extern bool debug_expected_races;
    141 extern bool debug_malloc;
    142 extern bool debug_free;
    143 extern bool debug_thread;
    144 extern bool debug_rtn;
    145 extern bool debug_wrap;
    146 extern bool debug_ins;
    147 extern bool debug_shadow_stack;
    148 extern bool debug_race_verifier;
    149 
    150 // -------- CallStack ------------- {{{1
    151 const size_t kMaxCallStackSize = 1 << 12;
    152 
    153 struct CallStackPod {
    154   uintptr_t *end_;
    155   uintptr_t pcs_[kMaxCallStackSize];
    156 };
    157 
    158 struct CallStack: public CallStackPod {
    159 
    160   CallStack() { Clear(); }
    161 
    162   size_t size() { return (size_t)(end_ - pcs_); }
    163   uintptr_t *pcs() { return pcs_; }
    164 
    165   bool empty() { return end_ == pcs_; }
    166 
    167   uintptr_t &back() {
    168     DCHECK(!empty());
    169     return *(end_ - 1);
    170   }
    171 
    172   void pop_back() {
    173     DCHECK(!empty());
    174     end_--;
    175   }
    176 
    177   void push_back(uintptr_t pc) {
    178     DCHECK(size() < kMaxCallStackSize);
    179     *end_ = pc;
    180     end_++;
    181   }
    182 
    183   void Clear() {
    184     end_ = pcs_;
    185   }
    186 
    187   uintptr_t &operator[] (size_t i) {
    188     DCHECK(i < size());
    189     return pcs_[i];
    190   }
    191 
    192 };
    193 
    194 //--------- TS Exports ----------------- {{{1
    195 #include "ts_events.h"
    196 #include "ts_trace_info.h"
    197 
    198 struct Thread;
    199 extern void ThreadSanitizerInit();
    200 extern void ThreadSanitizerFini();
    201 // TODO(glider): this is a temporary solution to avoid deadlocks after fork().
    202 #ifdef TS_LLVM
    203 extern void ThreadSanitizerLockAcquire();
    204 extern void ThreadSanitizerLockRelease();
    205 #endif
    206 extern void ThreadSanitizerHandleOneEvent(Event *event);
    207 extern Thread *ThreadSanitizerGetThreadByTid(int32_t tid);
    208 extern void ThreadSanitizerHandleTrace(int32_t tid, TraceInfo *trace_info,
    209                                        uintptr_t *tleb);
    210 extern void ThreadSanitizerHandleTrace(Thread *thr, TraceInfo *trace_info,
    211                                        uintptr_t *tleb);
    212 extern void ThreadSanitizerHandleOneMemoryAccess(Thread *thr, MopInfo mop,
    213                                                  uintptr_t addr);
    214 extern void ThreadSanitizerParseFlags(vector<string>* args);
    215 extern bool ThreadSanitizerWantToInstrumentSblock(uintptr_t pc);
    216 extern bool ThreadSanitizerWantToCreateSegmentsOnSblockEntry(uintptr_t pc);
    217 extern bool ThreadSanitizerIgnoreAccessesBelowFunction(uintptr_t pc);
    218 
    219 enum IGNORE_BELOW_RTN {
    220   IGNORE_BELOW_RTN_UNKNOWN,
    221   IGNORE_BELOW_RTN_NO,
    222   IGNORE_BELOW_RTN_YES
    223 };
    224 
    225 extern void ThreadSanitizerHandleRtnCall(int32_t tid, uintptr_t call_pc,
    226                                          uintptr_t target_pc,
    227                                          IGNORE_BELOW_RTN ignore_below);
    228 
    229 extern void ThreadSanitizerHandleRtnExit(int32_t tid);
    230 
    231 extern void ThreadSanitizerPrintUsage();
    232 extern "C" const char *ThreadSanitizerQuery(const char *query);
    233 extern bool PhaseDebugIsOn(const char *phase_name);
    234 
    235 extern bool g_has_entered_main;
    236 extern bool g_has_exited_main;
    237 
    238 // -------- Stats ------------------- {{{1
    239 #include "ts_stats.h"
    240 extern Stats *G_stats;
    241 
    242 // -------- Expected Race ---------------------- {{{1
    243 // Information about expected races.
    244 struct ExpectedRace {
    245   uintptr_t   ptr;
    246   uintptr_t   size;
    247   bool        is_verifiable;
    248   bool        is_nacl_untrusted;
    249   int         count;
    250   const char *description;
    251   uintptr_t   pc;
    252 };
    253 
    254 ExpectedRace* ThreadSanitizerFindExpectedRace(uintptr_t addr);
    255 
    256 // Tell ThreadSanitizer about the location of NaCl untrusted region.
    257 void ThreadSanitizerNaclUntrustedRegion(uintptr_t mem_start, uintptr_t mem_end);
    258 
    259 // Returns true if accesses and locks at the given address should be ignored
    260 // according to the current NaCl flags (--nacl-untrusted). Always false if not a
    261 // NaCl program.
    262 bool ThreadSanitizerIgnoreForNacl(uintptr_t addr);
    263 
    264 // end. {{{1
    265 #endif  //  THREAD_SANITIZER_H_
    266 
    267 // vim:shiftwidth=2:softtabstop=2:expandtab
    268