Skip to content

Commit abb289b

Browse files
committed
Use emscripten_err for logging in dynlink.c. NFC
This is more direct that using stderr/stdout and (under node) it doesn't involve any proxying to them main thread.
1 parent 82c9cd7 commit abb289b

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

system/lib/libc/dynlink.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <string.h>
2121
#include <dynlink.h>
2222

23+
#include <emscripten/console.h>
24+
2325
//#define DYLINK_DEBUG
2426

2527
struct async_data {
@@ -65,7 +67,14 @@ int __dl_invalid_handle(void* h) {
6567

6668
static void load_library_done(struct dso* p) {
6769
#ifdef DYLINK_DEBUG
68-
fprintf(stderr, "%p: load_library_done: dso=%p mem_addr=%p mem_size=%d table_addr=%p table_size=%d\n", pthread_self(), p, p->mem_addr, p->mem_size, p->table_addr, p->table_size);
70+
_emscripten_errf("%p: load_library_done: dso=%p mem_addr=%p mem_size=%zu "
71+
"table_addr=%p table_size=%zu",
72+
pthread_self(),
73+
p,
74+
p->mem_addr,
75+
p->mem_size,
76+
p->table_addr,
77+
p->table_size);
6978
#endif
7079

7180
// insert into linked list
@@ -98,7 +107,11 @@ static struct dso* load_library_start(const char* name, int flags) {
98107

99108
static void dlopen_js_onsuccess(struct dso* dso, struct async_data* data) {
100109
#ifdef DYLINK_DEBUG
101-
fprintf(stderr, "%p: dlopen_js_onsuccess: dso=%p mem_addr=%p mem_size=%p\n", pthread_self(), p, p->mem_addr, p->mem_size);
110+
_emscripten_errf("%p: dlopen_js_onsuccess: dso=%p mem_addr=%p mem_size=%zu",
111+
pthread_self(),
112+
dso,
113+
dso->mem_addr,
114+
dso->mem_size);
102115
#endif
103116
load_library_done(dso);
104117
pthread_rwlock_unlock(&lock);
@@ -108,7 +121,7 @@ static void dlopen_js_onsuccess(struct dso* dso, struct async_data* data) {
108121

109122
static void dlopen_js_onerror(struct dso* dso, struct async_data* data) {
110123
#ifdef DYLINK_DEBUG
111-
fprintf(stderr, "%p: dlopen_js_onerror: dso=%p\n", pthread_self(), handle);
124+
_emscripten_errf("%p: dlopen_js_onerror: dso=%p", pthread_self(), dso);
112125
#endif
113126
pthread_rwlock_unlock(&lock);
114127
data->onerror(data->user_data);
@@ -141,7 +154,7 @@ void* dlopen(const char* file, int flags) {
141154
return head;
142155
}
143156
#ifdef DYLINK_DEBUG
144-
fprintf(stderr, "%p: dlopen: %s [%d]\n", pthread_self(), file, flags);
157+
_emscripten_errf("%p: dlopen: %s [%d]", pthread_self(), file, flags);
145158
#endif
146159

147160
struct dso* p;
@@ -153,7 +166,7 @@ void* dlopen(const char* file, int flags) {
153166
for (p = head; p; p = p->next) {
154167
if (!strcmp(p->name, file)) {
155168
#ifdef DYLINK_DEBUG
156-
fprintf(stderr, "%p: dlopen: already opened: %p\n", pthread_self(), p);
169+
_emscripten_errf("%p: dlopen: already opened: %p", pthread_self(), p);
157170
#endif
158171
goto end;
159172
}
@@ -166,14 +179,14 @@ void* dlopen(const char* file, int flags) {
166179
void* success = _dlopen_js(p);
167180
if (!success) {
168181
#ifdef DYLINK_DEBUG
169-
fprintf(stderr, "%p: dlopen_js: failed\n", pthread_self(), p);
182+
_emscripten_errf("%p: dlopen_js: failed: %p", pthread_self(), p);
170183
#endif
171184
free(p);
172185
p = NULL;
173186
goto end;
174187
}
175188
#ifdef DYLINK_DEBUG
176-
fprintf(stderr, "%p: dlopen_js: success: %p\n", pthread_self(), p);
189+
_emscripten_errf("%p: dlopen_js: success: %p", pthread_self(), p);
177190
#endif
178191
load_library_done(p);
179192
end:
@@ -204,15 +217,15 @@ void emscripten_dlopen(const char* filename, int flags, void* user_data,
204217
d->onerror = onerror;
205218

206219
#ifdef DYLINK_DEBUG
207-
fprintf(stderr, "%p: calling emscripten_dlopen_js %p\n", pthread_self(), p);
220+
_emscripten_errf("%p: calling emscripten_dlopen_js %p", pthread_self(), p);
208221
#endif
209222
// Unlock happens in dlopen_js_onsuccess/dlopen_js_onerror
210223
_emscripten_dlopen_js(p, dlopen_js_onsuccess, dlopen_js_onerror, d);
211224
}
212225

213226
void* __dlsym(void* restrict p, const char* restrict s, void* restrict ra) {
214227
#ifdef DYLINK_DEBUG
215-
fprintf(stderr, "%p: __dlsym dso:%p sym:%s\n", pthread_self(), p, s);
228+
_emscripten_errf("%p: __dlsym dso:%p sym:%s", pthread_self(), p, s);
216229
#endif
217230
if (p != RTLD_DEFAULT && p != RTLD_NEXT && __dl_invalid_handle(p)) {
218231
return 0;
@@ -247,7 +260,7 @@ void _emscripten_thread_sync_code() {
247260
ensure_init();
248261
if (thread_local_tail == tail) {
249262
#ifdef DYLINK_DEBUG
250-
fprintf(stderr, "%p: emscripten_thread_sync_code: already in sync\n", pthread_self());
263+
_emscripten_errf("%p: emscripten_thread_sync_code: already in sync", pthread_self());
251264
#endif
252265
goto done;
253266
}
@@ -258,22 +271,29 @@ void _emscripten_thread_sync_code() {
258271
while (thread_local_tail->next) {
259272
struct dso* p = thread_local_tail->next;
260273
#ifdef DYLINK_DEBUG
261-
fprintf(stderr, "%p: emscripten_thread_sync_code: %s mem_addr=%p mem_size=%d table_addr=%p table_size=%d\n", pthread_self(), p->name, p->mem_addr, p->mem_size, p->table_addr, p->table_size);
274+
_emscripten_errf("%p: emscripten_thread_sync_code: %s mem_addr=%p "
275+
"mem_size=%zu table_addr=%p table_size=%zu",
276+
pthread_self(),
277+
p->name,
278+
p->mem_addr,
279+
p->mem_size,
280+
p->table_addr,
281+
p->table_size);
262282
#endif
263283
void* success = _dlopen_js(p);
264284
if (!success) {
265285
// If any on the libraries fails to load here then we give up.
266286
// TODO(sbc): Ideally this would never happen and we could/should
267287
// abort, but on the main thread (where we don't have sync xhr) its
268288
// often not possible to syncronously load side module.
269-
fprintf(stderr, "emscripten_thread_sync_code failed: %s\n", dlerror());
289+
_emscripten_errf("emscripten_thread_sync_code failed: %s", dlerror());
270290
break;
271291
}
272292
thread_local_tail = p;
273293
}
274294
pthread_rwlock_unlock(&lock);
275295
#ifdef DYLINK_DEBUG
276-
fprintf(stderr, "%p: emscripten_thread_sync_code done\n", pthread_self());
296+
_emscripten_errf("%p: emscripten_thread_sync_code done", pthread_self());
277297
#endif
278298

279299
done:

0 commit comments

Comments
 (0)