Skip to main content
summaryrefslogtreecommitdiffstats
blob: f96a07a894c55d7f77c6f3441d65e678422a18c5 (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*******************************************************************************
 * Copyright (c) 2009, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 * You may elect to redistribute this code under either of these licenses.
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/

/*
 * This module holds execution context memory maps.
 */

#include <config.h>

#if SERVICE_MemoryMap

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <framework/myalloc.h>
#include <framework/trace.h>
#include <framework/json.h>
#include <framework/events.h>
#include <framework/exceptions.h>
#include <services/memorymap.h>

typedef struct Listener {
    MemoryMapEventListener * listener;
    void * args;
} Listener;

typedef struct ContextExtensionMM {
    int valid;
    ErrorReport * error;
    MemoryMap target_map;
    MemoryMap client_map;
} ContextExtensionMM;

typedef struct ClientMap {
    LINK link_list;
    LINK link_hash;
    LINK link_ctx;
    char * id;
    MemoryMap map;
    Channel * channel;
} ClientMap;

static size_t context_extension_offset = 0;

#define EXT(ctx) ((ContextExtensionMM *)((char *)(ctx) + context_extension_offset))

static const char MEMORY_MAP[] = "MemoryMap";

static Listener * listeners = NULL;
static unsigned listener_cnt = 0;
static unsigned listener_max = 0;

#define list2map(A)    ((ClientMap *)((char *)(A) - offsetof(ClientMap, link_list)))
#define hash2map(A)    ((ClientMap *)((char *)(A) - offsetof(ClientMap, link_hash)))
#define ctx2map(A)     ((ClientMap *)((char *)(A) - offsetof(ClientMap, link_ctx)))

#define MAP_HASH_SIZE (4 * MEM_USAGE_FACTOR - 1)
static LINK client_map_list;
static LINK client_map_hash[MAP_HASH_SIZE];

static TCFBroadcastGroup * broadcast_group = NULL;

static unsigned map_id_hash(const char * id) {
    int i;
    unsigned h = 0;
    for (i = 0; id[i]; i++) h += id[i];
    return h % MAP_HASH_SIZE;
}

static MemoryRegion * add_region(MemoryMap * map) {
    MemoryRegion * r = NULL;
    if (map->region_cnt >= map->region_max) {
        map->region_max += 8;
        map->regions = (MemoryRegion *)loc_realloc(map->regions, sizeof(MemoryRegion) * map->region_max);
    }
    r = map->regions + map->region_cnt++;
    memset(r, 0, sizeof(MemoryRegion));
    return r;
}

static int str_equ(char * x, char * y) {
    if (x == y) return 1;
    if (x == NULL) return 0;
    if (y == NULL) return 0;
    return strcmp(x, y) == 0;
}

static unsigned find_maps(LINK * maps, const char * id) {
    LINK * l;
    unsigned cnt = 0;
    unsigned h = map_id_hash(id);
    for (l = client_map_hash[h].next; l != &client_map_hash[h]; l = l->next) {
        ClientMap * m = hash2map(l);
        if (list_is_empty(&m->link_ctx) && strcmp(m->id, id) == 0) {
            list_add_last(&m->link_ctx, maps);
            cnt += m->map.region_cnt;
        }
    }
    return cnt;
}

static Context * get_mem_context(Context * ctx) {
#if ENABLE_DebugContext
    ctx = context_get_group(ctx, CONTEXT_GROUP_PROCESS);
#endif
    return ctx;
}

static Context * get_sym_context(Context * ctx) {
#if ENABLE_DebugContext
    ctx = context_get_group(ctx, CONTEXT_GROUP_SYMBOLS);
#endif
    return ctx;
}

static void update_context_client_map(Context * ctx) {
    ContextExtensionMM * ext = EXT(ctx);
    Context * syms = get_sym_context(ctx);
    unsigned r_cnt = 0;
    int equ = 0;
    unsigned i;
    LINK * l;
    LINK maps;

    if (syms == NULL) return;
    assert(ctx == get_mem_context(ctx));
    list_init(&maps);
    r_cnt += find_maps(&maps, syms->id);
    if (syms->name != NULL) {
        r_cnt += find_maps(&maps, syms->name);
        r_cnt += find_maps(&maps, context_full_name(syms));
    }
    equ = ext->client_map.region_cnt == r_cnt;
    if (equ) {
        unsigned k = 0;
        for (l = maps.next; equ && l != &maps; l = l->next) {
            ClientMap * m = ctx2map(l);
            for (i = 0; equ && i < m->map.region_cnt; i++) {
                MemoryRegion * x = m->map.regions + i;
                MemoryRegion * y = ext->client_map.regions + k++;
                equ =
                    y->addr == x->addr &&
                    y->size == x->size &&
                    y->file_offs == x->file_offs &&
                    y->bss == x->bss &&
                    y->flags == x->flags &&
                    str_equ(y->file_name, x->file_name) &&
                    str_equ(y->sect_name, x->sect_name);
            }
        }
        assert(!equ || k == r_cnt);
    }
    if (!equ) {
        context_clear_memory_map(&ext->client_map);
        for (l = maps.next; l != &maps; l = l->next) {
            ClientMap * m = ctx2map(l);
            for (i = 0; i < m->map.region_cnt; i++) {
                MemoryRegion * x = m->map.regions + i;
                MemoryRegion * y = add_region(&ext->client_map);
                y->addr = x->addr;
                y->size = x->size;
                y->file_offs = x->file_offs;
                y->bss = x->bss;
                y->flags = x->flags;
                if (x->file_name) y->file_name = loc_strdup(x->file_name);
                if (x->sect_name) y->sect_name = loc_strdup(x->sect_name);
                if (x->id) y->id = loc_strdup(x->id);
            }
        }
        assert(ext->client_map.region_cnt == r_cnt);
    }
    while (!list_is_empty(&maps)) list_remove(maps.next);
    if (!equ) memory_map_event_mapping_changed(ctx);
}

static void update_all_context_client_maps(void) {
    LINK * l;
    for (l = context_root.next; l != &context_root; l = l->next) {
        Context * ctx = ctxl2ctxp(l);
        if (ctx->exited) continue;
        if (ctx != get_mem_context(ctx)) continue;
        update_context_client_map(ctx);
    }
}

static void event_memory_map_changed(Context * ctx, void * args) {
    OutputStream * out;
    ContextExtensionMM * ext = EXT(ctx);

    if (ctx->exited) return;
    if (!ext->valid) return;
    if (ctx != get_mem_context(ctx)) return;

    context_clear_memory_map(&ext->target_map);
    ext->valid = 0;

    out = &broadcast_group->out;

    write_stringz(out, "E");
    write_stringz(out, MEMORY_MAP);
    write_stringz(out, "changed");

    json_write_string(out, ctx->id);
    write_stream(out, 0);
    write_stream(out, MARKER_EOM);
}

static void event_context_changed(Context * ctx, void * args) {
    if (ctx->exited) return;
    if (ctx != get_mem_context(ctx)) return;
    update_context_client_map(ctx);
}

static void event_context_disposed(Context * ctx, void * args) {
    MemoryMap * map;
    ContextExtensionMM * ext = EXT(ctx);

    map = &ext->target_map;
    context_clear_memory_map(map);
    loc_free(map->regions);
    memset(map, 0, sizeof(MemoryMap));

    map = &ext->client_map;
    context_clear_memory_map(map);
    loc_free(map->regions);
    memset(map, 0, sizeof(MemoryMap));

    release_error_report(ext->error);
}

int memory_map_get(Context * ctx, MemoryMap ** client_map, MemoryMap ** target_map) {
    ContextExtensionMM * ext = EXT(ctx);
    assert(ctx == get_mem_context(ctx));
#if ENABLE_DebugContext
    if (!ext->valid) {
        context_clear_memory_map(&ext->target_map);
        release_error_report(ext->error);
        if (context_get_memory_map(ctx, &ext->target_map) < 0) {
            ext->error = get_error_report(errno);
            ext->valid = get_error_code(errno) != ERR_CACHE_MISS;
        }
        else {
            ext->error = NULL;
            ext->valid = 1;
        }
    }
#endif
    if (ext->error != NULL) {
        set_error_report_errno(ext->error);
        return -1;
    }
    *client_map = &ext->client_map;
    *target_map = &ext->target_map;
    return 0;
}

void memory_map_event_module_loaded(Context * ctx) {
    unsigned i;
    assert(ctx->ref_count > 0);
    assert(ctx == get_mem_context(ctx));
    event_memory_map_changed(ctx, NULL);
    for (i = 0; i < listener_cnt; i++) {
        Listener * l = listeners + i;
        if (l->listener->module_loaded == NULL) continue;
        l->listener->module_loaded(ctx, l->args);
    }
}

void memory_map_event_code_section_ummapped(Context * ctx, ContextAddress addr, ContextAddress size) {
    unsigned i;
    assert(ctx->ref_count > 0);
    assert(ctx == get_mem_context(ctx));
    event_memory_map_changed(ctx, NULL);
    for (i = 0; i < listener_cnt; i++) {
        Listener * l = listeners + i;
        if (l->listener->code_section_ummapped == NULL) continue;
        l->listener->code_section_ummapped(ctx, addr, size, l->args);
    }
}

void memory_map_event_module_unloaded(Context * ctx) {
    unsigned i;
    assert(ctx->ref_count > 0);
    assert(ctx == get_mem_context(ctx));
    event_memory_map_changed(ctx, NULL);
    for (i = 0; i < listener_cnt; i++) {
        Listener * l = listeners + i;
        if (l->listener->module_unloaded == NULL) continue;
        l->listener->module_unloaded(ctx, l->args);
    }
}

void memory_map_event_mapping_changed(Context * ctx) {
    unsigned i;
    assert(ctx->ref_count > 0);
    assert(ctx == get_mem_context(ctx));
    event_memory_map_changed(ctx, NULL);
    for (i = 0; i < listener_cnt; i++) {
        Listener * l = listeners + i;
        if (l->listener->mapping_changed == NULL) continue;
        l->listener->mapping_changed(ctx, l->args);
    }
}

void add_memory_map_event_listener(MemoryMapEventListener * listener, void * client_data) {
    Listener * l = NULL;
    if (listener_cnt >= listener_max) {
        listener_max += 8;
        listeners = (Listener *)loc_realloc(listeners, listener_max * sizeof(Listener));
    }
    l = listeners + listener_cnt++;
    l->listener = listener;
    l->args = client_data;
}

static void write_map_region(OutputStream * out, MemoryRegion * m) {
    MemoryRegionAttribute * x = m->attrs;

    write_stream(out, '{');
    json_write_string(out, "Addr");
    write_stream(out, ':');
    json_write_uint64(out, m->addr);
    write_stream(out, ',');
    json_write_string(out, "Size");
    write_stream(out, ':');
    json_write_uint64(out, m->size);
    write_stream(out, ',');
    json_write_string(out, "Flags");
    write_stream(out, ':');
    json_write_ulong(out, m->flags);
    if (m->file_name != NULL) {
        write_stream(out, ',');
        json_write_string(out, "FileName");
        write_stream(out, ':');
        json_write_string(out, m->file_name);
        write_stream(out, ',');
        if (m->sect_name != NULL) {
            json_write_string(out, "SectionName");
            write_stream(out, ':');
            json_write_string(out, m->sect_name);
        }
        else {
            json_write_string(out, "Offs");
            write_stream(out, ':');
            json_write_uint64(out, m->file_offs);
        }
        if (m->bss) {
            write_stream(out, ',');
            json_write_string(out, "BSS");
            write_stream(out, ':');
            json_write_boolean(out, m->bss);
        }
    }
    if (m->id != NULL) {
        write_stream(out, ',');
        json_write_string(out, "ID");
        write_stream(out, ':');
        json_write_string(out, m->id);
    }
    while (x != NULL) {
        write_stream(out, ',');
        json_write_string(out, x->name);
        write_stream(out, ':');
        write_string(out, x->value);
        x = x->next;
    }
    write_stream(out, '}');
}

static void command_get(char * token, Channel * c) {
    char id[256];
    int err = 0;
    Context * ctx = NULL;
    MemoryMap * client_map = NULL;
    MemoryMap * target_map = NULL;

    json_read_string(&c->inp, id, sizeof(id));
    if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX);
    if (read_stream(&c->inp) != MARKER_EOM) exception(ERR_JSON_SYNTAX);

    ctx = id2ctx(id);
    if (ctx == NULL) err = ERR_INV_CONTEXT;
    else ctx = get_mem_context(ctx);

    if (!err && memory_map_get(ctx, &client_map, &target_map) < 0) err = errno;

    write_stringz(&c->out, "R");
    write_stringz(&c->out, token);
    write_errno(&c->out, err);
    if (err) {
        write_stringz(&c->out, "null");
    }
    else {
        unsigned n;
        unsigned cnt = 0;
        write_stream(&c->out, '[');
        for (n = 0; n < client_map->region_cnt; n++) {
            if (cnt > 0) write_stream(&c->out, ',');
            write_map_region(&c->out, client_map->regions + n);
            cnt++;
        }
        for (n = 0; n < target_map->region_cnt; n++) {
            if (cnt > 0) write_stream(&c->out, ',');
            write_map_region(&c->out, target_map->regions + n);
            cnt++;
        }
        write_stream(&c->out, ']');
        write_stream(&c->out, 0);
    }

    write_stream(&c->out, MARKER_EOM);
}

