All of lore.kernel.org
 help / color / mirror / Atom feed
* [Xenomai-core] [PATCH 2/2] rtdk: Add assert_context helpers
@ 2009-02-25 18:39 Jan Kiszka
  2009-02-25 18:41 ` Gilles Chanteperdrix
  2009-02-26 14:02 ` [Xenomai-core] [PATCH v2 " Jan Kiszka
  0 siblings, 2 replies; 4+ messages in thread
From: Jan Kiszka @ 2009-02-25 18:39 UTC (permalink / raw)
  To: xenomai-core

Provide assert_nrt helper that checks if the caller is either not a
shadow thread or is currently running in relaxed mode. If not, SIG_XCPU
is raised.

This service is then used to provide wrappers for glibc functions that
are not realtime-safe but do not always trigger a syscall. Such
functions may therefore be used by RT threads in primary mode for quite
a while without being detected via some Xenomai mode switch. Moreover,
some functions that go through the vsyscall page even to raise an
ordinary syscall may not allow proper stack backtraces, making it harder
to find their callers.

So far we provide wrappers (for use with the --wrap linker switch) for
malloc/free, gettimeofday and clock_gettime. Adding more (if there
are/will be more) is trivial.

Signed-off-by: Jan Kiszka <jan.kiszka@domain.hid>
---

 src/rtdk/Makefile.am      |    5 +-
 src/rtdk/assert_context.c |   97 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+), 2 deletions(-)
 create mode 100644 src/rtdk/assert_context.c

diff --git a/src/rtdk/Makefile.am b/src/rtdk/Makefile.am
index cb80262..5b76b5d 100644
--- a/src/rtdk/Makefile.am
+++ b/src/rtdk/Makefile.am
@@ -1,10 +1,11 @@
 lib_LTLIBRARIES = librtdk.la
 
-librtdk_la_LDFLAGS = -version-info 0:0:0 -lpthread
+librtdk_la_LDFLAGS = -version-info 0:0:0 -lpthread -lrt
 
 librtdk_la_SOURCES = \
 	init.c \
-	rt_print.c
+	rt_print.c \
+	assert_context.c
 
 librtdk_la_CPPFLAGS = \
 	@XENO_USER_CFLAGS@ \
diff --git a/src/rtdk/assert_context.c b/src/rtdk/assert_context.c
new file mode 100644
index 0000000..e01fc93
--- /dev/null
+++ b/src/rtdk/assert_context.c
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2008 Jan Kiszka <jan.kiszka@domain.hid>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+
+#include <signal.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sys/time.h>
+
+#include <nucleus/thread.h>
+#include <asm-generic/syscall.h>
+#include <asm-generic/bits/current.h>
+
+void assert_nrt(void)
+{
+	xnthread_info_t info;
+	int err;
+
+	if (unlikely(xeno_get_current() != XN_NO_HANDLE &&
+		     !(xeno_get_current_mode() & XNRELAX))) {
+
+		err = XENOMAI_SYSCALL1(__xn_sys_current_info, &info);
+
+		if (err) {
+			fprintf(stderr, "__xn_sys_current_info failed: %s\n",
+				strerror(-err));
+			return;
+		}
+
+		if (info.state & XNTRAPSW)
+			pthread_kill(pthread_self(), SIGXCPU);
+	}
+}
+
+/* Memory allocation services */
+__attribute__ ((weak))
+void *__real_malloc(size_t size)
+{
+	return malloc(size);
+}
+
+void *__warp_malloc(size_t size)
+{
+	assert_nrt();
+	return __real_malloc(size);
+}
+
+__attribute__ ((weak))
+void __real_free(void *ptr)
+{
+	free(ptr);
+}
+
+void __warp_free(void *ptr)
+{
+	assert_nrt();
+	__real_free(ptr);
+}
+
+/* vsyscall-based services */
+__attribute__ ((weak))
+int __real_gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+	return gettimeofday(tv, tz);
+}
+
+int __wrap_gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+	assert_nrt();
+	return __real_gettimeofday(tv, tz);
+}
+
+__attribute__ ((weak))
+int __real_clock_gettime(clockid_t clk_id, struct timespec *tp)
+{
+	return clock_gettime(clk_id, tp);
+}
+
+int __wrap_clock_gettime(clockid_t clk_id, struct timespec *tp)
+{
+	assert_nrt();
+	return __real_clock_gettime(clk_id, tp);
+}


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [Xenomai-core] [PATCH 2/2] rtdk: Add assert_context helpers
  2009-02-25 18:39 [Xenomai-core] [PATCH 2/2] rtdk: Add assert_context helpers Jan Kiszka
