~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to util/debug_util.h

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// debug_util.h
 
2
 
 
3
/*    Copyright 2009 10gen Inc.
 
4
 *
 
5
 *    Licensed under the Apache License, Version 2.0 (the "License");
 
6
 *    you may not use this file except in compliance with the License.
 
7
 *    You may obtain a copy of the License at
 
8
 *
 
9
 *    http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 *    Unless required by applicable law or agreed to in writing, software
 
12
 *    distributed under the License is distributed on an "AS IS" BASIS,
 
13
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 *    See the License for the specific language governing permissions and
 
15
 *    limitations under the License.
 
16
 */
 
17
 
 
18
#pragma once
 
19
 
 
20
#ifndef _WIN32
 
21
#include <signal.h>
 
22
#endif // ndef _WIN32
 
23
 
 
24
namespace mongo {
 
25
 
 
26
// for debugging
 
27
    typedef struct _Ints {
 
28
        int i[100];
 
29
    } *Ints;
 
30
    typedef struct _Chars {
 
31
        char c[200];
 
32
    } *Chars;
 
33
 
 
34
    typedef char CHARS[400];
 
35
 
 
36
    typedef struct _OWS {
 
37
        int size;
 
38
        char type;
 
39
        char string[400];
 
40
    } *OWS;
 
41
 
 
42
// for now, running on win32 means development not production --
 
43
// use this to log things just there.
 
44
#if defined(_WIN32)
 
45
#define WIN if( 1 )
 
46
#else
 
47
#define WIN if( 0 )
 
48
#endif
 
49
 
 
50
#if defined(_DEBUG)
 
51
#define DEV if( 1 )
 
52
#else
 
53
#define DEV if( 0 )
 
54
#endif
 
55
 
 
56
#define DEBUGGING if( 0 )
 
57
 
 
58
// The following declare one unique counter per enclosing function.
 
59
// NOTE The implementation double-increments on a match, but we don't really care.
 
60
#define SOMETIMES( occasion, howOften ) for( static unsigned occasion = 0; ++occasion % howOften == 0; )
 
61
#define OCCASIONALLY SOMETIMES( occasionally, 16 )
 
62
#define RARELY SOMETIMES( rarely, 128 )
 
63
#define ONCE for( static bool undone = true; undone; undone = false ) 
 
64
 
 
65
#if defined(_WIN32)
 
66
#define strcasecmp _stricmp
 
67
#endif
 
68
 
 
69
    // Sets SIGTRAP handler to launch GDB
 
70
    // Noop unless on *NIX and compiled with _DEBUG
 
71
    void setupSIGTRAPforGDB();
 
72
 
 
73
#if defined(_WIN32)
 
74
    inline void breakpoint() {} //noop
 
75
#else // defined(_WIN32)
 
76
    // code to raise a breakpoint in GDB
 
77
    inline void breakpoint(){
 
78
        ONCE {
 
79
            //prevent SIGTRAP from crashing the program if default action is specified and we are not in gdb
 
80
            struct sigaction current;
 
81
            sigaction(SIGTRAP, NULL, &current);
 
82
            if (current.sa_handler == SIG_DFL){
 
83
                signal(SIGTRAP, SIG_IGN);
 
84
            }
 
85
        }
 
86
 
 
87
        raise(SIGTRAP);
 
88
    }
 
89
#endif // defined(_WIN32)
 
90
 
 
91
    // conditional breakpoint
 
92
    inline void breakif(bool test){
 
93
        if (test)
 
94
            breakpoint();
 
95
    }
 
96
   
 
97
} // namespace mongo