Strange segmentation fault
Hi,
I'm experiencing an strange segmentation fault problem. I've boiled down the code to the simplest possible program that causes the segfault:
1- Set the timer in oneshot mode. 2- Create a task.
3- Start the task.
At the task function:
1- Make the task periodic (1s). 2- Issue a fprintf.
That's all. The code segfaults. If I remove the fprintf, it won't segfault. If I change it for a printf, it won't segfault.
If I use fprintf but print just "TICK" instead of "TICK %u", it won't segfault while the task is running, but it will when the program is interrupted (not shown here, but I tried this with a signal handler in place that properly deleted the task, though I think this is not absolutely necessary).
I'm totally confused.
I know I can use fprintf as long as I assume that the RT task will go into "secondary" mode at this point because it'll be using linux services, and that it will go back to "primary" RT mode on the next call to rt_task_wait_period.
Also, I know that it would be best to use periodic instead of oneshot mode, but my program is obviously more complex than this boiled down example and really needs oneshot mode.
Any ideas about what could be the problem?
Thanks.
P.S: This is the same problem I described earlier inthe list as caused by inb. I though that inb was the cause because removing it avoided the segfault, however, it turns out that removing the inb was causing another piece of code containing the fprintf not to execute.
//===========================================================================
#include <sys/types.h> #include <sys/mman.h> #include <sys/io.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <native/task.h>
#include "main.h"
static RT_TASK _io_task;
static void _io_proc (void *arg) {
u_long overrun;
rt_task_set_periodic(&_io_task, TM_NOW,rt_timer_ns2ticks(1000000000LL));
for (;;) { rt_task_wait_period(&overrun);
fprintf(stderr, "TICK %u\n", 1); }
}
static void _cleanup (void) {
rt_task_delete(&_io_task);
}
static int _init (void) {
int r;
r = rt_timer_set_mode(TM_ONESHOT);
if (r < 0) {
fprintf(stderr, "ERROR %i initializing RT\n", r);
_cleanup(); return r;
}
r = rt_task_create(&_io_task, "rtcore_io_task", 0x1000, 10, T_FPU);
if (r < 0) {
fprintf(stderr, "ERROR %i creating task\n", r);
_cleanup(); return r;
}
r = rt_task_start(&_io_task, &_io_proc, NULL);
if (r < 0) {
fprintf(stderr, "ERROR %i starting task\n", r);
_cleanup(); return r;
} return 0;
}
int main (int argc, char **argv) {
iopl(3); mlockall(MCL_CURRENT | MCL_FUTURE); if ( _init() < 0) return 1; for (;;) sleep(1); _cleanup();
return 0;
}
Quoted 86 lines · Ignacio García Pérez
Hi,
I'm experiencing an strange segmentation fault problem. I've boiled down the code to the simplest possible program that causes the segfault:
1- Set the timer in oneshot mode.
2- Create a task.
3- Start the task.At the task function:
1- Make the task periodic (1s).
2- Issue a fprintf.That's all. The code segfaults. If I remove the fprintf, it won't segfault. If I change it for a printf, it won't segfault.
If I use fprintf but print just "TICK" instead of "TICK %u", it won't segfault while the task is running, but it will when the program is interrupted (not shown here, but I tried this with a signal handler in place that properly deleted the task, though I think this is not absolutely necessary).
I'm totally confused.
I know I can use fprintf as long as I assume that the RT task will go into "secondary" mode at this point because it'll be using linux services, and that it will go back to "primary" RT mode on the next call to
rt_task_wait_period.Also, I know that it would be best to use periodic instead of oneshot mode, but my program is obviously more complex than this boiled down example and really needs oneshot mode.
Any ideas about what could be the problem?
Thanks.
P.S: This is the same problem I described earlier inthe list as caused by inb. I though that inb was the cause because removing it avoided the segfault, however, it turns out that removing the inb was causing another piece of code containing the fprintf not to execute.//===========================================================================
#include <
sys/types.h>
#include <sys/mman.h>
#include <sys/io.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>#include <
native/task.h>#include "
main.h"static
RT_TASK_io_task;static void
_io_proc(void *arg) {u_long overrun; rt_task_set_periodic(&_io_task, TM_NOW,
rt_timer_ns2ticks(1000000000LL));for (;;) {rt_task_wait_period(&overrun); fprintf(stderr, "TICK %u\n", 1);}
}static void _cleanup (void) { rt_task_delete(&_io_task); }static int
_init(void) {
Quoted 9 lines
int r;
r = rt_timer_set_mode(TM_ONESHOT);
if (r < 0) {
fprintf(stderr, "ERROR %i initializing RT\n", r);
_cleanup(); return r;
}
r = rt_task_create(&_io_task, "rtcore_io_task", 0x1000, 10, T_FPU);Try PTHREAD_STACK_MIN instead of 0x1000 - or, better, leave it 0 and let Xenomai pick a value for you. Unless you need more than the default stack (PTHREAD_STACK_MIN * 4) or you are *sure* to get a way with PTHREAD_STACK_MIN, you should avoid touching this value.
Quoted 27 lines
if (r < 0) { fprintf(stderr, "ERROR %i creating task\n", r); _cleanup(); return r; } r = rt_task_start(&_io_task, &_io_proc, NULL); if (r < 0) { fprintf(stderr, "ERROR %i starting task\n", r); _cleanup(); return r; }return 0; }int main (int argc, char **argv) {
iopl(3); mlockall(MCL_CURRENT | MCL_FUTURE); if ( _init() < 0) return 1; for (;;) sleep(1); _cleanup();
Quoted 3 lines
return 0; }
Jan
Quoted 1 line · Ignacio García Pérez
r = rt_task_create(&_io_task, "rtcore_io_task", 0x1000, 10, T_FPU);
Try replacing 0x1000 with say 32000 or 64000. printf uses the stack, so need enough room to operate properly.
Signature · 1 line
Gilles Chanteperdrix.
Quoted 7 lines · Gilles Chanteperdrix
Quoted 1 line · Ignacio García Pérez
r = rt_task_create(&_io_task, "rtcore_io_task", 0x1000, 10, T_FPU);Try replacing
0x1000with say 32000 or 64000. printf uses the stack, so need enough room to operate properly.
Yeah, I should have though about that. Stupid me. As I suspected, it was something truly obvious.
Sorry, for the noise, guys.
Nacho.