@ 2009-02-25 18:41 ` Gilles Chanteperdrix
  2009-02-25 18:57   ` Jan Kiszka
  2009-02-26 14:02 ` [Xenomai-core] [PATCH v2 " Jan Kiszka
  1 sibling, 1 reply; 4+ messages in thread
From: Gilles Chanteperdrix @ 2009-02-25 18:41 UTC (permalink / raw)
  To: Jan Kiszka; +Cc: xenomai-core

Jan Kiszka wrote:
> Provide assert_nrt helper that checks if the caller is either not a
> shadow thread or is currently running in relaxed mode. If not, SIG_XCPU
> is raised.
> 
> This service is then used to provide wrappers for glibc functions that
> are not realtime-safe but do not always trigger a syscall. Such
> functions may therefore be used by RT threads in primary mode for quite
> a while without being detected via some Xenomai mode switch. Moreover,
> some functions that go through the vsyscall page even to raise an
> ordinary syscall may not allow proper stack backtraces, making it harder
> to find their callers.
> 
> So far we provide wrappers (for use with the --wrap linker switch) for
> malloc/free, gettimeofday and clock_gettime. Adding more (if there
> are/will be more) is trivial.
> 
> Signed-off-by: Jan Kiszka <jan.kiszka@domain.hid>
> ---
> 
>  src/rtdk/Makefile.am      |    5 +-
>  src/rtdk/assert_context.c |   97 +++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 100 insertions(+), 2 deletions(-)
>  create mode 100644 src/rtdk/assert_context.c
> 
> diff --git a/src/rtdk/Makefile.am b/src/rtdk/Makefile.am
> index cb80262..5b76b5d 100644
> --- a/src/rtdk/Makefile.am
> +++ b/src/rtdk/Makefile.am
> @@ -1,10 +1,11 @@
>  lib_LTLIBRARIES = librtdk.la
>  
> -librtdk_la_LDFLAGS = -version-info 0:0:0 -lpthread
> +librtdk_la_LDFLAGS = -version-info 0:0:0 -lpthread -lrt
>  
>  librtdk_la_SOURCES = \
>  	init.c \
> -	rt_print.c
> +	rt_print.c \
> +	assert_context.c
>  
>  librtdk_la_CPPFLAGS = \
>  	@XENO_USER_CFLAGS@ \
> diff --git a/src/rtdk/assert_context.c b/src/rtdk/assert_context.c
> new file mode 100644
> index 0000000..e01fc93
> --- /dev/null
> +++ b/src/rtdk/assert_context.c
> @@ -0,0 +1,97 @@
> +/*
> + * Copyright (C) 2008 Jan Kiszka <jan.kiszka@domain.hid>.
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2 of the License, or (at your option) any later version.
> + *
> + * This library is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> +
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library; if not, write to the Free Software
> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
> + */
> +
> +#include <signal.h>
> +#include <stdlib.h>
> +#include <time.h>
> +#include <sys/time.h>
> +
> +#include <nucleus/thread.h>
> +#include <asm-generic/syscall.h>
> +#include <asm-generic/bits/current.h>
> +
> +void assert_nrt(void)
> +{
> +	xnthread_info_t info;
> +	int err;
> +
> +	if (unlikely(xeno_get_current() != XN_NO_HANDLE &&
> +		     !(xeno_get_current_mode() & XNRELAX))) {
> +
> +		err = XENOMAI_SYSCALL1(__xn_sys_current_info, &info);
> +
> +		if (err) {
> +			fprintf(stderr, "__xn_sys_current_info failed: %s\n",
> +				strerror(-err));
> +			return;
> +		}
> +
> +		if (info.state & XNTRAPSW)
> +			pthread_kill(pthread_self(), SIGXCPU);
> +	}
> +}
> +
> +/* Memory allocation services */
> +__attribute__ ((weak))
> +void *__real_malloc(size_t size)
> +{
> +	return malloc(size);
> +}
> +
> +void *__warp_malloc(size_t size)
> +{
> +	assert_nrt();
> +	return __real_malloc(size);
> +}

It is wrap not warp, and the __real functions should not be put in the
same compilation unit as the __wrap functions.

-- 
                                                 Gilles.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Xenomai-core] [PATCH 2/2] rtdk: Add assert_context helpers
  2009-02-25 18:41 ` Gilles Chanteperdrix
