summaryrefslogtreecommitdiffstats
path: root/c/src/libmisc/monitor/mon-monitor.c
blob: aa466143f906d70c45d5beb9de81581bb7043b5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 *	@(#)monitor.c	1.6 - 95/04/24
 *	
 */

/*
 *  mon-task.c
 *
 *  Description:
 *	RTEMS monitor task
 *	
 *	
 *	
 *  TODO:
 *	add pause command (monitor sleeps for 'n' ticks, then wakes up)
 *      
 */

#include <rtems.h>
/* #include <bsp.h> */

#include "symbols.h"
#include "monitor.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#define STREQ(a,b)	(strcmp(a,b) == 0)

/* set by trap handler */
extern rtems_tcb       *debugger_interrupted_task;
extern rtems_context   *debugger_interrupted_task_context;
extern rtems_unsigned32 debugger_trap;

/* our task id needs to be public so any debugger can resume us */
rtems_unsigned32        rtems_monitor_task_id;


rtems_symbol_table_t *rtems_monitor_symbols;


#ifndef MONITOR_PROMPT
#define MONITOR_PROMPT "rtems> "
#endif

#define MONITOR_WAKEUP_EVENT   RTEMS_EVENT_0

/*
 *  Function:	rtems_monitor_init
 *
 *  Description:
 *	Create the RTEMS monitor task
 *	
 *  Parameters:
 *	'monitor_suspend' arg is passed as initial arg to monitor task
 *      If TRUE, monitor will suspend itself as it starts up.  Otherwise
 *      it will begin its command loop.
 *	
 *  Returns:
 *	
 *	
 *  Side Effects:
 *	
 *	
 *  Notes:
 *	
 *	
 *  Deficiencies/ToDo:
 *	
 *	
 */

/*
 * make_argv(cp): token-count
 *	Break up the command line in 'cp' into global argv[] and argc (return
 *	value).
 */

int
rtems_monitor_make_argv(
    char *cp,
    int  *argc_p,
    char **argv)
{
    int argc = 0;

    while ((cp = strtok(cp, " \t\n\r")))
    {
	argv[argc++] = cp;
	cp = (char *) NULL;
    }
    argv[argc] = (char *) NULL;			/* end of argv */

    return *argc_p = argc;
}

void
rtems_monitor_init(rtems_boolean monitor_suspend)
{
    rtems_status_code status;
    
    status = rtems_task_create(rtems_build_name('R', 'M', 'O', 'N'),
                               1, 0/*stack*/, RTEMS_NO_PREEMPT | RTEMS_INTERRUPT_LEVEL(0), RTEMS_DEFAULT_ATTRIBUTES, &rtems_monitor_task_id);
    if (status != RTEMS_SUCCESSFUL)
    {
        printf("could not create monitor task\n");
        goto done;
    }

    rtems_monitor_symbols_loadup();

    status = rtems_task_start(rtems_monitor_task_id, rtems_monitor_task, monitor_suspend);
    if (status != RTEMS_SUCCESSFUL)
    {
        printf("could not start monitor!\n");
        goto done;
    }

done:
}

rtems_status_code
rtems_monitor_suspend(rtems_interval timeout)
{
    rtems_event_set event_set;
    rtems_status_code status;
    
    status = rtems_event_receive(MONITOR_WAKEUP_EVENT, RTEMS_DEFAULT_OPTIONS, timeout, &event_set);
    return status;
}

void
rtems_monitor_wakeup(void)
{
    rtems_status_code status;
    
    status = rtems_event_send(rtems_monitor_task_id, MONITOR_WAKEUP_EVENT);
}


/*
 * Read and break up a monitor command
 *
 * We have to loop on the gets call, since it will return NULL under UNIX
 *  RTEMS when we get a signal (eg: SIGALRM).
 */

int
rtems_monitor_read_command(char *command,
                           int  *argc,
                           char **argv)
{
    printf("%s", MONITOR_PROMPT);  fflush(stdout);
    while (gets(command) == (char *) 0)
        ;
    return rtems_monitor_make_argv(command, argc, argv);
}

void
rtems_monitor_task(rtems_task_argument monitor_suspend)
{
    rtems_tcb *debugee = 0;
    char command[513];
    rtems_context *rp;
    rtems_context_fp *fp;
    char *cp;
    int argc;
    char *argv[64];        

    if ((rtems_boolean) monitor_suspend)
        (void) rtems_monitor_suspend(RTEMS_NO_TIMEOUT);

    for (;;)
    {
        extern rtems_tcb * _Thread_Executing;
        debugee = _Thread_Executing;
        rp = &debugee->Registers;
        fp = (rtems_context_fp *) debugee->fp_context;  /* possibly 0 */

        if (0 == rtems_monitor_read_command(command, &argc, argv))
            continue;
        
        if (STREQ(argv[0], "quit"))
            rtems_monitor_suspend(RTEMS_NO_TIMEOUT);
        else if (STREQ(argv[0], "pause"))
            rtems_monitor_suspend(1);

#ifdef CPU_INVOKE_DEBUGGER
        else if (STREQ(argv[0], "debug"))
        {
            CPU_INVOKE_DEBUGGER;
        }
#endif            
        else if (STREQ(argv[0], "symbol"))
        {
            char *symbol;
            char *value;

            if (argc != 3)
            {
                printf("usage: symbol symname symvalue\n");
                continue;
            }

            symbol = argv[1];
            value = argv[2];
            if (symbol && value)
            {
                rtems_symbol_t *sp;
                sp = rtems_symbol_create(rtems_monitor_symbols,
                                         symbol,
                                         (rtems_unsigned32) strtoul(value, 0, 16));
                if (sp)
                    printf("symbol defined is at %p\n", sp);
                else
                    printf("could not define symbol\n");
            }
            else
                printf("parsing error\n");
        }
        else
        {
            printf("Unrecognized command: '%s'\n", argv[0]);
        }
    }
}

/*
 *  Function:   rtems_monitor_symbols_loadup
 *
 *  Description:
 *      Create and load the monitor's symbol table.
 *      We are reading the output format of 'gnm' which looks like this:
 *
 *              400a7068 ? _Rate_monotonic_Information
 *              400a708c ? _Thread_Dispatch_disable_level
 *              400a7090 ? _Configuration_Table
 *
 *
 *      We ignore the type field.
 *
 *  Parameters:
 *
 *
 *  Returns:
 *
 *
 *  Side Effects:
 *      Creates and fills in 'rtems_monitor_symbols' table
 *
 *  Notes:
 *
 *
 *  Deficiencies/ToDo:
 *      Someday this should know BFD
 *              Maybe we could get objcopy to just copy the symbol areas
 *              and copy that down.
 *
 */

void
rtems_monitor_symbols_loadup(void)
{
    FILE *fp;
    char buffer[128];

    rtems_monitor_symbols = rtems_symbol_table_create(10);
    if (rtems_monitor_symbols == 0)
        return;

    fp = fdopen(8, "r");
    if (fp == 0)
        return;

    while (fgets(buffer, sizeof(buffer) - 1, fp))
    {
        char *symbol;
        char *value;
        char *ignored_type;

        value = strtok(buffer, " \t\n");
        ignored_type = strtok(0, " \t\n");
        symbol = strtok(0, " \t\n");

        if (symbol && ignored_type && value)
        {
            rtems_symbol_t *sp;
            sp = rtems_symbol_create(rtems_monitor_symbols,
                                     symbol,
                                     (rtems_unsigned32) strtoul(value, 0, 16));
            if (sp == 0)
            {
                printf("could not define symbol\n");
                goto done;
            }
        }
        else
        {
            printf("parsing error\n");
            goto done;
        }
    }

done:
}