summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src
diff options
context:
space:
mode:
authorsbc@chromium.org <sbc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-14 01:28:22 +0000
committersbc@chromium.org <sbc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-14 01:28:22 +0000
commit6728c8f3deaf67e492054296f3f840400b9e4e04 (patch)
treece7a69d0125604bcb8568d200aaaafe2ac551ea4 /native_client_sdk/src
parenta1831928401890f45e7d4eb09ee7932bc3fe101c (diff)
downloadchromium_src-6728c8f3deaf67e492054296f3f840400b9e4e04.zip
chromium_src-6728c8f3deaf67e492054296f3f840400b9e4e04.tar.gz
chromium_src-6728c8f3deaf67e492054296f3f840400b9e4e04.tar.bz2
[NaCl SDK] nacl_io: add dbgprint utility.
This can be used from within nacl_io to send debug strings directly to the real stderr. R=binji@chromium.org Review URL: https://codereview.chromium.org/22711004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@217428 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'native_client_sdk/src')
-rw-r--r--native_client_sdk/src/libraries/nacl_io/dbgprint.c36
-rw-r--r--native_client_sdk/src/libraries/nacl_io/dbgprint.h16
-rw-r--r--native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc39
-rw-r--r--native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc45
-rw-r--r--native_client_sdk/src/libraries/nacl_io/library.dsc2
5 files changed, 109 insertions, 29 deletions
diff --git a/native_client_sdk/src/libraries/nacl_io/dbgprint.c b/native_client_sdk/src/libraries/nacl_io/dbgprint.c
new file mode 100644
index 0000000..b8a5bdf
--- /dev/null
+++ b/native_client_sdk/src/libraries/nacl_io/dbgprint.c
@@ -0,0 +1,36 @@
+/* Copyright (c) 2013 The Chromium Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file. */
+
+#include "nacl_io/dbgprint.h"
+
+#include "nacl_io/kernel_wrap_real.h"
+
+#include <alloca.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+void dbgprintf(const char* format, ...) {
+ va_list args;
+ size_t wrote;
+ char* output;
+
+#ifdef _MSC_VER
+ /* TODO(sbc): vsnprintf on win32 does not return the
+ * size of the buffer needed. This can be implemented
+ * on win32 in terms of _vscprintf; */
+#error "not implemented for win32"
+#endif
+
+ va_start(args, format);
+ int len = vsnprintf(NULL, 0, format, args);
+ va_end(args);
+ output = alloca(len + 1);
+
+ va_start(args, format);
+ vsnprintf(output, len + 1, format, args);
+ va_end(args);
+
+ _real_write(2, output, strlen(output), &wrote);
+}
diff --git a/native_client_sdk/src/libraries/nacl_io/dbgprint.h b/native_client_sdk/src/libraries/nacl_io/dbgprint.h
new file mode 100644
index 0000000..305fea5
--- /dev/null
+++ b/native_client_sdk/src/libraries/nacl_io/dbgprint.h
@@ -0,0 +1,16 @@
+/* Copyright (c) 2013 The Chromium Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file. */
+
+#ifndef LIBRARIES_NACL_IO_DBGPRINT_H_
+#define LIBRARIES_NACL_IO_DBGPRINT_H_
+
+#include "sdk_util/macros.h"
+
+EXTERN_C_BEGIN
+
+void dbgprintf(const char* format, ...);
+
+EXTERN_C_END
+
+#endif /* LIBRARIES_NACL_IO_DBGPRINT_H_ */
diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc
index c2a9e1f..8c270b1 100644
--- a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc
+++ b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc
@@ -22,6 +22,7 @@
#include <sys/time.h>
#include "nacl_io/kernel_intercept.h"
+#include "nacl_io/kernel_wrap_real.h"
#include "nacl_io/osmman.h"
@@ -243,9 +244,6 @@ int WRAP(poll)(struct pollfd *fds, nfds_t nfds, int timeout, int* count) {
}
int WRAP(read)(int fd, void *buf, size_t count, size_t *nread) {
- if (!ki_is_initialized())
- return REAL(read)(fd, buf, count, nread);
-
ssize_t signed_nread = ki_read(fd, buf, count);
*nread = static_cast<size_t>(signed_nread);
return (signed_nread < 0) ? errno : 0;
@@ -277,22 +275,33 @@ int WRAP(stat)(const char *pathname, struct nacl_abi_stat *nacl_buf) {
}
int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) {
- if (!ki_is_initialized())
- return REAL(write)(fd, buf, count, nwrote);
-
ssize_t signed_nwrote = ki_write(fd, buf, count);
*nwrote = static_cast<size_t>(signed_nwrote);
return (signed_nwrote < 0) ? errno : 0;
}
+static void assign_real_pointers() {
+ static bool assigned = false;
+ if (!assigned) {
+ EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR)
+ assigned = true;
+ }
+}
+
+#define CHECK_REAL(func) \
+ if (!REAL(func)) \
+ assign_real_pointers();
+
// "real" functions, i.e. the unwrapped original functions.
int _real_close(int fd) {
+ CHECK_REAL(close);
return REAL(close)(fd);
}
int _real_fstat(int fd, struct stat* buf) {
struct nacl_abi_stat st;
+ CHECK_REAL(fstat);
int err = REAL(fstat)(fd, &st);
if (err) {
errno = err;
@@ -310,6 +319,7 @@ int _real_getdents(int fd, void* buf, size_t count, size_t* nread) {
size_t offset = 0;
size_t nacl_offset = 0;
size_t nacl_nread;
+ CHECK_REAL(getdents);
int err = REAL(getdents)(fd, (dirent*)nacl_buf, count, &nacl_nread);
if (err)
return err;
@@ -333,39 +343,48 @@ int _real_getdents(int fd, void* buf, size_t count, size_t* nread) {
}
int _real_lseek(int fd, off_t offset, int whence, off_t* new_offset) {
+ CHECK_REAL(seek);
return REAL(seek)(fd, offset, whence, new_offset);
}
int _real_mkdir(const char* pathname, mode_t mode) {
+ CHECK_REAL(mkdir);
return REAL(mkdir)(pathname, mode);
}
int _real_mmap(void** addr, size_t length, int prot, int flags, int fd,
off_t offset) {
+ CHECK_REAL(mmap);
return REAL(mmap)(addr, length, prot, flags, fd, offset);
}
int _real_munmap(void* addr, size_t length) {
+ CHECK_REAL(munmap);
return REAL(munmap)(addr, length);
}
int _real_open(const char* pathname, int oflag, mode_t cmode, int* newfd) {
+ CHECK_REAL(open);
return REAL(open)(pathname, oflag, cmode, newfd);
}
int _real_open_resource(const char* file, int* fd) {
+ CHECK_REAL(open_resource);
return REAL(open_resource)(file, fd);
}
int _real_read(int fd, void *buf, size_t count, size_t *nread) {
+ CHECK_REAL(read);
return REAL(read)(fd, buf, count, nread);
}
int _real_rmdir(const char* pathname) {
+ CHECK_REAL(rmdir);
return REAL(rmdir)(pathname);
}
int _real_write(int fd, const void *buf, size_t count, size_t *nwrote) {
+ CHECK_REAL(write);
return REAL(write)(fd, buf, count, nwrote);
}
@@ -376,13 +395,9 @@ uint64_t usec_since_epoch() {
}
static bool s_wrapped = false;
-static bool s_assigned = false;
void kernel_wrap_init() {
if (!s_wrapped) {
- if (!s_assigned) {
- EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR)
- s_assigned = true;
- }
+ assign_real_pointers();
EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP)
s_wrapped = true;
}
@@ -397,6 +412,4 @@ void kernel_wrap_uninit() {
EXTERN_C_END
-
#endif // defined(__native_client__) && defined(__GLIBC__)
-
diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc
index 34ab407..e7325c5 100644
--- a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc
+++ b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc
@@ -9,6 +9,7 @@
#if defined(__native_client__) && !defined(__GLIBC__)
#include "nacl_io/kernel_wrap.h"
+
#include <assert.h>
#include <dirent.h>
#include <errno.h>
@@ -16,7 +17,9 @@
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
+
#include "nacl_io/kernel_intercept.h"
+#include "nacl_io/kernel_wrap_real.h"
EXTERN_C_BEGIN
@@ -111,9 +114,6 @@ int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) {
}
int WRAP(read)(int fd, void* buf, size_t count, size_t* nread) {
- if (!ki_is_initialized())
- return REAL(read)(fd, buf, count, nread);
-
ssize_t signed_nread = ki_read(fd, buf, count);
*nread = static_cast<size_t>(signed_nread);
return (signed_nread < 0) ? errno : 0;
@@ -129,29 +129,43 @@ int WRAP(stat)(const char* pathname, struct stat* buf) {
}
int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) {
- if (!ki_is_initialized())
- return REAL(write)(fd, buf, count, nwrote);
-
ssize_t signed_nwrote = ki_write(fd, buf, count);
*nwrote = static_cast<size_t>(signed_nwrote);
return (signed_nwrote < 0) ? errno : 0;
}
+static void assign_real_pointers() {
+ static bool assigned = false;
+ if (!assigned) {
+ __libnacl_irt_filename_init();
+ EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR)
+ assigned = true;
+ }
+}
+
+#define CHECK_REAL(func) \
+ if (!REAL(func)) \
+ assign_real_pointers();
+
// "real" functions, i.e. the unwrapped original functions.
int _real_close(int fd) {
+ CHECK_REAL(close);
return REAL(close)(fd);
}
int _real_fstat(int fd, struct stat* buf) {
+ CHECK_REAL(fstat);
return REAL(fstat)(fd, buf);
}
-int _real_getdents(int fd, dirent* nacl_buf, size_t nacl_count, size_t* nread) {
- return REAL(getdents)(fd, nacl_buf, nacl_count, nread);
+int _real_getdents(int fd, void* nacl_buf, size_t nacl_count, size_t* nread) {
+ CHECK_REAL(getdents);
+ return REAL(getdents)(fd, static_cast<dirent*>(nacl_buf), nacl_count, nread);
}
int _real_lseek(int fd, off_t offset, int whence, off_t* new_offset) {
+ CHECK_REAL(seek);
return REAL(seek)(fd, offset, whence, new_offset);
}
@@ -161,14 +175,17 @@ int _real_mkdir(const char* pathname, mode_t mode) {
int _real_mmap(void** addr, size_t length, int prot, int flags, int fd,
off_t offset) {
+ CHECK_REAL(mmap);
return REAL(mmap)(addr, length, prot, flags, fd, offset);
}
int _real_munmap(void* addr, size_t length) {
+ CHECK_REAL(munmap);
return REAL(munmap)(addr, length);
}
int _real_open(const char* pathname, int oflag, mode_t cmode, int* newfd) {
+ CHECK_REAL(open);
return REAL(open)(pathname, oflag, cmode, newfd);
}
@@ -177,6 +194,7 @@ int _real_open_resource(const char* file, int* fd) {
}
int _real_read(int fd, void* buf, size_t count, size_t* nread) {
+ CHECK_REAL(read);
return REAL(read)(fd, buf, count, nread);
}
@@ -185,6 +203,7 @@ int _real_rmdir(const char* pathname) {
}
int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) {
+ CHECK_REAL(write);
return REAL(write)(fd, buf, count, nwrote);
}
@@ -195,14 +214,10 @@ uint64_t usec_since_epoch() {
}
static bool s_wrapped = false;
-static bool s_assigned = false;
+
void kernel_wrap_init() {
if (!s_wrapped) {
- if (!s_assigned) {
- __libnacl_irt_filename_init();
- EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR)
- s_assigned = true;
- }
+ assign_real_pointers();
EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP)
s_wrapped = true;
}
@@ -217,6 +232,4 @@ void kernel_wrap_uninit() {
EXTERN_C_END
-
#endif // defined(__native_client__) && !defined(__GLIBC__)
-
diff --git a/native_client_sdk/src/libraries/nacl_io/library.dsc b/native_client_sdk/src/libraries/nacl_io/library.dsc
index 4d75b2c..49b0979 100644
--- a/native_client_sdk/src/libraries/nacl_io/library.dsc
+++ b/native_client_sdk/src/libraries/nacl_io/library.dsc
@@ -11,6 +11,7 @@
'NAME' : 'nacl_io',
'TYPE' : 'lib',
'SOURCES' : [
+ 'dbgprint.c',
"event_emitter.cc",
"event_listener.cc",
"h_errno.cc",
@@ -97,6 +98,7 @@
'HEADERS': [
{
'FILES': [
+ 'dbgprint.h',
"event_emitter.h",
"event_listener.h",
"error.h",