@ 2009-02-25 18:57   ` Jan Kiszka
  0 siblings, 0 replies; 4+ messages in thread
From: Jan Kiszka @ 2009-02-25 18:57 UTC (permalink / raw)
  To: Gilles Chanteperdrix; +Cc: xenomai-core

Gilles Chanteperdrix wrote:
> Jan Kiszka wrote:
>> Provide assert_nrt helper that checks if the caller is either not a
>> shadow thread or is currently running in relaxed mode. If not, SIG_XCPU
>> is raised.
>>
>> This service is then used to provide wrappers for glibc functions that
>> are not realtime-safe but do not always trigger a syscall. Such
>> functions may therefore be used by RT threads in primary mode for quite
>> a while without being detected via some Xenomai mode switch. Moreover,
>> some functions that go through the vsyscall page even to raise an
>> ordinary syscall may not allow proper stack backtraces, making it harder
>> to find their callers.
>>
>> So far we provide wrappers (for use with the --wrap linker switch) for
>> malloc/free, gettimeofday and clock_gettime. Adding more (if there
>> are/will be more) is trivial.
>>
>> Signed-off-by: Jan Kiszka <jan.kiszka@domain.hid>
>> ---
>>
>>  src/rtdk/Makefile.am      |    5 +-
>>  src/rtdk/assert_context.c |   97 +++++++++++++++++++++++++++++++++++++++++++++
>>  2 files changed, 100 insertions(+), 2 deletions(-)
>>  create mode 100644 src/rtdk/assert_context.c
>>
>> diff --git a/src/rtdk/Makefile.am b/src/rtdk/Makefile.am
>> index cb80262..5b76b5d 100644
>> --- a/src/rtdk/Makefile.am
>> +++ b/src/rtdk/Makefile.am
>> @@ -1,10 +1,11 @@
>>  lib_LTLIBRARIES = librtdk.la
>>  
>> -librtdk_la_LDFLAGS = -version-info 0:0:0 -lpthread
>> +librtdk_la_LDFLAGS = -version-info 0:0:0 -lpthread -lrt
>>  
>>  librtdk_la_SOURCES = \
>>  	init.c \
>> -	rt_print.c
>> +	rt_print.c \
>> +	assert_context.c
>>  
>>  librtdk_la_CPPFLAGS = \
>>  	@XENO_USER_CFLAGS@ \
>> diff --git a/src/rtdk/assert_context.c b/src/rtdk/assert_context.c
>> new file mode 100644
>> index 0000000..e01fc93
>> --- /dev/null
>> +++ b/src/rtdk/assert_context.c
>> @@ -0,0 +1,97 @@
>> +/*
>> + * Copyright (C) 2008 Jan Kiszka <jan.kiszka@domain.hid>.
>> + *
>> + * This library is free software; you can redistribute it and/or
>> + * modify it under the terms of the GNU Lesser General Public
>> + * License as published by the Free Software Foundation; either
>> + * version 2 of the License, or (at your option) any later version.
>> + *
>> + * This library is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> + * Lesser General Public License for more details.
>> +
>> + * You should have received a copy of the GNU Lesser General Public
>> + * License along with this library; if not, write to the Free Software
>> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
>> + */
>> +
>> +#include <signal.h>
>> +#include <stdlib.h>
>> +#include <time.h>
>> +#include <sys/time.h>
>> +
>> +#include <nucleus/thread.h>
>> +#include <asm-generic/syscall.h>
>> +#include <asm-generic/bits/current.h>
>> +
>> +void assert_nrt(void)
>> +{
>> +	xnthread_info_t info;
>> +	int err;
>> +
>> +	if (unlikely(xeno_get_current() != XN_NO_HANDLE &&
>> +		     !(xeno_get_current_mode() & XNRELAX))) {
>> +
>> +		err = XENOMAI_SYSCALL1(__xn_sys_current_info, &info);
>> +
>> +		if (err) {
>> +			fprintf(stderr, "__xn_sys_current_info failed: %s\n",
>> +				strerror(-err));
>> +			return;
>> +		}
>> +
>> +		if (info.state & XNTRAPSW)
>> +			pthread_kill(pthread_self(), SIGXCPU);
>> +	}
>> +}
>> +
>> +/* Memory allocation services */
>> +__attribute__ ((weak))
>> +void *__real_malloc(size_t size)
>> +{
>> +	return malloc(size);
>> +}
>> +
>> +void *__warp_malloc(size_t size)
>> +{
>> +	assert_nrt();
>> +	return __real_malloc(size);
>> +}
> 
> It is wrap not warp, and the __real functions should not be put in the
> same compilation unit as the __wrap functions.

OK, seems to have worked by chance. Will provide an update.

Thanks,
Jan

-- 
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Xenomai-core] [PATCH v2 2/2] rtdk: Add assert_context helpers
  2009-02-25 18:39 [Xenomai-core] [PATCH 2/2] rtdk: Add assert_context helpers Jan Kiszka
  2009-02-25 18:41 ` Gilles Chanteperdrix
