* contrib/vmlog/vmlog_cacao.c, contrib/vmlog/vmlog_cacao.h: Replaced
[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
31 /*** locking *********************************************************/
32
33 #define VMLOG_LOCK(vml)    lock_monitor_enter(vmlog_global_lock)
34 #define VMLOG_UNLOCK(vml)  lock_monitor_exit(vmlog_global_lock)
35
36 /*** include the vmlog code ******************************************/
37
38 #include <vmlog.c>
39
40 /*** internal functions **********************************************/
41
42 void vmlog_cacao_init(JavaVMInitArgs *vmargs)
43 {
44         vmlog_options *opts;
45
46         opts = vmlog_opt_parse_vmargs(vmargs);
47         
48         if (!opts->prefix)
49                 return;
50
51         vmlog_global_log = vmlog_log_new(opts->prefix,1);
52
53         if (opts->ignoreprefix) {
54                 vmlog_log_load_ignorelist(vmlog_global_log,
55                                 opts->ignoreprefix);
56         }
57
58         if (opts->stringprefix) {
59                 vmlog_load_stringhash(vmlog_global_log,
60                                 opts->stringprefix);
61         }
62
63         vmlog_opt_free(opts);
64 }
65
66 void vmlog_cacao_init_lock(void)
67 {
68         vmlog_global_lock = NEW(java_object_t);
69         lock_init_object_lock(vmlog_global_lock);
70 }
71
72 static void vmlog_cacao_do_log(vmlog_log_function fun,
73                                methodinfo *m)
74 {
75         char *name;
76         int namelen;
77
78         assert(m);
79
80         if (!vmlog_global_log)
81                 return;
82
83         name = vmlog_concat4len(m->class->name->text,m->class->name->blength,
84                                 ".",1,
85                                 m->name->text,m->name->blength,
86                                 m->descriptor->text,m->descriptor->blength,
87                                 &namelen);
88
89         fun(vmlog_global_log,(void*) THREADOBJECT,name,namelen);
90
91         VMLOG_FREE_ARRAY(char,namelen+1,name);
92 }
93
94 /*** functions callable from cacao ***********************************/
95
96 void vmlog_cacao_enter_method(methodinfo *m)
97 {
98         vmlog_cacao_do_log(vmlog_log_enter,m);
99 }
100
101 void vmlog_cacao_leave_method(methodinfo *m)
102 {
103         vmlog_cacao_do_log(vmlog_log_leave,m);
104 }
105
106 void vmlog_cacao_unrol_method(methodinfo *m)
107 {
108         vmlog_cacao_do_log(vmlog_log_unrol,m);
109 }
110
111 void vmlog_cacao_rerol_method(methodinfo *m)
112 {
113         vmlog_cacao_do_log(vmlog_log_rerol,m);
114 }
115
116 void vmlog_cacao_unwnd_method(methodinfo *m)
117 {
118         vmlog_cacao_do_log(vmlog_log_unwnd,m);
119 }
120
121 void vmlog_cacao_throw(java_object_t *xptr)
122 {
123         classinfo *c;
124         
125         if (!vmlog_global_log)
126                 return;
127
128         if (xptr) {
129                 c = xptr->vftbl->class;
130                 vmlog_log_throw(vmlog_global_log,(void*) THREADOBJECT,
131                                 c->name->text,c->name->blength);
132         }
133         else {
134                 vmlog_log_throw(vmlog_global_log,(void*) THREADOBJECT,
135                                 "unknown Throwable",strlen("unknown Throwable"));
136         }
137 }
138
139 void vmlog_cacao_catch(java_object_t *xptr)
140 {
141         classinfo *c;
142         
143         if (!vmlog_global_log)
144                 return;
145
146         if (xptr) {
147                 c = xptr->vftbl->class;
148                 vmlog_log_catch(vmlog_global_log,(void*) THREADOBJECT,
149                                 c->name->text,c->name->blength);
150         }
151         else {
152                 vmlog_log_catch(vmlog_global_log,(void*) THREADOBJECT,
153                                 "unknown Throwable",strlen("unknown Throwable"));
154         }
155 }
156
157 void vmlog_cacao_signl(const char *name)
158 {
159         if (!vmlog_global_log)
160                 return;
161
162         vmlog_log_signl(vmlog_global_log,(void*) THREADOBJECT,
163                         name, strlen(name));
164 }
165
166 /* vim: noet ts=8 sw=8
167  */
168