static void read_map_attribute(InputStream * inp, const char * name, void * args) {
    MemoryRegion * r = (MemoryRegion *)args;
    if (strcmp(name, "Addr") == 0) r->addr = (ContextAddress)json_read_uint64(inp);
    else if (strcmp(name, "Size") == 0) r->size = (ContextAddress)json_read_uint64(inp);
    else if (strcmp(name, "Offs") == 0) r->file_offs = json_read_uint64(inp);
    else if (strcmp(name, "BSS") == 0) r->bss = json_read_boolean(inp);
    else if (strcmp(name, "Flags") == 0) r->flags = (unsigned)json_read_long(inp);
    else if (strcmp(name, "FileName") == 0) r->file_name = json_read_alloc_string(inp);
    else if (strcmp(name, "SectionName") == 0) r->sect_name = json_read_alloc_string(inp);
    else if (strcmp(name, "ID") == 0) r->id = json_read_alloc_string(inp);
    else {
        MemoryRegionAttribute * x = (MemoryRegionAttribute *)loc_alloc(sizeof(MemoryRegionAttribute));
        x->name = loc_strdup(name);
        x->value = json_read_object(inp);
        x->next = r->attrs;
        r->attrs = x;
    }
}

static void read_map_item(InputStream * inp, void * args) {
    MemoryMap * map = (MemoryMap *)args;
    MemoryRegion * r = add_region(map);

    json_read_struct(inp, read_map_attribute, r);
}

