summaryrefslogtreecommitdiff
path: root/process.c
blob: fbb57adadbb668c4652cfbcba14bb6d91e8f171d (plain)
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/* MemProf -- memory profiler and leak detector
 * Copyright 1999, 2000, 2001, Red Hat, Inc.
 * Copyright 2002, Kristian Rietveld
 *
 * Sysprof -- Sampling, systemwide CPU profiler
 * Copyright 2004-2007 Soeren Sandmann
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>

#include "process.h"
#include "binfile.h"

static GHashTable *processes_by_pid;

typedef struct Map Map;
struct Map
{
    char *	filename;
    gulong	start;
    gulong	end;
    gulong      offset;
    gulong	inode;
    
    BinFile *	bin_file;
};

struct Process
{
    char *	cmdline;
    
    int		n_maps;
    Map *	maps;
    
    GList *	bad_pages;
    
    int		pid;
    
    char *	undefined;
};

static void
initialize (void)
{
    if (!processes_by_pid)
	processes_by_pid = g_hash_table_new (g_direct_hash, g_direct_equal);
}

static Map *
read_maps (int pid, int *n_maps)
{
    char *name = g_strdup_printf ("/proc/%d/maps", pid);
    char buffer[1024];
    FILE *in;
    GArray *result;

    in = fopen (name, "r");
    if (!in)
    {
	g_free (name);
	return NULL;
    }

    result = g_array_new (FALSE, FALSE, sizeof (Map));
    
    while (fgets (buffer, sizeof (buffer) - 1, in))
    {
	char file[256];
	int count;
	gulong start;
	gulong end;
	gulong offset;
	gulong inode;
	
	count = sscanf (
	    buffer, "%lx-%lx %*15s %lx %*x:%*x %lu %255s", 
	    &start, &end, &offset, &inode, file);
	if (count == 5)
	{
	    Map map;
	    
	    map.filename = g_strdup (file);
	    map.start = start;
	    map.end = end;
	    
	    if (strcmp (map.filename, "[vdso]") == 0)
	    {
		/* For the vdso, the kernel reports 'offset' as the
		 * the same as the mapping addres. This doesn't make
		 * any sense to me, so we just zero it here. There
		 * is code in binfile.c (read_inode) that returns 0
		 * for [vdso].
		 */
		map.offset = 0;
		map.inode = 0;
	    }
	    else
	    {
		map.offset = offset;
		map.inode = inode;
	    }

	    map.bin_file = NULL;

	    g_array_append_val (result, map);
	}
    }

    g_free (name);
    fclose (in);

    if (n_maps)
	*n_maps = result->len;

    return (Map *)g_array_free (result, FALSE);
}

static void
free_maps (int *n_maps,
	   Map *maps)
{
    int i;

    for (i = 0; i < *n_maps; ++i)
    {
	Map *map = &(maps[i]);
	
	if (map->filename)
	    g_free (map->filename);
	
	if (map->bin_file)
	    bin_file_free (map->bin_file);
    }

    g_free (maps);
    *n_maps = 0;
}

const guint8 *
process_get_vdso_bytes (gsize *length)
{
    static gboolean has_data;
    static const guint8 *bytes = NULL;
    static gsize n_bytes = 0;    

    if (!has_data)
    {
	Map *maps;
	int n_maps, i;

	maps = read_maps (getpid(), &n_maps);

	for (i = 0; i < n_maps; ++i)
	{
	    Map *map = &(maps[i]);

	    if (strcmp (map->filename, "[vdso]") == 0)
	    {
		n_bytes = map->end - map->start;

		/* Dup the memory here so that valgrind will only
		 * report one 1 byte invalid read instead of
		 * a ton when the elf parser scans the vdso
		 *
		 * The reason we get a spurious invalid read from
		 * valgrind is that we are getting the address directly
		 * from /proc/maps, and valgrind knows that its mmap()
		 * wrapper never returned that address. But since it
		 * is a legal mapping, it is legal to read it.
		 */
		bytes = g_memdup ((guint8 *)map->start, n_bytes);
	    }
	}
	
	has_data = TRUE;
	free_maps (&n_maps, maps);
    }

    if (length)
	*length = n_bytes;

    return bytes;
}

