PR123: LD_LIBRARY_PATH and java.library.path
[cacao.git] / contrib / vmlog / vmlog_cacao.c
1 /* vmlog - high-speed logging for free VMs                  */
2 /* Copyright (C) 2006 Edwin Steiner <edwin.steiner@gmx.net> */
3
4 /* This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /* vmlog_cacao.c - code to be #included in cacao */
20
21 #include <vmlog_cacao.h>
22 #define VMLOG_HAVE_PTRINT
23 #include <vmlog.h>
24 #include <assert.h>
25
26 /*** global variables ************************************************/
27
28 static vmlog_log *vmlog_global_log = NULL;
29 static java_object_t *vmlog_global_lock = NULL;
30 static vmlog_options *vmlog_cacao_options = NULL;
31
32 /*** locking *********************************************************/
33
34 #define VMLOG_LOCK(vml)    lock_monitor_enter(vmlog_global_lock)
35 #define VMLOG_UNLOCK(vml)  lock_monitor_exit(vmlog_global_lock)
36
37 /*** include the vmlog code ******************************************/
38
39 #include <vmlog.c>
40
41 /*** internal functions **********************************************/
42
43 void vmlog_cacao_init()
44 {
45         if (!vmlog_cacao_options->prefix)
46                 return;
47
48         vmlog_global_log = vmlog_log_new(vmlog_cacao_options->prefix,1);
49
50         if (vmlog_cacao_options->ignoreprefix) {
51                 vmlog_log_load_ignorelist(vmlog_global_log,
52                                 vmlog_cacao_options->ignoreprefix);
53         }
54
55         if (vmlog_cacao_options->stringprefix) {
56                 vmlog_load_stringhash(vmlog_global_log,
57                                 vmlog_cacao_options->stringprefix);
58         }
59
60         vmlog_opt_free(vmlog_cacao_options);
61         vmlog_cacao_options = NULL;
62 }
63
64 void vmlog_cacao_init_lock(void)
65 {
66         vmlog_global_lock = NEW(java_object_t);
67         lock_init_object_lock(vmlog_global_lock);
68 }
69
70 static void vmlog_cacao_do_log(vmlog_log_function fun,
71                                methodinfo *m)
72 {
73         char *name;
74         int namelen;
75         char *cname;
76         int cnamelen;
77
78         assert(m);
79
80         if (!vmlog_global_log)
81                 return;
82
83         if (m->class) {
84                 cname = m->class->name->text;
85                 cnamelen = m->class->name->blength;
86         }
87         else {
88                 cname = "<NULL>";
89                 cnamelen = 6;
90         }
91
92         name = vmlog_concat4len(cname,cnamelen,
93                                 ".",1,
94                                 m->name->text,m->name->blength,
95                                 m->descriptor->text,m->descriptor->blength,
96                                 &namelen);
97
98         fun(vmlog_global_log,(void*) THREADOBJECT,name,namelen);
99
100         VMLOG_FREE_ARRAY(char,namelen+1,name);
101 }
102
103 /*** functions callable from cacao ***********************************/
104
105 void vmlog_cacao_enter_method(methodinfo *m)
106 {
107         vmlog_cacao_do_log(vmlog_log_enter,m);
108 }
109
110 void vmlog_cacao_leave_method(methodinfo *m)
111 {
112         vmlog_cacao_do_log(vmlog_log_leave,m);
113 }
114
115 void vmlog_cacao_unrol_method(methodinfo *m)
116 {
117         vmlog_cacao_do_log(vmlog_log_unrol,m);
118 }
119
120 void vmlog_cacao_rerol_method(methodinfo *m)
121 {
122         vmlog_cacao_do_log(vmlog_log_rerol,m);
123 }
124
125 void vmlog_cacao_unwnd_method(methodinfo *m)
126 {
127         vmlog_cacao_do_log(vmlog_log_unwnd,m);
128 }
129
130 void vmlog_cacao_throw(java_object_t *xptr)
131 {
132         classinfo *c;
133         
134         if (!vmlog_global_log)
135                 return;
136
137         if (xptr) {
138                 c = xptr->vftbl->class;
139                 vmlog_log_throw(vmlog_global_log,(void*) THREADOBJECT,
140                                 c->name->text,c->name->blength);
141         }
142         else {
143                 vmlog_log_throw(vmlog_global_log,(void*) THREADOBJECT,
144                                 "unknown Throwable",strlen("unknown Throwable"));
145         }
146 }
147
148 void vmlog_cacao_catch(java_object_t *xptr)
149 {
150         classinfo *c;
151         
152         if (!vmlog_global_log)
153                 return;
154
155         if (xptr) {
156                 c = xptr->vftbl->class;
157                 vmlog_log_catch(vmlog_global_log,(void*) THREADOBJECT,
158                                 c->name->text,c->name->blength);
159         }
160         else {
161                 vmlog_log_catch(vmlog_global_log,(void*) THREADOBJECT,
162                                 "unknown Throwable",strlen("unknown Throwable"));
163         }
164 }
165
166 void vmlog_cacao_signl(const char *name)
167 {
168         if (!vmlog_global_log)
169                 return;
170
171         vmlog_log_signl(vmlog_global_log,(void*) THREADOBJECT,
172                         name, strlen(name));
173 }
174
175 void vmlog_cacao_signl_type(int type)
176 {
177         char message[20];
178
179         if (!vmlog_global_log)
180                 return;
181
182         sprintf(message, "EXC %d", type);
183
184         vmlog_log_signl(vmlog_global_log,(void*) THREADOBJECT,
185                         message, strlen(message));
186 }
187
188 void vmlog_cacao_init_options(void)
189 {
190         vmlog_cacao_options = vmlog_opt_new();
191 }
192
193 void vmlog_cacao_set_prefix(const char *arg)
194 {
195         vmlog_opt_set_prefix(vmlog_cacao_options, arg);
196 }
197
198 void vmlog_cacao_set_stringprefix(const char *arg)
199 {
200         vmlog_opt_set_stringprefix(vmlog_cacao_options, arg);
201 }
202
203 void vmlog_cacao_set_ignoreprefix(const char *arg)
204 {
205         vmlog_opt_set_ignoreprefix(vmlog_cacao_options, arg);
206 }
207
208 /* vim: noet ts=8 sw=8
209  */
210