Home | History | Annotate | Download | only in drd
      1 /* -*- mode: C; c-basic-offset: 3; -*- */
      2 /*
      3   This file is part of drd, a thread error detector.
      4 
      5   Copyright (C) 2006-2010 Bart Van Assche <bvanassche (at) acm.org>.
      6 
      7   This program is free software; you can redistribute it and/or
      8   modify it under the terms of the GNU General Public License as
      9   published by the Free Software Foundation; either version 2 of the
     10   License, or (at your option) any later version.
     11 
     12   This program is distributed in the hope that it will be useful, but
     13   WITHOUT ANY WARRANTY; without even the implied warranty of
     14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15   General Public License for more details.
     16 
     17   You should have received a copy of the GNU General Public License
     18   along with this program; if not, write to the Free Software
     19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
     20   02111-1307, USA.
     21 
     22   The GNU General Public License is contained in the file COPYING.
     23 */
     24 
     25 
     26 #include "drd_clientobj.h"
     27 #include "drd_suppression.h"
     28 #include "pub_tool_basics.h"
     29 #include "pub_tool_libcassert.h"
     30 #include "pub_tool_libcbase.h"
     31 #include "pub_tool_libcprint.h"   // VG_(message)()
     32 #include "pub_tool_mallocfree.h"
     33 #include "pub_tool_options.h"     // VG_(clo_backtrace_size)
     34 #include "pub_tool_oset.h"
     35 #include "pub_tool_stacktrace.h"
     36 #include "pub_tool_threadstate.h" // VG_(get_running_tid)()
     37 
     38 
     39 /* Local variables. */
     40 
     41 static OSet* s_clientobj_set;
     42 static Bool s_trace_clientobj;
     43 
     44 
     45 /* Local functions. */
     46 
     47 static Bool clientobj_remove_obj(DrdClientobj* const p);
     48 
     49 
     50 /* Function definitions. */
     51 
     52 void DRD_(clientobj_set_trace)(const Bool trace)
     53 {
     54    s_trace_clientobj = trace;
     55 }
     56 
     57 /** Initialize the client object set. */
     58 void DRD_(clientobj_init)(void)
     59 {
     60    tl_assert(s_clientobj_set == 0);
     61    s_clientobj_set = VG_(OSetGen_Create)(0, 0, VG_(malloc),
     62                                          "drd.clientobj.ci.1", VG_(free));
     63    tl_assert(s_clientobj_set);
     64 }
     65 
     66 /**
     67  * Free the memory allocated for the client object set.
     68  *
     69  * @pre Client object set is empty.
     70  */
     71 void DRD_(clientobj_cleanup)(void)
     72 {
     73    tl_assert(s_clientobj_set);
     74    tl_assert(VG_(OSetGen_Size)(s_clientobj_set) == 0);
     75    VG_(OSetGen_Destroy)(s_clientobj_set);
     76    s_clientobj_set = 0;
     77 }
     78 
     79 /**
     80  * Return the data associated with the client object at client address addr.
     81  * Return 0 if there is no client object in the set with the specified start
     82  * address.
     83  */
     84 DrdClientobj* DRD_(clientobj_get_any)(const Addr addr)
     85 {
     86    return VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
     87 }
     88 
     89 /**
     90  * Return the data associated with the client object at client address addr
     91  * and that has object type t. Return 0 if there is no client object in the
     92  * set with the specified start address.
     93  */
     94 DrdClientobj* DRD_(clientobj_get)(const Addr addr, const ObjType t)
     95 {
     96    DrdClientobj* p;
     97    p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
     98    if (p && p->any.type == t)
     99       return p;
    100    return 0;
    101 }
    102 
    103 /** Return true if and only if the address range of any client object overlaps
    104  *  with the specified address range.
    105  */
    106 Bool DRD_(clientobj_present)(const Addr a1, const Addr a2)
    107 {
    108    DrdClientobj *p;
    109 
    110    tl_assert(a1 <= a2);
    111    VG_(OSetGen_ResetIter)(s_clientobj_set);
    112    for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0; )
    113    {
    114       if (a1 <= p->any.a1 && p->any.a1 < a2)
    115       {
    116          return True;
    117       }
    118    }
    119    return False;
    120 }
    121 
    122 /**
    123  * Add state information for the client object at client address addr and
    124  * of type t. Suppress data race reports on the address range [addr,addr+size[.
    125  *
    126  * @pre No other client object is present in the address range [addr,addr+size[.
    127  */
    128 DrdClientobj* DRD_(clientobj_add)(const Addr a1, const ObjType t)
    129 {
    130    DrdClientobj* p;
    131 
    132    tl_assert(! DRD_(clientobj_present)(a1, a1 + 1));
    133    tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == 0);
    134 
    135    if (s_trace_clientobj)
    136    {
    137       VG_(message)(Vg_UserMsg, "Adding client object 0x%lx of type %d\n", a1, t);
    138    }
    139 
    140    p = VG_(OSetGen_AllocNode)(s_clientobj_set, sizeof(*p));
    141    VG_(memset)(p, 0, sizeof(*p));
    142    p->any.a1   = a1;
    143    p->any.type = t;
    144    p->any.first_observed_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
    145    VG_(OSetGen_Insert)(s_clientobj_set, p);
    146    tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == p);
    147    if (t == ClientHbvar)
    148       DRD_(mark_hbvar)(a1);
    149    else
    150       DRD_(start_suppression)(a1, a1 + 1, "clientobj");
    151    return p;
    152 }
    153 
    154 /**
    155  * Remove the information that was stored about the client object.
    156  *
    157  * @param[in] addr Address of the client object in the client address space.
    158  * @param[in] t    Type of the client object.
    159  */
    160 Bool DRD_(clientobj_remove)(const Addr addr, const ObjType t)
    161 {
    162    DrdClientobj* p;
    163 
    164    p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
    165    tl_assert(p);
    166    tl_assert(p->any.type == t);
    167    return clientobj_remove_obj(p);
    168 }
    169 
    170 /**
    171  * Remove the information that was stored about the client object p.
    172  *
    173  * @note The order of operations below is important. The client object is
    174  *   removed from the client object set after the cleanup function has been
    175  *   called such that if the cleanup function can still use the function
    176  *   DRD_(clientobj_get_any)(). This happens e.g. in the function
    177  *   first_observed() in drd_error.c.
    178  */
    179 static Bool clientobj_remove_obj(DrdClientobj* const p)
    180 {
    181    tl_assert(p);
    182 
    183    if (s_trace_clientobj)
    184    {
    185       VG_(message)(Vg_UserMsg, "Removing client object 0x%lx of type %d\n",
    186                    p->any.a1, p->any.type);
    187 #if 0
    188       VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
    189                                  VG_(clo_backtrace_size));
    190 #endif
    191    }
    192 
    193    tl_assert(p->any.cleanup);
    194    (*p->any.cleanup)(p);
    195    VG_(OSetGen_Remove)(s_clientobj_set, &p->any.a1);
    196    VG_(OSetGen_FreeNode)(s_clientobj_set, p);
    197    return True;
    198 }
    199 
    200 /**
    201  * Clean up all client objects p for which their start address p->any.a1 fits
    202  * inside the address range [ a1, a2 [.
    203  *
    204  * @note The implementation of this function relies on the fact that the
    205  *   data in s_clientobj_set is sorted on the start address of client objects.
    206  */
    207 void DRD_(clientobj_stop_using_mem)(const Addr a1, const Addr a2)
    208 {
    209    Addr removed_at;
    210    DrdClientobj* p;
    211 
    212    tl_assert(s_clientobj_set);
    213 
    214    if (! DRD_(range_contains_suppression_or_hbvar)(a1, a2))
    215       return;
    216 
    217    VG_(OSetGen_ResetIterAt)(s_clientobj_set, &a1);
    218    for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0 && p->any.a1 < a2; )
    219    {
    220       tl_assert(a1 <= p->any.a1);
    221       removed_at = p->any.a1;
    222       clientobj_remove_obj(p);
    223       /*
    224        * The above call removes an element from the oset and hence
    225        * invalidates the iterator. Restore the iterator.
    226        */
    227       VG_(OSetGen_ResetIterAt)(s_clientobj_set, &removed_at);
    228    }
    229 }
    230 
    231 /**
    232  * Delete the per-thread information stored in client objects for the
    233  * specified thread.
    234  */
    235 void DRD_(clientobj_delete_thread)(const DrdThreadId tid)
    236 {
    237    DrdClientobj *p;
    238 
    239    VG_(OSetGen_ResetIter)(s_clientobj_set);
    240    for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0; )
    241    {
    242       if (p->any.delete_thread)
    243       {
    244          (*p->any.delete_thread)(p, tid);
    245       }
    246    }
    247 }
    248 
    249 const char* DRD_(clientobj_type_name)(const ObjType t)
    250 {
    251    switch (t)
    252    {
    253    case ClientMutex:     return "mutex";
    254    case ClientCondvar:   return "cond";
    255    case ClientHbvar:     return "order annotation";
    256    case ClientSemaphore: return "semaphore";
    257    case ClientBarrier:   return "barrier";
    258    case ClientRwlock:    return "rwlock";
    259    }
    260    return "(unknown)";
    261 }
    262