static Process *
create_process (const char *cmdline, int pid)
{
    Process *p;
    
    p = g_new0 (Process, 1);
    
    if (*cmdline != '\0')
	p->cmdline = g_strdup_printf ("[%s]", cmdline);
    else
	p->cmdline = g_strdup_printf ("[pid %d]", pid);
    
    p->bad_pages = NULL;
    p->n_maps = 0;
    p->maps = NULL;
    p->pid = pid;
    p->undefined = NULL;
    
    g_assert (!g_hash_table_lookup (processes_by_pid, GINT_TO_POINTER (pid)));
    
    g_hash_table_insert (processes_by_pid, GINT_TO_POINTER (pid), p);
    
    return p;
}

static Map *
process_locate_map (Process *process, gulong addr)
{
    int i;

    for (i = 0; i < process->n_maps; ++i)
    {
	Map *map = &(process->maps[i]);

	if ((addr >= map->start) &&
	    (addr < map->end))
	{
	    if (i > 4)
	    {
		/* FIXME: Is this move-to-front really worth it? */
		Map tmp = *map;

		memmove (process->maps + 1, process->maps, i * sizeof (Map));

		*(process->maps) = tmp;

		map = process->maps;
	    }
	    
	    return map;
	}
    }
    
    return NULL;
}

static void
free_process (gpointer key, gpointer value, gpointer data)
{
    Process *process = value;
    
    free_maps (&(process->n_maps), process->maps);

    g_free (process->undefined);
    g_free (process->cmdline);
    g_list_free (process->bad_pages);
    
    g_free (process);
}

void
process_flush_caches  (void)
{
    if (!processes_by_pid)
	return;
    
    g_hash_table_foreach (processes_by_pid, free_process, NULL);
    g_hash_table_destroy (processes_by_pid);
    
    processes_by_pid = NULL;
}


static gboolean
process_has_page (Process *process, gulong addr)
{
    if (process_locate_map (process, addr))
	return TRUE;
    else
	return FALSE;
}

static int
page_size (void)
{
    static int page_size;
    static gboolean has_page_size = FALSE;

    if (!has_page_size)
    {
	page_size = getpagesize();
	has_page_size = TRUE;
    }

    return page_size;
}

void
process_ensure_map (Process *process, int pid, gulong addr)
{
    /* Round down to closest page */
    
    addr = (addr - addr % page_size());
    
    if (process_has_page (process, addr))
	return;
    
    if (g_list_find (process->bad_pages, (gpointer)addr))
	return;
    
    /* a map containing addr was not found */
    if (process->maps)
	free_maps (&(process->n_maps), process->maps);
    
    process->maps = read_maps (pid, &(process->n_maps));

    if (!process_has_page (process, addr))
    {
#if 0
	g_print ("Bad page: %p\n", addr);
#endif
	process->bad_pages = g_list_prepend (process->bad_pages, (gpointer)addr);
    }
}

static gboolean
do_idle_free (gpointer d)
{
    g_free (d);
    return FALSE;
}

static char *
idle_free (char *d)
{
    g_idle_add (do_idle_free, d);
    return d;
}

static char *
get_cmdline (int pid)
{
    char *cmdline;
    char *filename = idle_free (g_strdup_printf ("/proc/%d/cmdline", pid));
    
    if (g_file_get_contents (filename, &cmdline, NULL, NULL))
    {
	if (*cmdline == '\0')
	{
	    g_free (cmdline);
	    return NULL;
 	}
	return cmdline;
    }
    
    return NULL;
}

static char *
get_statname (int pid)
{
    char *stat;
    char *filename = idle_free (g_strdup_printf ("/proc/%d/stat", pid));
    
    if (g_file_get_contents (filename, &stat, NULL, NULL))
    {
	char result[200];
	
	idle_free (stat);
	
	if (sscanf (stat, "%*d %200s %*s", result) == 1)
	    return g_strndup (result, 200);
    }
    
    return NULL;
}

static char *
get_pidname (int pid)
{
    if (pid == -1)
	return g_strdup_printf ("kernel");
    else
	return g_strdup_printf ("pid %d", pid);
}

static char *
get_name (int pid)
{
    char *cmdline = NULL;
    
    if ((cmdline = get_cmdline (pid)))
	return cmdline;
    
    if ((cmdline = get_statname (pid)))
	return cmdline;
    
    return get_pidname (pid);
}

