proc.c revision 642626096a694c6af279d25d2b1b2fba5b10ddfb
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Zachary T Welch, CodeSourcery
5 * Copyright (C) 2010 Joe Damato
6 * Copyright (C) 1998,2008,2009 Juan Cespedes
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24#define _GNU_SOURCE /* For getline.  */
25#include "config.h"
26
27#include <sys/stat.h>
28#include <sys/syscall.h>
29#include <sys/types.h>
30#include <ctype.h>
31#include <dirent.h>
32#include <errno.h>
33#include <fcntl.h>
34#include <inttypes.h>
35#include <link.h>
36#include <signal.h>
37#include <stdio.h>
38#include <string.h>
39#include <unistd.h>
40
41#include "config.h"
42#include "breakpoint.h"
43#include "proc.h"
44#include "library.h"
45#include "backend.h"
46#include "events.h"
47
48/* /proc/pid doesn't exist just after the fork, and sometimes `ltrace'
49 * couldn't open it to find the executable.  So it may be necessary to
50 * have a bit delay
51 */
52
53#define	MAX_DELAY	100000	/* 100000 microseconds = 0.1 seconds */
54
55#define PROC_PID_FILE(VAR, FORMAT, PID)		\
56	char VAR[strlen(FORMAT) + 6];		\
57	sprintf(VAR, FORMAT, PID)
58
59/*
60 * Returns a (malloc'd) file name corresponding to a running pid
61 */
62char *
63pid2name(pid_t pid) {
64	if (!kill(pid, 0)) {
65		int delay = 0;
66
67		PROC_PID_FILE(proc_exe, "/proc/%d/exe", pid);
68
69		while (delay < MAX_DELAY) {
70			if (!access(proc_exe, F_OK)) {
71				return strdup(proc_exe);
72			}
73			delay += 1000;	/* 1 milisecond */
74		}
75	}
76	return NULL;
77}
78
79static FILE *
80open_status_file(pid_t pid)
81{
82	PROC_PID_FILE(fn, "/proc/%d/status", pid);
83	/* Don't complain if we fail.  This would typically happen
84	   when the process is about to terminate, and these files are
85	   not available anymore.  This function is called from the
86	   event loop, and we don't want to clutter the output just
87	   because the process terminates.  */
88	return fopen(fn, "r");
89}
90
91static char *
92find_line_starting(FILE * file, const char * prefix, size_t len)
93{
94	char * line = NULL;
95	size_t line_len = 0;
96	while (!feof(file)) {
97		if (getline(&line, &line_len, file) < 0)
98			return NULL;
99		if (strncmp(line, prefix, len) == 0)
100			return line;
101	}
102	return NULL;
103}
104
105static void
106each_line_starting(FILE *file, const char *prefix,
107		   enum callback_status (*cb)(const char *line,
108					      const char *prefix,
109					      void *data),
110		   void *data)
111{
112	size_t len = strlen(prefix);
113	char * line;
114	while ((line = find_line_starting(file, prefix, len)) != NULL) {
115		enum callback_status st = (*cb)(line, prefix, data);
116		free (line);
117		if (st == CBS_STOP)
118			return;
119	}
120}
121
122static enum callback_status
123process_leader_cb(const char *line, const char *prefix, void *data)
124{
125	pid_t * pidp = data;
126	*pidp = atoi(line + strlen(prefix));
127	return CBS_STOP;
128}
129
130pid_t
131process_leader(pid_t pid)
132{
133	pid_t tgid = 0;
134	FILE * file = open_status_file(pid);
135	if (file != NULL) {
136		each_line_starting(file, "Tgid:\t", &process_leader_cb, &tgid);
137		fclose(file);
138	}
139
140	return tgid;
141}
142
143static enum callback_status
144process_stopped_cb(const char *line, const char *prefix, void *data)
145{
146	char c = line[strlen(prefix)];
147	// t:tracing stop, T:job control stop
148	*(int *)data = (c == 't' || c == 'T');
149	return CBS_STOP;
150}
151
152int
153process_stopped(pid_t pid)
154{
155	int is_stopped = -1;
156	FILE * file = open_status_file(pid);
157	if (file != NULL) {
158		each_line_starting(file, "State:\t", &process_stopped_cb,
159				   &is_stopped);
160		fclose(file);
161	}
162	return is_stopped;
163}
164
165static enum callback_status
166process_status_cb(const char *line, const char *prefix, void *data)
167{
168	const char * status = line + strlen(prefix);
169	const char c = *status;
170
171#define RETURN(C) do {					\
172		*(enum process_status *)data = C;	\
173		return CBS_STOP;			\
174	} while (0)
175
176	switch (c) {
177	case 'Z': RETURN(ps_zombie);
178	case 't': RETURN(ps_tracing_stop);
179	case 'T':
180		/* This can be either "T (stopped)" or, for older
181		 * kernels, "T (tracing stop)".  */
182		if (!strcmp(status, "T (stopped)\n"))
183			RETURN(ps_stop);
184		else if (!strcmp(status, "T (tracing stop)\n"))
185			RETURN(ps_tracing_stop);
186		else {
187			fprintf(stderr, "Unknown process status: %s",
188				status);
189			RETURN(ps_stop); /* Some sort of stop
190					  * anyway.  */
191		}
192	case 'D':
193	case 'S': RETURN(ps_sleeping);
194	}
195
196	RETURN(ps_other);
197#undef RETURN
198}
199
200enum process_status
201process_status(pid_t pid)
202{
203	enum process_status ret = ps_invalid;
204	FILE * file = open_status_file(pid);
205	if (file != NULL) {
206		each_line_starting(file, "State:\t", &process_status_cb, &ret);
207		fclose(file);
208		if (ret == ps_invalid)
209			fprintf(stderr, "process_status %d: %s", pid,
210				strerror(errno));
211	} else
212		/* If the file is not present, the process presumably
213		 * exited already.  */
214		ret = ps_zombie;
215
216	return ret;
217}
218
219static int
220all_digits(const char *str)
221{
222	while (isdigit(*str))
223		str++;
224	return !*str;
225}
226
227int
228process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n)
229{
230	PROC_PID_FILE(fn, "/proc/%d/task", pid);
231	DIR * d = opendir(fn);
232	if (d == NULL)
233		return -1;
234
235	pid_t *tasks = NULL;
236	size_t n = 0;
237	size_t alloc = 0;
238
239	while (1) {
240		struct dirent entry;
241		struct dirent *result;
242		if (readdir_r(d, &entry, &result) != 0) {
243			free(tasks);
244			return -1;
245		}
246		if (result == NULL)
247			break;
248		if (result->d_type == DT_DIR && all_digits(result->d_name)) {
249			pid_t npid = atoi(result->d_name);
250			if (n >= alloc) {
251				alloc = alloc > 0 ? (2 * alloc) : 8;
252				pid_t *ntasks = realloc(tasks,
253							sizeof(*tasks) * alloc);
254				if (ntasks == NULL) {
255					free(tasks);
256					return -1;
257				}
258				tasks = ntasks;
259			}
260			if (n >= alloc)
261				abort();
262			tasks[n++] = npid;
263		}
264	}
265
266	closedir(d);
267
268	*ret_tasks = tasks;
269	*ret_n = n;
270	return 0;
271}
272
273/* On native 64-bit system, we need to be careful when handling cross
274 * tracing.  This select appropriate pointer depending on host and
275 * target architectures.  XXX Really we should abstract this into the
276 * ABI object, as theorized about somewhere on pmachata/revamp
277 * branch.  */
278static void *
279select_32_64(struct Process *proc, void *p32, void *p64)
280{
281	if (sizeof(long) == 4 || proc->mask_32bit)
282		return p32;
283	else
284		return p64;
285}
286
287static int
288fetch_dyn64(struct Process *proc, target_address_t *addr, Elf64_Dyn *ret)
289{
290	if (umovebytes(proc, *addr, ret, sizeof(*ret)) != sizeof(*ret))
291		return -1;
292	*addr += sizeof(*ret);
293	return 0;
294}
295
296static int
297fetch_dyn32(struct Process *proc, target_address_t *addr, Elf64_Dyn *ret)
298{
299	Elf32_Dyn dyn;
300	if (umovebytes(proc, *addr, &dyn, sizeof(dyn)) != sizeof(dyn))
301		return -1;
302
303	*addr += sizeof(dyn);
304	ret->d_tag = dyn.d_tag;
305	ret->d_un.d_val = dyn.d_un.d_val;
306
307	return 0;
308}
309
310static int (*
311dyn_fetcher(struct Process *proc))(struct Process *,
312				   target_address_t *, Elf64_Dyn *)
313{
314	return select_32_64(proc, fetch_dyn32, fetch_dyn64);
315}
316
317static int
318find_dynamic_entry_addr(struct Process *proc, target_address_t src_addr,
319			int d_tag, target_address_t *ret)
320{
321	debug(DEBUG_FUNCTION, "find_dynamic_entry()");
322
323	if (ret == NULL || src_addr == 0 || d_tag < 0 || d_tag > DT_NUM)
324		return -1;
325
326	int i = 0;
327	while (1) {
328		Elf64_Dyn entry;
329		if (dyn_fetcher(proc)(proc, &src_addr, &entry) < 0
330		    || entry.d_tag == DT_NULL
331		    || i++ > 100) { /* Arbitrary cut-off so that we
332				     * don't loop forever if the
333				     * binary is corrupted.  */
334			debug(2, "Couldn't find address for dtag!");
335			return -1;
336		}
337
338		if (entry.d_tag == d_tag) {
339			/* XXX The double cast should be removed when
340			 * target_address_t becomes integral type.  */
341			*ret = (target_address_t)(uintptr_t)entry.d_un.d_val;
342			debug(2, "found address: %p in dtag %d", *ret, d_tag);
343			return 0;
344		}
345	}
346}
347
348/* Our own type for representing 32-bit linkmap.  We can't rely on the
349 * definition in link.h, because that's only accurate for our host
350 * architecture, not for target architecture (where the traced process
351 * runs). */
352#define LT_LINK_MAP(BITS)			\
353	{					\
354		Elf##BITS##_Addr l_addr;	\
355		Elf##BITS##_Addr l_name;	\
356		Elf##BITS##_Addr l_ld;		\
357		Elf##BITS##_Addr l_next;	\
358		Elf##BITS##_Addr l_prev;	\
359	}
360struct lt_link_map_32 LT_LINK_MAP(32);
361struct lt_link_map_64 LT_LINK_MAP(64);
362
363static int
364fetch_lm64(struct Process *proc, target_address_t addr,
365	   struct lt_link_map_64 *ret)
366{
367	if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
368		return -1;
369	return 0;
370}
371
372static int
373fetch_lm32(struct Process *proc, target_address_t addr,
374	   struct lt_link_map_64 *ret)
375{
376	struct lt_link_map_32 lm;
377	if (umovebytes(proc, addr, &lm, sizeof(lm)) != sizeof(lm))
378		return -1;
379
380	ret->l_addr = lm.l_addr;
381	ret->l_name = lm.l_name;
382	ret->l_ld = lm.l_ld;
383	ret->l_next = lm.l_next;
384	ret->l_prev = lm.l_prev;
385
386	return 0;
387}
388
389static int (*
390lm_fetcher(struct Process *proc))(struct Process *,
391				  target_address_t, struct lt_link_map_64 *)
392{
393	return select_32_64(proc, fetch_lm32, fetch_lm64);
394}
395
396/* The same as above holds for struct r_debug.  */
397#define LT_R_DEBUG(BITS)			\
398	{					\
399		int r_version;			\
400		Elf##BITS##_Addr r_map;		\
401		Elf##BITS##_Addr r_brk;		\
402		int r_state;			\
403		Elf##BITS##_Addr r_ldbase;	\
404	}
405
406struct lt_r_debug_32 LT_R_DEBUG(32);
407struct lt_r_debug_64 LT_R_DEBUG(64);
408
409static int
410fetch_rd64(struct Process *proc, target_address_t addr,
411	   struct lt_r_debug_64 *ret)
412{
413	if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
414		return -1;
415	return 0;
416}
417
418static int
419fetch_rd32(struct Process *proc, target_address_t addr,
420	   struct lt_r_debug_64 *ret)
421{
422	struct lt_r_debug_32 rd;
423	if (umovebytes(proc, addr, &rd, sizeof(rd)) != sizeof(rd))
424		return -1;
425
426	ret->r_version = rd.r_version;
427	ret->r_map = rd.r_map;
428	ret->r_brk = rd.r_brk;
429	ret->r_state = rd.r_state;
430	ret->r_ldbase = rd.r_ldbase;
431
432	return 0;
433}
434
435static int (*
436rdebug_fetcher(struct Process *proc))(struct Process *,
437				      target_address_t, struct lt_r_debug_64 *)
438{
439	return select_32_64(proc, fetch_rd32, fetch_rd64);
440}
441
442static void
443crawl_linkmap(struct Process *proc, struct lt_r_debug_64 *dbg)
444{
445	debug (DEBUG_FUNCTION, "crawl_linkmap()");
446
447	if (!dbg || !dbg->r_map) {
448		debug(2, "Debug structure or it's linkmap are NULL!");
449		return;
450	}
451
452	/* XXX The double cast should be removed when
453	 * target_address_t becomes integral type.  */
454	target_address_t addr = (target_address_t)(uintptr_t)dbg->r_map;
455
456	while (addr != 0) {
457		struct lt_link_map_64 rlm;
458		if (lm_fetcher(proc)(proc, addr, &rlm) < 0) {
459			debug(2, "Unable to read link map");
460			return;
461		}
462
463		target_address_t key = addr;
464		/* XXX The double cast should be removed when
465		 * target_address_t becomes integral type.  */
466		addr = (target_address_t)(uintptr_t)rlm.l_next;
467		if (rlm.l_name == 0) {
468			debug(2, "Name of mapped library is NULL");
469			return;
470		}
471
472		char lib_name[BUFSIZ];
473		/* XXX The double cast should be removed when
474		 * target_address_t becomes integral type.  */
475		umovebytes(proc, (target_address_t)(uintptr_t)rlm.l_name,
476			   lib_name, sizeof(lib_name));
477
478		if (*lib_name == '\0') {
479			/* VDSO.  No associated file, XXX but we might
480			 * load it from the address space of the
481			 * process.  */
482			continue;
483		}
484
485		/* Do we have that library already?  */
486		if (proc_each_library(proc, NULL, library_with_key_cb, &key))
487			continue;
488
489		struct library *lib = malloc(sizeof(*lib));
490		if (lib == NULL) {
491		fail:
492			if (lib != NULL)
493				library_destroy(lib);
494			fprintf(stderr, "Couldn't load ELF object %s: %s\n",
495				lib_name, strerror(errno));
496			continue;
497		}
498		library_init(lib, LT_LIBTYPE_DSO);
499
500		if (ltelf_read_library(lib, proc, lib_name, rlm.l_addr) < 0)
501			goto fail;
502
503		lib->key = key;
504		proc_add_library(proc, lib);
505	}
506	return;
507}
508
509/* A struct stored at proc->debug.  */
510struct debug_struct
511{
512	target_address_t debug_addr;
513	int state;
514};
515
516static int
517load_debug_struct(struct Process *proc, struct lt_r_debug_64 *ret)
518{
519	debug(DEBUG_FUNCTION, "load_debug_struct");
520
521	struct debug_struct *debug = proc->debug;
522
523	if (rdebug_fetcher(proc)(proc, debug->debug_addr, ret) < 0) {
524		debug(2, "This process does not have a debug structure!\n");
525		return -1;
526	}
527
528	return 0;
529}
530
531static void
532rdebug_bp_on_hit(struct breakpoint *bp, struct Process *proc)
533{
534	debug(DEBUG_FUNCTION, "arch_check_dbg");
535
536	struct lt_r_debug_64 rdbg;
537	if (load_debug_struct(proc, &rdbg) < 0) {
538		debug(2, "Unable to load debug structure!");
539		return;
540	}
541
542	struct debug_struct *debug = proc->debug;
543	if (rdbg.r_state == RT_CONSISTENT) {
544		debug(2, "Linkmap is now consistent");
545		if (debug->state == RT_ADD) {
546			debug(2, "Adding DSO to linkmap");
547			//data.proc = proc;
548			crawl_linkmap(proc, &rdbg);
549			//&data);
550		} else if (debug->state == RT_DELETE) {
551			debug(2, "Removing DSO from linkmap");
552		} else {
553			debug(2, "Unexpected debug state!");
554		}
555	}
556
557	debug->state = rdbg.r_state;
558}
559
560int
561linkmap_init(struct Process *proc, target_address_t dyn_addr)
562{
563	debug(DEBUG_FUNCTION, "linkmap_init()");
564
565	struct debug_struct *debug = malloc(sizeof(*debug));
566	if (debug == NULL) {
567		fprintf(stderr, "couldn't allocate debug struct: %s\n",
568			strerror(errno));
569	fail:
570		proc->debug = NULL;
571		free(debug);
572		return -1;
573	}
574	proc->debug = debug;
575
576	if (find_dynamic_entry_addr(proc, dyn_addr, DT_DEBUG,
577				    &debug->debug_addr) == -1) {
578		debug(2, "Couldn't find debug structure!");
579		goto fail;
580	}
581
582	int status;
583	struct lt_r_debug_64 rdbg;
584	if ((status = load_debug_struct(proc, &rdbg)) < 0) {
585		debug(2, "No debug structure or no memory to allocate one!");
586		return status;
587	}
588
589	/* XXX The double cast should be removed when
590	 * target_address_t becomes integral type.  */
591	target_address_t addr = (target_address_t)(uintptr_t)rdbg.r_brk;
592	if (arch_translate_address_dyn(proc, addr, &addr) < 0)
593		goto fail;
594
595	struct breakpoint *rdebug_bp = insert_breakpoint(proc, addr, NULL);
596	static struct bp_callbacks rdebug_callbacks = {
597		.on_hit = rdebug_bp_on_hit,
598	};
599	rdebug_bp->cbs = &rdebug_callbacks;
600
601	crawl_linkmap(proc, &rdbg);
602
603	return 0;
604}
605
606int
607task_kill (pid_t pid, int sig)
608{
609	// Taken from GDB
610        int ret;
611
612        errno = 0;
613        ret = syscall (__NR_tkill, pid, sig);
614	return ret;
615}
616
617void
618process_removed(struct Process *proc)
619{
620	delete_events_for(proc);
621}
622