static void command_set(char * token, Channel * c) {
    char id[256];
    unsigned h;
    ClientMap * cm = NULL;
    LINK * l = NULL;
    MemoryMap map;

    memset(&map, 0, sizeof(map));

    json_read_string(&c->inp, id, sizeof(id));
    if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX);
    json_read_array(&c->inp, read_map_item, &map);
    if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX);
    if (read_stream(&c->inp) != MARKER_EOM) exception(ERR_JSON_SYNTAX);

    h = map_id_hash(id);
    for (l = client_map_hash[h].next; l != &client_map_hash[h]; l = l->next) {
        ClientMap * m = hash2map(l);
        if (m->channel == c && strcmp(m->id, id) == 0) {
            context_clear_memory_map(&m->map);
            loc_free(m->map.regions);
            cm = m;
            break;
        }
    }
    if (map.region_cnt > 0) {
        if (cm == NULL) {
            cm = (ClientMap *)loc_alloc_zero(sizeof(ClientMap));
            cm->id = loc_strdup(id);
            cm->channel = c;
            list_add_last(&cm->link_list, &client_map_list);
            list_add_last(&cm->link_hash, &client_map_hash[h]);
        }
        cm->map = map;
        update_all_context_client_maps();
    }
    else if (cm != NULL) {
        list_remove(&cm->link_list);
        list_remove(&cm->link_hash);
        loc_free(cm->id);
        loc_free(cm);
        update_all_context_client_maps();
    }

    write_stringz(&c->out, "R");
    write_stringz(&c->out, token);
    write_errno(&c->out, 0);
    write_stream(&c->out, MARKER_EOM);
}