Process *
process_get_from_pid (int pid)
{
    Process *p;
    
    initialize();
    
    p = g_hash_table_lookup (processes_by_pid, GINT_TO_POINTER (pid));
    
    if (!p)
	p = create_process (idle_free (get_name (pid)), pid);
    
    return p;
}

static gboolean
file_exists (const char *name)
{
    int fd;
    fd = open (name, O_RDONLY);
    
    if (fd > 0)
    {
	close (fd);
	return TRUE;
    }
    return FALSE;
}

static gchar *
look_for_vmlinux (void)
{
    struct utsname utsname;
    char *result;
    char **s;
    char *names[4];

    uname (&utsname);

    names[0] = g_strdup_printf (
	"/usr/lib/debug/lib/modules/%s/vmlinux", utsname.release);
    names[1] = g_strdup_printf (
	"/lib/modules/%s/source/vmlinux", utsname.release);
    names[2] = g_strdup_printf (
	"/boot/vmlinux-%s", utsname.release);
    names[3] = NULL;

    result = NULL;
    for (s = names; *s; s++)
    {
	if (file_exists (*s))
	{
	    result = g_strdup (*s);
	    break;
	}
    }

    for (s = names; *s; s++)
	g_free (*s);

    return result;
}

static const gchar *
find_kernel_binary (void)
{
    static gboolean looked_for_vmlinux;
    static gchar *binary = NULL;
    
    if (!looked_for_vmlinux)
    {
	binary = look_for_vmlinux ();
	looked_for_vmlinux = TRUE;
    }
    
    return binary;
}

typedef struct
{
    gulong	 address;
    char	*name;
} KernelSymbol;

static void
parse_kallsym_line (const char *line,
		    GArray *table)
{
    char **tokens = g_strsplit_set (line, " \t", -1);

    if (tokens[0] && tokens[1] && tokens[2])
    {
	glong address;
	char *endptr;

	address = strtoul (tokens[0], &endptr, 16);

	if (*endptr == '\0'			&&
	    (strcmp (tokens[1], "T") == 0	||
	     strcmp (tokens[1], "t") == 0))
	{
	    KernelSymbol sym;

	    sym.address = address;
	    sym.name = g_strdup (tokens[2]);

	    g_array_append_val (table, sym);
	}
    }

    g_strfreev (tokens);
}

static gboolean
parse_kallsyms (const char *kallsyms,
		GArray     *table)
{
    const char *sol;
    const char *eol;
    
    sol = kallsyms;
    eol = strchr (sol, '\n');
    while (eol)
    {
	char *line = g_strndup (sol, eol - sol);
	
	parse_kallsym_line (line, table);
	
	g_free (line);
	
	sol = eol + 1;
	eol = strchr (sol, '\n');
    }

    if (table->len <= 1)
	return FALSE;
    
    return TRUE;
}

static int
compare_syms (gconstpointer a, gconstpointer b)
{
    const KernelSymbol *sym_a = a;
    const KernelSymbol *sym_b = b;

    if (sym_a->address > sym_b->address)
	return 1;
    else if (sym_a->address == sym_b->address)
	return 0;
    else
	return -1;
}

static GArray *
get_kernel_symbols (void)
{
    static GArray *kernel_syms;
    static gboolean initialized = FALSE;

    find_kernel_binary();
    
    if (!initialized)
    {
	char *kallsyms;
	if (g_file_get_contents ("/proc/kallsyms", &kallsyms, NULL, NULL))
	{
	    if (kallsyms)
	    {
		kernel_syms = g_array_new (TRUE, TRUE, sizeof (KernelSymbol));
		
		if (parse_kallsyms (kallsyms, kernel_syms))
		{
		    g_array_sort (kernel_syms, compare_syms);
		}
		else
		{
		    g_array_free (kernel_syms, TRUE);
		    kernel_syms = NULL;
		}
	    }
	}

	if (!kernel_syms)
	    g_print ("Warning: /proc/kallsyms could not be "
		     "read. Kernel symbols will not be available\n");
	
	initialized = TRUE;
    }

    return kernel_syms;
}

