]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/kern/kobject_dbg.cpp
7d1b150366428ac9c4fac539c9472409dec791d9
[l4.git] / kernel / fiasco / src / kern / kobject_dbg.cpp
1 INTERFACE:
2
3 class Kobject_dbg
4 {
5 };
6
7 //----------------------------------------------------------------------------
8 INTERFACE[debug]:
9
10 #include "spin_lock.h"
11 #include "lock_guard.h"
12 #include <dlist>
13 #include <hlist>
14
15 class Kobject;
16
17 EXTENSION class Kobject_dbg : public cxx::D_list_item
18 {
19   friend class Jdb_kobject;
20   friend class Jdb_kobject_list;
21   friend class Jdb_mapdb;
22
23 public:
24   class Dbg_extension : public cxx::H_list_item
25   {
26   public:
27     virtual ~Dbg_extension() = 0;
28   };
29
30 public:
31   typedef cxx::H_list<Dbg_extension> Dbg_ext_list;
32   Dbg_ext_list _jdb_data;
33
34 private:
35   Mword _dbg_id;
36
37 public:
38   Mword dbg_id() const { return _dbg_id; }
39   virtual Address kobject_start_addr() const = 0;
40   virtual Mword kobject_size() const = 0;
41   virtual ~Kobject_dbg() = 0;
42
43
44   typedef cxx::D_list<Kobject_dbg> Kobject_list;
45   typedef Kobject_list::Iterator Iterator;
46   typedef Kobject_list::Const_iterator Const_iterator;
47
48   static Spin_lock<> _kobjects_lock;
49   static Kobject_list _kobjects;
50
51   static Iterator begin() { return _kobjects.begin(); }
52   static Iterator end() { return _kobjects.end(); }
53
54 private:
55   static unsigned long _next_dbg_id;
56 };
57
58
59 //----------------------------------------------------------------------------
60 IMPLEMENTATION[debug]:
61 #include "static_init.h"
62 Spin_lock<> Kobject_dbg::_kobjects_lock;
63 Kobject_dbg::Kobject_list Kobject_dbg::_kobjects INIT_PRIORITY(101);
64 unsigned long Kobject_dbg::_next_dbg_id;
65
66 IMPLEMENT inline Kobject_dbg::Dbg_extension::~Dbg_extension() {}
67
68 PUBLIC static
69 Kobject_dbg::Iterator
70 Kobject_dbg::pointer_to_obj(void const *p)
71 {
72   for (Iterator l = _kobjects.begin(); l != _kobjects.end(); ++l)
73     {
74       Mword a = l->kobject_start_addr();
75       if (a <= Mword(p) && Mword(p) < (a + l->kobject_size()))
76         return l;
77     }
78   return _kobjects.end();
79 }
80
81 PUBLIC static
82 unsigned long
83 Kobject_dbg::pointer_to_id(void const *p)
84 {
85   Iterator o = pointer_to_obj(p);
86   if (o != _kobjects.end())
87     return o->dbg_id();
88   return ~0UL;
89 }
90
91 PUBLIC static
92 bool
93 Kobject_dbg::is_kobj(void const *o)
94 {
95   return pointer_to_obj(o) != _kobjects.end();
96 }
97
98 PUBLIC static
99 Kobject_dbg::Iterator
100 Kobject_dbg::id_to_obj(unsigned long id)
101 {
102   for (Iterator l = _kobjects.begin(); l != _kobjects.end(); ++l)
103     {
104       if (l->dbg_id() == id)
105         return l;
106     }
107   return end();
108 }
109
110 PUBLIC static
111 unsigned long
112 Kobject_dbg::obj_to_id(void const *o)
113 {
114   return pointer_to_id(o);
115 }
116
117
118 PROTECTED
119 Kobject_dbg::Kobject_dbg()
120 {
121   Lock_guard<decltype(_kobjects_lock)> guard(&_kobjects_lock);
122
123   _dbg_id = _next_dbg_id++;
124   _kobjects.push_back(this);
125 }
126
127 IMPLEMENT inline
128 Kobject_dbg::~Kobject_dbg()
129 {
130     {
131       Lock_guard<decltype(_kobjects_lock)> guard(&_kobjects_lock);
132       _kobjects.remove(this);
133     }
134
135   while (Dbg_extension *ex = _jdb_data.front())
136     delete ex;
137 }
138
139 //---------------------------------------------------------------------------
140 IMPLEMENTATION [!debug]:
141
142 PUBLIC inline
143 unsigned long
144 Kobject_dbg::dbg_id() const
145 { return 0; }
146
147 PUBLIC static inline
148 unsigned long
149 Kobject_dbg::dbg_id(void const *)
150 { return ~0UL; }
151
152 PUBLIC static inline
153 Kobject_dbg *
154 Kobject_dbg::pointer_to_obj(void const *)
155 { return 0; }
156
157 PUBLIC static inline
158 unsigned long
159 Kobject_dbg::pointer_to_id(void const *)
160 { return ~0UL; }
161
162 PUBLIC static
163 bool
164 Kobject_dbg::is_kobj(void const *)
165 { return false; }
166
167 PUBLIC static
168 Kobject_dbg *
169 Kobject_dbg::id_to_obj(unsigned long)
170 { return 0; }
171
172 PUBLIC static
173 unsigned long
174 Kobject_dbg::obj_to_id(void const *)
175 { return ~0UL; }