static void channel_close_listener(Channel * c) {
    int notify = 0;
    LINK * l = client_map_list.next;
    while (l != &client_map_list) {
        ClientMap * m = list2map(l);
        l = l->next;
        if (m->channel != c) continue;
        list_remove(&m->link_list);
        list_remove(&m->link_hash);
        context_clear_memory_map(&m->map);
        loc_free(m->map.regions);
        loc_free(m->id);
        loc_free(m);
        notify = 1;
    }
    if (notify) update_all_context_client_maps();
}

void ini_memory_map_service(Protocol * proto, TCFBroadcastGroup * bcg) {
    int i;
    static ContextEventListener listener = {
        event_context_changed,
        NULL,
        NULL,
        NULL,
        event_context_changed,
        event_context_disposed
    };
    broadcast_group = bcg;
    add_channel_close_listener(channel_close_listener);
    list_init(&client_map_list);
    for (i = 0; i < MAP_HASH_SIZE; i++) {
        list_init(&client_map_hash[i]);
    }
    add_context_event_listener(&listener, NULL);
    add_command_handler(proto, MEMORY_MAP, "get", command_get);
    add_command_handler(proto, MEMORY_MAP, "set", command_set);
    context_extension_offset = context_extension(sizeof(ContextExtensionMM));
}


#endif

Back to the top