]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/jdb/jdb_kobject_names.cpp
update
[l4.git] / kernel / fiasco / src / jdb / jdb_kobject_names.cpp
1 //-----------------------------------------------------------------------
2 INTERFACE:
3
4 #include "config.h"
5 #include "jdb_kobject.h"
6 #include "l4_types.h"
7 #include "initcalls.h"
8
9
10 class Jdb_kobject_name : public Jdb_kobject_extension
11 {
12 public:
13   static char const *const static_type;
14   virtual char const *type() const { return static_type; }
15
16   ~Jdb_kobject_name() {}
17
18   void *operator new (size_t) throw();
19   void operator delete (void *);
20
21 private:
22
23   char _name[16];
24
25   static Jdb_kobject_name *_names;
26 };
27
28 //-----------------------------------------------------------------------
29 IMPLEMENTATION:
30
31 #include <cstdio>
32
33 #include <feature.h>
34 #include "context.h"
35 #include "kmem_alloc.h"
36 #include "minmax.h"
37 #include "panic.h"
38 #include "space.h"
39 #include "thread.h"
40 #include "static_init.h"
41
42
43
44 enum
45 {
46   Name_buffer_size = 8192,
47   Name_entries = Name_buffer_size / sizeof(Jdb_kobject_name),
48 };
49
50
51 char const *const Jdb_kobject_name::static_type = "Jdb_kobject_names";
52 Jdb_kobject_name *Jdb_kobject_name::_names;
53
54
55 PUBLIC
56 unsigned
57 Jdb_kobject_name::max_len()
58 { return sizeof(_name); }
59
60 PUBLIC
61 void
62 Jdb_kobject_name::name(char const *name)
63 {
64   strncpy(_name, name, sizeof(_name));
65 }
66
67 PUBLIC
68 Jdb_kobject_name::Jdb_kobject_name()
69 { _name[0] = 0; }
70
71 static Spin_lock<> allocator_lock;
72
73 IMPLEMENT
74 void *
75 Jdb_kobject_name::operator new (size_t) throw()
76 {
77   Jdb_kobject_name *n = _names;
78   while (1)
79     {
80       void **o = reinterpret_cast<void**>(n);
81       if (!*o)
82         {
83           auto g = lock_guard(allocator_lock);
84           if (!*o)
85             {
86               *o = (void*)10;
87               return n;
88             }
89         }
90
91       ++n;
92
93       if ((n - _names) >= Name_entries)
94         return 0;
95     }
96 }
97
98 IMPLEMENT
99 void
100 Jdb_kobject_name::operator delete (void *p)
101 {
102   auto g = lock_guard(allocator_lock);
103   void **o = reinterpret_cast<void**>(p);
104   *o = 0;
105 }
106
107 PUBLIC
108 void
109 Jdb_kobject_name::clear_name()
110 {
111   for (unsigned i = 0; i < max_len(); ++i)
112     _name[i] = 0;
113 }
114
115 PUBLIC inline
116 char const *
117 Jdb_kobject_name::name() const
118 { return _name; }
119
120 PUBLIC inline
121 char *
122 Jdb_kobject_name::name()
123 { return _name; }
124
125 class Jdb_name_hdl : public Jdb_kobject_handler
126 {
127 public:
128   Jdb_name_hdl() : Jdb_kobject_handler(0) {}
129   virtual bool show_kobject(Kobject_common *, int) { return true; }
130   virtual ~Jdb_name_hdl() {}
131 };
132
133 PUBLIC
134 int
135 Jdb_name_hdl::show_kobject_short(char *buf, int max, Kobject_common *o)
136 {
137   Jdb_kobject_name *ex
138     = Jdb_kobject_extension::find_extension<Jdb_kobject_name>(o);
139
140   if (ex)
141     return snprintf(buf, max, " {%-*.*s}", ex->max_len(), ex->max_len(), ex->name());
142
143   return 0;
144 }
145
146 PUBLIC
147 bool
148 Jdb_name_hdl::invoke(Kobject_common *o, Syscall_frame *f, Utcb *utcb)
149 {
150   switch (utcb->values[0])
151     {
152     case Op_set_name:
153         {
154           Jdb_kobject_name *ne = new Jdb_kobject_name();
155           if (!ne)
156             {
157               f->tag(Kobject_iface::commit_result(-L4_err::ENomem));
158               return true;
159             }
160
161           char const *name = reinterpret_cast<char const*>(&utcb->values[1]);
162           ne->clear_name();
163           strncpy(ne->name(), name, ne->max_len());
164           o->dbg_info()->_jdb_data.add(ne);
165           f->tag(Kobject_iface::commit_result(0));
166           return true;
167         }
168     case Op_get_name:
169         {
170           Kobject_dbg::Iterator o = Kobject_dbg::id_to_obj(utcb->values[1]);
171           if (o == Kobject_dbg::end())
172             {
173               f->tag(Kobject_iface::commit_result(-L4_err::ENoent));
174               return true;
175             }
176           Jdb_kobject_name *n = Jdb_kobject_extension::find_extension<Jdb_kobject_name>(Kobject::from_dbg(o));
177           if (!n)
178             {
179               f->tag(Kobject_iface::commit_result(-L4_err::ENoent));
180               return true;
181             }
182
183           unsigned l = min<unsigned>(n->max_len(), sizeof(utcb->values));
184           char *dst = reinterpret_cast<char *>(utcb->values);
185           strncpy(dst, n->name(), l);
186           dst[l - 1] = 0;
187
188           f->tag(Kobject_iface::commit_result(0));
189           return true;
190         }
191     }
192   return false;
193 }
194
195 PUBLIC static FIASCO_INIT
196 void
197 Jdb_kobject_name::init()
198 {
199   _names = (Jdb_kobject_name*)Kmem_alloc::allocator()->unaligned_alloc(Name_buffer_size);
200   if (!_names)
201     panic("No memory for thread names");
202
203   for (int i=0; i<Name_entries; i++)
204     *reinterpret_cast<unsigned long*>(_names + i) = 0;
205
206   static Jdb_name_hdl hdl;
207   Jdb_kobject::module()->register_handler(&hdl);
208 }
209
210
211 STATIC_INITIALIZE(Jdb_kobject_name);
212