]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/jdb/jdb_kobject_names.cpp
12f6765dde4f47589d66e3aaad73efdf63a9094e
[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 "panic.h"
37 #include "space.h"
38 #include "thread.h"
39 #include "static_init.h"
40
41
42
43 enum
44 {
45   Name_buffer_size = 8192,
46   Name_entries = Name_buffer_size / sizeof(Jdb_kobject_name),
47 };
48
49
50 char const *const Jdb_kobject_name::static_type = "Jdb_kobject_names";
51 Jdb_kobject_name *Jdb_kobject_name::_names;
52
53
54 PUBLIC
55 unsigned
56 Jdb_kobject_name::max_len()
57 { return sizeof(_name); }
58
59 PUBLIC
60 void
61 Jdb_kobject_name::name(char const *name)
62 {
63   strncpy(_name, name, sizeof(_name));
64 }
65
66 PUBLIC
67 Jdb_kobject_name::Jdb_kobject_name()
68 { _name[0] = 0; }
69
70 static Spin_lock allocator_lock;
71
72 IMPLEMENT
73 void *
74 Jdb_kobject_name::operator new (size_t) throw()
75 {
76   Jdb_kobject_name *n = _names;
77   while (1)
78     {
79       void **o = reinterpret_cast<void**>(n);
80       if (!*o)
81         {
82           Lock_guard<Spin_lock> g(&allocator_lock);
83           if (!*o)
84             {
85               *o = (void*)10;
86               return n;
87             }
88         }
89
90       ++n;
91
92       if ((n - _names) >= Name_entries)
93         return 0;
94     }
95 }
96
97 IMPLEMENT
98 void
99 Jdb_kobject_name::operator delete (void *p)
100 {
101   Lock_guard<Spin_lock> g(&allocator_lock);
102   void **o = reinterpret_cast<void**>(p);
103   *o = 0;
104 }
105
106 PUBLIC
107 void
108 Jdb_kobject_name::clear_name()
109 {
110   for (unsigned i = 0; i < max_len(); ++i)
111     _name[i] = 0;
112 }
113
114 PUBLIC inline
115 char const *
116 Jdb_kobject_name::name() const
117 { return _name; }
118
119 PUBLIC inline
120 char *
121 Jdb_kobject_name::name()
122 { return _name; }
123
124 #if 0 // XXX use copy from user later 
125 PUBLIC
126 void
127 Name_entry::set(Global_id id, const char *name)
128 {
129   unsigned i;
130   Mem_space * const s = current()->mem_space();
131
132   _id = id;
133   for (i=0; i<sizeof(_name)-1; i++)
134     {
135       _name[i] = s->peek_user(name++);
136       if (!_name[i])
137         break;
138     }
139   _name[i] = 0;
140 }
141 #endif
142
143 class Jdb_name_hdl : public Jdb_kobject_handler
144 {
145 public:
146   Jdb_name_hdl() : Jdb_kobject_handler(0) {}
147   virtual bool show_kobject(Kobject *, int) { return true; }
148   virtual ~Jdb_name_hdl() {}
149 };
150
151 PUBLIC
152 int
153 Jdb_name_hdl::show_kobject_short(char *buf, int max, Kobject *o)
154 {
155   Jdb_kobject_name *ex
156     = Jdb_kobject_extension::find_extension<Jdb_kobject_name>(o);
157
158   if (ex)
159     return snprintf(buf, max, " {%-*.*s}", ex->max_len(), ex->max_len(), ex->name());
160
161   return 0;
162 }
163
164 PUBLIC
165 bool
166 Jdb_name_hdl::invoke(Kobject *o, Syscall_frame *f, Utcb *utcb)
167 {
168   if (utcb->values[0] == 0)
169     {
170       if (!o)
171         {
172           f->tag(Kobject_iface::commit_result(-L4_err::EInval));
173           return true;
174         }
175       Jdb_kobject_name *ne = new Jdb_kobject_name();
176       if (!ne)
177         {
178           f->tag(Kobject_iface::commit_result(-L4_err::ENomem));
179           return true;
180         }
181
182       char const *name = reinterpret_cast<char const*>(&utcb->values[1]);
183       ne->clear_name();
184       strncpy(ne->name(), name, ne->max_len());
185       ne->add(&o->_jdb_data);
186       f->tag(Kobject_iface::commit_result(0));
187       return true;
188     }
189   return false;
190 }
191
192 PUBLIC static FIASCO_INIT
193 void
194 Jdb_kobject_name::init()
195 {
196   _names = (Jdb_kobject_name*)Kmem_alloc::allocator()->unaligned_alloc(Name_buffer_size);
197   if (!_names)
198     panic("No memory for thread names");
199
200   for (int i=0; i<Name_entries; i++)
201     *reinterpret_cast<unsigned long*>(_names + i) = 0;
202
203   static Jdb_name_hdl hdl;
204   Jdb_kobject::module()->register_handler(&hdl);
205 }
206
207
208 STATIC_INITIALIZE(Jdb_kobject_name);
209