gboolean
process_is_kernel_address (gulong address)
{
    GArray *ksyms = get_kernel_symbols ();

    if (ksyms &&
	address >= g_array_index (ksyms, KernelSymbol, 0).address	&&
	address <  g_array_index (ksyms, KernelSymbol, ksyms->len - 1).address)
    {
	return TRUE;
    }

    return FALSE;
}

static KernelSymbol *
do_lookup (KernelSymbol *symbols,
	   gulong        address,
	   int           first,
	   int           last)
{
    if (address >= symbols[last].address)
    {
	return &(symbols[last]);
    }
    else if (last - first < 3)
    {
	while (last >= first)
	{
	    if (address >= symbols[last].address)
		return &(symbols[last]);
	    
	    last--;
	}
	
	return NULL;
    }
    else
    {
	int mid = (first + last) / 2;
	
	if (symbols[mid].address > address)
	    return do_lookup (symbols, address, first, mid);
	else
	    return do_lookup (symbols, address, mid, last);
    }
}

const char *
process_lookup_kernel_symbol (gulong address,
			      gulong *offset)
{
    GArray *ksyms = get_kernel_symbols ();
    KernelSymbol *result;

    if (offset)
    {
	/* If we don't have any offset, just return 1, so it doesn't
	 * look like a callback
	 */
	*offset = 1;
    }
    
    if (ksyms->len == 0)
	return NULL;
    
    result = do_lookup ((KernelSymbol *)ksyms->data, address, 0, ksyms->len - 1);
    if (result && offset)
	*offset = address - result->address;

    return result? result->name : NULL;
}

const char *
process_lookup_symbol (Process *process, gulong address, gulong *offset)
{
    static const char *const kernel = "kernel";
    const BinSymbol *result;
    Map *map = process_locate_map (process, address);
    
/*     g_print ("addr: %x\n", address); */

    if (offset)
    {
	/* If we don't have any offset, just return 1, so it doesn't
	 * look like a callback
	 */
	*offset = 1;
    }
	
    if (address == 0x1)
    {
	return kernel;
    }
    else if (!map)
    {
	if (!process->undefined)
	{
	    process->undefined =
		g_strdup_printf ("No map (%s)", process->cmdline);
	}

	return process->undefined;
    }

#if 0
    if (strcmp (map->filename, "/home/ssp/sysprof/sysprof") == 0)
    {
	g_print ("YES\n");

	g_print ("map address: %lx\n", map->start);
	g_print ("map offset: %lx\n", map->offset);
	g_print ("address before: %lx  (%s)\n", address, map->filename);
    }

    g_print ("address before: \n");
#endif

#if 0
    g_print ("%s is mapped at %lx + %lx\n", map->filename, map->start, map->offset);
    g_print ("incoming address: %lx\n", address);
#endif
    
    address -= map->start;
    address += map->offset;

#if 0
    if (strcmp (map->filename, "[vdso]") == 0)
    {
	g_print ("address after: %lx\n", address);
    }
#endif
    
    if (!map->bin_file)
	map->bin_file = bin_file_new (map->filename);

/*     g_print ("%s: start: %p, load: %p\n", */
/* 	     map->filename, map->start, bin_file_get_load_address (map->bin_file)); */

    if (!bin_file_check_inode (map->bin_file, map->inode))
    {
	/* If the inodes don't match, it's probably because the
	 * file has changed since the process was started. Just return
	 * the undefined symbol in that case.
	 */
	address = 0x0;
    }

    result = bin_file_lookup_symbol (map->bin_file, address);
    
#if 0
    g_print (" ---> %s\n", result->name);
#endif
    
/*     g_print ("(%x) %x %x name; %s\n", address, map->start, map->offset, result->name); */

#if 0
    g_print ("name: %s (in %s)\n", bin_symbol_get_name (map->bin_file, result), map->filename);
    g_print ("  in addr: %lx\n", address);
    g_print ("  out addr: %lx\n", bin_symbol_get_address (map->bin_file, result));
    g_print ("  map start: %lx\n", map->start);
    g_print ("  map offset: %lx\n", map->offset);
#endif

    if (offset)
	*offset = address - bin_symbol_get_address (map->bin_file, result);
    
    return bin_symbol_get_name (map->bin_file, result);
}

const char *
process_get_cmdline (Process *process)
{
    return process->cmdline;
}