debug.hh revision 9554:406fbcf60223
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2003-2005 The Regents of The University of Michigan
312855Sgabeblack@google.com * Copyright (c) 2010 The Hewlett-Packard Development Company
412855Sgabeblack@google.com * All rights reserved.
512855Sgabeblack@google.com *
612855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
712855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
812855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
912855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
1012855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1112855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1212855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1312855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1412855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1512855Sgabeblack@google.com * this software without specific prior written permission.
1612855Sgabeblack@google.com *
1712855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1812855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1912855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2012855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2112855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2212855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2312855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2412855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2512855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2612855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2712855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2812855Sgabeblack@google.com *
2912855Sgabeblack@google.com * Authors: Nathan Binkert
3012855Sgabeblack@google.com */
3112855Sgabeblack@google.com
3212855Sgabeblack@google.com#ifndef __BASE_DEBUG_HH__
3312855Sgabeblack@google.com#define __BASE_DEBUG_HH__
3412855Sgabeblack@google.com
3512855Sgabeblack@google.com#include <map>
3612855Sgabeblack@google.com#include <string>
3712855Sgabeblack@google.com#include <vector>
3812855Sgabeblack@google.com
3912855Sgabeblack@google.comnamespace Debug {
4012855Sgabeblack@google.com
4112855Sgabeblack@google.comvoid breakpoint();
4212855Sgabeblack@google.com
4312855Sgabeblack@google.comclass Flag
4412855Sgabeblack@google.com{
4512855Sgabeblack@google.com  protected:
4612855Sgabeblack@google.com    const char *_name;
4712855Sgabeblack@google.com    const char *_desc;
4812855Sgabeblack@google.com    std::vector<Flag *> _kids;
4912855Sgabeblack@google.com
5012855Sgabeblack@google.com  public:
5112855Sgabeblack@google.com    Flag(const char *name, const char *desc);
5212855Sgabeblack@google.com    virtual ~Flag();
5312855Sgabeblack@google.com
5412855Sgabeblack@google.com    std::string name() const { return _name; }
5512855Sgabeblack@google.com    std::string desc() const { return _desc; }
5612855Sgabeblack@google.com    std::vector<Flag *> kids() { return _kids; }
5712855Sgabeblack@google.com
5812855Sgabeblack@google.com    virtual void enable() = 0;
5912855Sgabeblack@google.com    virtual void disable() = 0;
6012855Sgabeblack@google.com};
6112855Sgabeblack@google.com
6212855Sgabeblack@google.comclass SimpleFlag : public Flag
6312855Sgabeblack@google.com{
6412855Sgabeblack@google.com  protected:
6512855Sgabeblack@google.com    bool _status;
66
67  public:
68    SimpleFlag(const char *name, const char *desc)
69        : Flag(name, desc)
70    { }
71
72    bool status() const { return _status; }
73    operator bool() const { return _status; }
74    bool operator!() const { return !_status; }
75
76    void enable() { _status = true; }
77    void disable() { _status = false; }
78};
79
80class CompoundFlag : public SimpleFlag
81{
82  protected:
83    void
84    addFlag(Flag &f)
85    {
86        if (&f != NULL)
87            _kids.push_back(&f);
88    }
89
90  public:
91    CompoundFlag(const char *name, const char *desc,
92        Flag &f00 = *(Flag *)0, Flag &f01 = *(Flag *)0,
93        Flag &f02 = *(Flag *)0, Flag &f03 = *(Flag *)0,
94        Flag &f04 = *(Flag *)0, Flag &f05 = *(Flag *)0,
95        Flag &f06 = *(Flag *)0, Flag &f07 = *(Flag *)0,
96        Flag &f08 = *(Flag *)0, Flag &f09 = *(Flag *)0,
97        Flag &f10 = *(Flag *)0, Flag &f11 = *(Flag *)0,
98        Flag &f12 = *(Flag *)0, Flag &f13 = *(Flag *)0,
99        Flag &f14 = *(Flag *)0, Flag &f15 = *(Flag *)0,
100        Flag &f16 = *(Flag *)0, Flag &f17 = *(Flag *)0,
101        Flag &f18 = *(Flag *)0, Flag &f19 = *(Flag *)0)
102        : SimpleFlag(name, desc)
103    {
104        addFlag(f00); addFlag(f01); addFlag(f02); addFlag(f03); addFlag(f04);
105        addFlag(f05); addFlag(f06); addFlag(f07); addFlag(f08); addFlag(f09);
106        addFlag(f10); addFlag(f11); addFlag(f12); addFlag(f13); addFlag(f14);
107        addFlag(f15); addFlag(f16); addFlag(f17); addFlag(f18); addFlag(f19);
108    }
109
110    void enable();
111    void disable();
112};
113
114typedef std::map<std::string, Flag *> FlagsMap;
115FlagsMap &allFlags();
116
117Flag *findFlag(const std::string &name);
118
119bool changeFlag(const char *s, bool value);
120
121} // namespace Debug
122
123void setDebugFlag(const char *string);
124
125void clearDebugFlag(const char *string);
126
127void dumpDebugFlags();
128
129#endif // __BASE_DEBUG_HH__
130