@ 2009-02-26 14:02 ` Jan Kiszka
  1 sibling, 0 replies; 4+ messages in thread
From: Jan Kiszka @ 2009-02-26 14:02 UTC (permalink / raw)
  Cc: xenomai-core

[ fixed typos, separated __real functions, exported assert_nrt ]

Provide assert_nrt helper that checks if the caller is either not a
shadow thread or is currently running in relaxed mode. If not, SIG_XCPU
is raised.

This service is then used to provide wrappers for glibc functions that
are not realtime-safe but do not always trigger a syscall. Such
functions may therefore be used by RT threads in primary mode for quite
a while without being detected via some Xenomai mode switch. Moreover,
some functions that go through the vsyscall page even to raise an
ordinary syscall may not allow proper stack backtraces, making it harder
to find their callers.

So far we provide wrappers (for use with the --wrap linker switch) for
malloc/free, gettimeofday and clock_gettime. Adding more (if there
are/will be more) is trivial.

Signed-off-by: Jan Kiszka <jan.kiszka@domain.hid>
---

 include/rtdk.h            |    2 +
 src/rtdk/Makefile.am      |    6 ++--
 src/rtdk/assert_context.c |   73 +++++++++++++++++++++++++++++++++++++++++++++
 src/rtdk/internal.h       |    9 ++++++
 src/rtdk/wrappers.c       |   45 ++++++++++++++++++++++++++++
 5 files changed, 133 insertions(+), 2 deletions(-)
 create mode 100644 src/rtdk/assert_context.c
 create mode 100644 src/rtdk/wrappers.c

diff --git a/include/rtdk.h b/include/rtdk.h
index fc7525d..981bfda 100644
--- a/include/rtdk.h
+++ b/include/rtdk.h
@@ -58,6 +58,8 @@ void rt_print_cleanup(void);
 void rt_print_auto_init(int enable);
 const char *rt_print_buffer_name(void);
 
+void assert_nrt(void);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
diff --git a/src/rtdk/Makefile.am b/src/rtdk/Makefile.am
index cb80262..778c697 100644
--- a/src/rtdk/Makefile.am
+++ b/src/rtdk/Makefile.am
@@ -1,10 +1,12 @@
 lib_LTLIBRARIES = librtdk.la
 
-librtdk_la_LDFLAGS = -version-info 0:0:0 -lpthread
+librtdk_la_LDFLAGS = -version-info 0:0:0 -lpthread -lrt
 
 librtdk_la_SOURCES = \
 	init.c \
-	rt_print.c
+	rt_print.c \
+	assert_context.c \
+	wrappers.c
 
 librtdk_la_CPPFLAGS = \
 	@XENO_USER_CFLAGS@ \
diff --git a/src/rtdk/assert_context.c b/src/rtdk/assert_context.c
new file mode 100644
index 0000000..2176db3
--- /dev/null
+++ b/src/rtdk/assert_context.c
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2008, 2009 Jan Kiszka <jan.kiszka@domain.hid>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+
+#include <signal.h>
+#include <stdlib.h>
+
+#include "internal.h"
+
+#include <nucleus/thread.h>
+#include <asm-generic/syscall.h>
+#include <asm-generic/bits/current.h>
+
+void assert_nrt(void)
+{
+	xnthread_info_t info;
+	int err;
+
+	if (unlikely(xeno_get_current() != XN_NO_HANDLE &&
+		     !(xeno_get_current_mode() & XNRELAX))) {
+
+		err = XENOMAI_SYSCALL1(__xn_sys_current_info, &info);
+
+		if (err) {
+			fprintf(stderr, "__xn_sys_current_info failed: %s\n",
+				strerror(-err));
+			return;
+		}
+
+		if (info.state & XNTRAPSW)
+			pthread_kill(pthread_self(), SIGXCPU);
+	}
+}
+
+/* Memory allocation services */
+void *__wrap_malloc(size_t size)
+{
+	assert_nrt();
+	return __real_malloc(size);
+}
+
+void __wrap_free(void *ptr)
+{
+	assert_nrt();
+	__real_free(ptr);
+}
+
+/* vsyscall-based services */
+int __wrap_gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+	assert_nrt();
+	return __real_gettimeofday(tv, tz);
+}
+
+int __wrap_clock_gettime(clockid_t clk_id, struct timespec *tp)
+{
+	assert_nrt();
+	return __real_clock_gettime(clk_id, tp);
+}
diff --git a/src/rtdk/internal.h b/src/rtdk/internal.h
index 69b869f..bd15b2d 100644
--- a/src/rtdk/internal.h
+++ b/src/rtdk/internal.h
@@ -19,6 +19,15 @@
 #ifndef _LIBRTUTILS_INTERNAL_H
 #define _LIBRTUTILS_INTERNAL_H
 
+#include <time.h>
+#include <sys/time.h>
+
 void __rt_print_init(void);
 
+void __real_free(void *ptr);
+void *__real_malloc(size_t size);
+
+int __real_gettimeofday(struct timeval *tv, struct timezone *tz);
+int __real_clock_gettime(clockid_t clk_id, struct timespec *tp);
+
 #endif /* !_LIBRTUTILS_INTERNAL_H */
diff --git a/src/rtdk/wrappers.c b/src/rtdk/wrappers.c
new file mode 100644
index 0000000..e5340c9
--- /dev/null
+++ b/src/rtdk/wrappers.c
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2008, 2009 Jan Kiszka <jan.kiszka@domain.hid>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+
+#include <stdlib.h>
+
+#include "internal.h"
+
+__attribute__ ((weak))
+void *__real_malloc(size_t size)
+{
+	return malloc(size);
+}
+
+__attribute__ ((weak))
+void __real_free(void *ptr)
+{
+	free(ptr);
+}
+
+__attribute__ ((weak))
+int __real_gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+	return gettimeofday(tv, tz);
+}
+
+__attribute__ ((weak))
+int __real_clock_gettime(clockid_t clk_id, struct timespec *tp)
+{
+	return clock_gettime(clk_id, tp);
+}


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-02-26 14:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-25 18:39 [Xenomai-core] [PATCH 2/2] rtdk: Add assert_context helpers Jan Kiszka
2009-02-25 18:41 ` Gilles Chanteperdrix
2009-02-25 18:57   ` Jan Kiszka
2009-02-26 14:02 ` [Xenomai-core] [PATCH v2 " Jan Kiszka

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.