blob: 953fd862f94168d855dfa9e4574651cf1cbfae98 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
Petr Machata4e553c02013-01-07 17:16:58 +01003 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
Petr Machata64262602012-01-07 03:41:36 +01004 * 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
Petr Machata750ca8c2011-10-06 14:29:34 +020024#define _GNU_SOURCE /* For getline. */
Juan Cespedesd44c6b81998-09-25 14:48:42 +020025#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +020026
Petr Machata9a5420c2011-07-09 11:21:23 +020027#include <sys/stat.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +020028#include <sys/syscall.h>
29#include <sys/types.h>
30#include <ctype.h>
31#include <dirent.h>
32#include <errno.h>
Petr Machata9a5420c2011-07-09 11:21:23 +020033#include <fcntl.h>
Zachary T Welchbfb26c72010-12-06 23:21:00 -080034#include <inttypes.h>
Joe Damato47cae1e2010-11-08 15:47:39 -080035#include <link.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +020036#include <signal.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +010037#include <stdio.h>
Petr Machataba1664b2012-04-28 14:59:05 +020038#include <stdlib.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +010039#include <string.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +010040#include <unistd.h>
Petr Machata9a5420c2011-07-09 11:21:23 +020041
Petr Machata64262602012-01-07 03:41:36 +010042#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020043#include "breakpoint.h"
44#include "config.h"
45#include "debug.h"
Petr Machatacd972582012-01-07 03:02:07 +010046#include "events.h"
Petr Machataba1664b2012-04-28 14:59:05 +020047#include "library.h"
48#include "ltrace-elf.h"
49#include "proc.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010050
51/* /proc/pid doesn't exist just after the fork, and sometimes `ltrace'
52 * couldn't open it to find the executable. So it may be necessary to
53 * have a bit delay
54 */
55
Ian Wienand2d45b1a2006-02-20 22:48:07 +010056#define MAX_DELAY 100000 /* 100000 microseconds = 0.1 seconds */
Juan Cespedes1fe93d51998-03-13 00:29:21 +010057
Petr Machata9a5420c2011-07-09 11:21:23 +020058#define PROC_PID_FILE(VAR, FORMAT, PID) \
59 char VAR[strlen(FORMAT) + 6]; \
60 sprintf(VAR, FORMAT, PID)
61
Juan Cespedes1fe93d51998-03-13 00:29:21 +010062/*
Juan Cespedese0660df2009-05-21 18:14:39 +020063 * Returns a (malloc'd) file name corresponding to a running pid
Juan Cespedes1fe93d51998-03-13 00:29:21 +010064 */
Juan Cespedesf1350522008-12-16 18:19:58 +010065char *
66pid2name(pid_t pid) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +010067 if (!kill(pid, 0)) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010068 int delay = 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010069
Petr Machata9a5420c2011-07-09 11:21:23 +020070 PROC_PID_FILE(proc_exe, "/proc/%d/exe", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010071
Ian Wienand2d45b1a2006-02-20 22:48:07 +010072 while (delay < MAX_DELAY) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010073 if (!access(proc_exe, F_OK)) {
74 return strdup(proc_exe);
75 }
76 delay += 1000; /* 1 milisecond */
77 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +010078 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010079 return NULL;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010080}
Joe Damato47cae1e2010-11-08 15:47:39 -080081
Petr Machata9a5420c2011-07-09 11:21:23 +020082static FILE *
83open_status_file(pid_t pid)
84{
85 PROC_PID_FILE(fn, "/proc/%d/status", pid);
86 /* Don't complain if we fail. This would typically happen
87 when the process is about to terminate, and these files are
88 not available anymore. This function is called from the
89 event loop, and we don't want to clutter the output just
90 because the process terminates. */
91 return fopen(fn, "r");
92}
93
94static char *
95find_line_starting(FILE * file, const char * prefix, size_t len)
96{
97 char * line = NULL;
98 size_t line_len = 0;
99 while (!feof(file)) {
100 if (getline(&line, &line_len, file) < 0)
101 return NULL;
102 if (strncmp(line, prefix, len) == 0)
103 return line;
104 }
105 return NULL;
106}
107
108static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100109each_line_starting(FILE *file, const char *prefix,
110 enum callback_status (*cb)(const char *line,
111 const char *prefix,
112 void *data),
113 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200114{
115 size_t len = strlen(prefix);
116 char * line;
117 while ((line = find_line_starting(file, prefix, len)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100118 enum callback_status st = (*cb)(line, prefix, data);
Petr Machataba1664b2012-04-28 14:59:05 +0200119 free(line);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100120 if (st == CBS_STOP)
Petr Machata9a5420c2011-07-09 11:21:23 +0200121 return;
122 }
123}
124
Petr Machata2b46cfc2012-02-18 11:17:29 +0100125static enum callback_status
126process_leader_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200127{
128 pid_t * pidp = data;
129 *pidp = atoi(line + strlen(prefix));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100130 return CBS_STOP;
Petr Machata9a5420c2011-07-09 11:21:23 +0200131}
132
133pid_t
134process_leader(pid_t pid)
135{
Petr Machata1974dbc2011-08-19 18:58:01 +0200136 pid_t tgid = 0;
Petr Machata9a5420c2011-07-09 11:21:23 +0200137 FILE * file = open_status_file(pid);
138 if (file != NULL) {
139 each_line_starting(file, "Tgid:\t", &process_leader_cb, &tgid);
140 fclose(file);
141 }
142
143 return tgid;
144}
145
Petr Machata2b46cfc2012-02-18 11:17:29 +0100146static enum callback_status
147process_stopped_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200148{
149 char c = line[strlen(prefix)];
150 // t:tracing stop, T:job control stop
151 *(int *)data = (c == 't' || c == 'T');
Petr Machata2b46cfc2012-02-18 11:17:29 +0100152 return CBS_STOP;
Petr Machata9a5420c2011-07-09 11:21:23 +0200153}
154
155int
156process_stopped(pid_t pid)
157{
158 int is_stopped = -1;
159 FILE * file = open_status_file(pid);
160 if (file != NULL) {
161 each_line_starting(file, "State:\t", &process_stopped_cb,
162 &is_stopped);
163 fclose(file);
164 }
165 return is_stopped;
166}
167
Petr Machata2b46cfc2012-02-18 11:17:29 +0100168static enum callback_status
169process_status_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200170{
Petr Machata617ff0b2011-10-06 14:23:24 +0200171 const char * status = line + strlen(prefix);
172 const char c = *status;
173
174#define RETURN(C) do { \
175 *(enum process_status *)data = C; \
Petr Machata2b46cfc2012-02-18 11:17:29 +0100176 return CBS_STOP; \
Petr Machata617ff0b2011-10-06 14:23:24 +0200177 } while (0)
178
179 switch (c) {
Petr Machata6dfc5442012-12-17 03:38:36 +0100180 case 'Z': RETURN(PS_ZOMBIE);
181 case 't': RETURN(PS_TRACING_STOP);
Petr Machatacbe29c62011-09-27 02:27:58 +0200182 case 'T':
Petr Machata617ff0b2011-10-06 14:23:24 +0200183 /* This can be either "T (stopped)" or, for older
184 * kernels, "T (tracing stop)". */
185 if (!strcmp(status, "T (stopped)\n"))
Petr Machata6dfc5442012-12-17 03:38:36 +0100186 RETURN(PS_STOP);
Petr Machata617ff0b2011-10-06 14:23:24 +0200187 else if (!strcmp(status, "T (tracing stop)\n"))
Petr Machata6dfc5442012-12-17 03:38:36 +0100188 RETURN(PS_TRACING_STOP);
Petr Machata617ff0b2011-10-06 14:23:24 +0200189 else {
190 fprintf(stderr, "Unknown process status: %s",
191 status);
Petr Machata6dfc5442012-12-17 03:38:36 +0100192 RETURN(PS_STOP); /* Some sort of stop
Petr Machata617ff0b2011-10-06 14:23:24 +0200193 * anyway. */
194 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200195 case 'D':
Petr Machata6dfc5442012-12-17 03:38:36 +0100196 case 'S': RETURN(PS_SLEEPING);
Petr Machata617ff0b2011-10-06 14:23:24 +0200197 }
198
Petr Machata6dfc5442012-12-17 03:38:36 +0100199 RETURN(PS_OTHER);
Petr Machata617ff0b2011-10-06 14:23:24 +0200200#undef RETURN
Petr Machata9a5420c2011-07-09 11:21:23 +0200201}
202
Petr Machata617ff0b2011-10-06 14:23:24 +0200203enum process_status
Petr Machata9a5420c2011-07-09 11:21:23 +0200204process_status(pid_t pid)
205{
Petr Machata6dfc5442012-12-17 03:38:36 +0100206 enum process_status ret = PS_INVALID;
Petr Machata9a5420c2011-07-09 11:21:23 +0200207 FILE * file = open_status_file(pid);
208 if (file != NULL) {
209 each_line_starting(file, "State:\t", &process_status_cb, &ret);
210 fclose(file);
Petr Machata6dfc5442012-12-17 03:38:36 +0100211 if (ret == PS_INVALID)
Petr Machataaacb95e2013-01-07 18:12:19 +0100212 fprintf(stderr,
213 "Couldn't determine status of process %d: %s\n",
214 pid, strerror(errno));
Petr Machata6dfc5442012-12-17 03:38:36 +0100215 } else {
Petr Machata750ca8c2011-10-06 14:29:34 +0200216 /* If the file is not present, the process presumably
217 * exited already. */
Petr Machata6dfc5442012-12-17 03:38:36 +0100218 ret = PS_ZOMBIE;
219 }
Petr Machata750ca8c2011-10-06 14:29:34 +0200220
Petr Machata9a5420c2011-07-09 11:21:23 +0200221 return ret;
222}
223
224static int
225all_digits(const char *str)
226{
227 while (isdigit(*str))
228 str++;
229 return !*str;
230}
231
232int
233process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n)
234{
235 PROC_PID_FILE(fn, "/proc/%d/task", pid);
Petr Machata6bf1e6a2013-01-09 16:39:10 +0100236 DIR *d = opendir(fn);
Petr Machata9a5420c2011-07-09 11:21:23 +0200237 if (d == NULL)
238 return -1;
239
Petr Machata9a5420c2011-07-09 11:21:23 +0200240 pid_t *tasks = NULL;
241 size_t n = 0;
242 size_t alloc = 0;
243
244 while (1) {
245 struct dirent entry;
246 struct dirent *result;
247 if (readdir_r(d, &entry, &result) != 0) {
Petr Machata6bf1e6a2013-01-09 16:39:10 +0100248 fail:
Petr Machata9a5420c2011-07-09 11:21:23 +0200249 free(tasks);
Petr Machata6bf1e6a2013-01-09 16:39:10 +0100250 closedir(d);
Petr Machata9a5420c2011-07-09 11:21:23 +0200251 return -1;
252 }
253 if (result == NULL)
254 break;
255 if (result->d_type == DT_DIR && all_digits(result->d_name)) {
256 pid_t npid = atoi(result->d_name);
257 if (n >= alloc) {
258 alloc = alloc > 0 ? (2 * alloc) : 8;
259 pid_t *ntasks = realloc(tasks,
260 sizeof(*tasks) * alloc);
Petr Machata6bf1e6a2013-01-09 16:39:10 +0100261 if (ntasks == NULL)
262 goto fail;
Petr Machata9a5420c2011-07-09 11:21:23 +0200263 tasks = ntasks;
264 }
Petr Machata6bf1e6a2013-01-09 16:39:10 +0100265 assert(n < alloc);
Petr Machata9a5420c2011-07-09 11:21:23 +0200266 tasks[n++] = npid;
267 }
268 }
269
270 closedir(d);
271
272 *ret_tasks = tasks;
273 *ret_n = n;
274 return 0;
275}
276
Petr Machata6a7997d2012-03-29 18:37:15 +0200277/* On native 64-bit system, we need to be careful when handling cross
278 * tracing. This select appropriate pointer depending on host and
279 * target architectures. XXX Really we should abstract this into the
280 * ABI object, as theorized about somewhere on pmachata/revamp
281 * branch. */
282static void *
Petr Machata929bd572012-12-17 03:20:34 +0100283select_32_64(struct process *proc, void *p32, void *p64)
Petr Machata6a7997d2012-03-29 18:37:15 +0200284{
285 if (sizeof(long) == 4 || proc->mask_32bit)
286 return p32;
287 else
288 return p64;
289}
290
Joe Damato47cae1e2010-11-08 15:47:39 -0800291static int
Petr Machata929bd572012-12-17 03:20:34 +0100292fetch_dyn64(struct process *proc, arch_addr_t *addr, Elf64_Dyn *ret)
Petr Machata6a7997d2012-03-29 18:37:15 +0200293{
294 if (umovebytes(proc, *addr, ret, sizeof(*ret)) != sizeof(*ret))
295 return -1;
296 *addr += sizeof(*ret);
297 return 0;
298}
299
300static int
Petr Machata929bd572012-12-17 03:20:34 +0100301fetch_dyn32(struct process *proc, arch_addr_t *addr, Elf64_Dyn *ret)
Petr Machata6a7997d2012-03-29 18:37:15 +0200302{
303 Elf32_Dyn dyn;
304 if (umovebytes(proc, *addr, &dyn, sizeof(dyn)) != sizeof(dyn))
305 return -1;
306
307 *addr += sizeof(dyn);
308 ret->d_tag = dyn.d_tag;
309 ret->d_un.d_val = dyn.d_un.d_val;
310
311 return 0;
312}
313
314static int (*
Petr Machata929bd572012-12-17 03:20:34 +0100315dyn_fetcher(struct process *proc))(struct process *,
Petr Machatabac2da52012-05-29 00:42:59 +0200316 arch_addr_t *, Elf64_Dyn *)
Petr Machata6a7997d2012-03-29 18:37:15 +0200317{
318 return select_32_64(proc, fetch_dyn32, fetch_dyn64);
319}
320
Edgar E. Iglesiascc77b0e2012-10-09 12:15:20 +0200321int
Petr Machata929bd572012-12-17 03:20:34 +0100322proc_find_dynamic_entry_addr(struct process *proc, arch_addr_t src_addr,
Edgar E. Iglesiascc77b0e2012-10-09 12:15:20 +0200323 int d_tag, arch_addr_t *ret)
Petr Machata6a7997d2012-03-29 18:37:15 +0200324{
Joe Damato47cae1e2010-11-08 15:47:39 -0800325 debug(DEBUG_FUNCTION, "find_dynamic_entry()");
326
Edgar E. Iglesiasfd4b4ef2012-10-09 12:21:17 +0200327 if (ret == NULL || src_addr == 0 || d_tag < 0)
Joe Damato47cae1e2010-11-08 15:47:39 -0800328 return -1;
Joe Damato47cae1e2010-11-08 15:47:39 -0800329
Petr Machata6a7997d2012-03-29 18:37:15 +0200330 int i = 0;
331 while (1) {
332 Elf64_Dyn entry;
333 if (dyn_fetcher(proc)(proc, &src_addr, &entry) < 0
334 || entry.d_tag == DT_NULL
335 || i++ > 100) { /* Arbitrary cut-off so that we
336 * don't loop forever if the
337 * binary is corrupted. */
338 debug(2, "Couldn't find address for dtag!");
339 return -1;
340 }
341
Joe Damato47cae1e2010-11-08 15:47:39 -0800342 if (entry.d_tag == d_tag) {
Petr Machataea8eb9a2012-04-17 01:32:07 +0200343 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200344 * arch_addr_t becomes integral type. */
345 *ret = (arch_addr_t)(uintptr_t)entry.d_un.d_val;
Petr Machata8454bd72012-04-17 17:12:44 +0200346 debug(2, "found address: %p in dtag %d", *ret, d_tag);
Petr Machata17476b72012-02-23 18:50:37 +0100347 return 0;
Joe Damato47cae1e2010-11-08 15:47:39 -0800348 }
Petr Machata6a7997d2012-03-29 18:37:15 +0200349 }
350}
351
352/* Our own type for representing 32-bit linkmap. We can't rely on the
353 * definition in link.h, because that's only accurate for our host
354 * architecture, not for target architecture (where the traced process
355 * runs). */
356#define LT_LINK_MAP(BITS) \
357 { \
358 Elf##BITS##_Addr l_addr; \
359 Elf##BITS##_Addr l_name; \
360 Elf##BITS##_Addr l_ld; \
361 Elf##BITS##_Addr l_next; \
362 Elf##BITS##_Addr l_prev; \
363 }
364struct lt_link_map_32 LT_LINK_MAP(32);
365struct lt_link_map_64 LT_LINK_MAP(64);
366
367static int
Petr Machata929bd572012-12-17 03:20:34 +0100368fetch_lm64(struct process *proc, arch_addr_t addr,
Petr Machata6a7997d2012-03-29 18:37:15 +0200369 struct lt_link_map_64 *ret)
370{
371 if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
372 return -1;
373 return 0;
374}
375
376static int
Petr Machata929bd572012-12-17 03:20:34 +0100377fetch_lm32(struct process *proc, arch_addr_t addr,
Petr Machata6a7997d2012-03-29 18:37:15 +0200378 struct lt_link_map_64 *ret)
379{
380 struct lt_link_map_32 lm;
381 if (umovebytes(proc, addr, &lm, sizeof(lm)) != sizeof(lm))
382 return -1;
383
384 ret->l_addr = lm.l_addr;
385 ret->l_name = lm.l_name;
386 ret->l_ld = lm.l_ld;
387 ret->l_next = lm.l_next;
388 ret->l_prev = lm.l_prev;
389
390 return 0;
391}
392
393static int (*
Petr Machata929bd572012-12-17 03:20:34 +0100394lm_fetcher(struct process *proc))(struct process *,
Petr Machatabac2da52012-05-29 00:42:59 +0200395 arch_addr_t, struct lt_link_map_64 *)
Petr Machata6a7997d2012-03-29 18:37:15 +0200396{
397 return select_32_64(proc, fetch_lm32, fetch_lm64);
398}
399
400/* The same as above holds for struct r_debug. */
401#define LT_R_DEBUG(BITS) \
402 { \
403 int r_version; \
404 Elf##BITS##_Addr r_map; \
405 Elf##BITS##_Addr r_brk; \
406 int r_state; \
407 Elf##BITS##_Addr r_ldbase; \
Joe Damato47cae1e2010-11-08 15:47:39 -0800408 }
409
Petr Machata6a7997d2012-03-29 18:37:15 +0200410struct lt_r_debug_32 LT_R_DEBUG(32);
411struct lt_r_debug_64 LT_R_DEBUG(64);
412
413static int
Petr Machata929bd572012-12-17 03:20:34 +0100414fetch_rd64(struct process *proc, arch_addr_t addr,
Petr Machata6a7997d2012-03-29 18:37:15 +0200415 struct lt_r_debug_64 *ret)
416{
417 if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
418 return -1;
419 return 0;
420}
421
422static int
Petr Machata929bd572012-12-17 03:20:34 +0100423fetch_rd32(struct process *proc, arch_addr_t addr,
Petr Machata6a7997d2012-03-29 18:37:15 +0200424 struct lt_r_debug_64 *ret)
425{
426 struct lt_r_debug_32 rd;
427 if (umovebytes(proc, addr, &rd, sizeof(rd)) != sizeof(rd))
428 return -1;
429
430 ret->r_version = rd.r_version;
431 ret->r_map = rd.r_map;
432 ret->r_brk = rd.r_brk;
433 ret->r_state = rd.r_state;
434 ret->r_ldbase = rd.r_ldbase;
435
436 return 0;
437}
438
439static int (*
Petr Machata929bd572012-12-17 03:20:34 +0100440rdebug_fetcher(struct process *proc))(struct process *,
Petr Machatabac2da52012-05-29 00:42:59 +0200441 arch_addr_t, struct lt_r_debug_64 *)
Petr Machata6a7997d2012-03-29 18:37:15 +0200442{
443 return select_32_64(proc, fetch_rd32, fetch_rd64);
Joe Damato47cae1e2010-11-08 15:47:39 -0800444}
Joe Damatof0bd98b2010-11-08 15:47:42 -0800445
Petr Machata0475ac32012-10-18 15:37:04 +0200446static int
447fetch_auxv64_entry(int fd, Elf64_auxv_t *ret)
448{
449 /* Reaching EOF is as much problem as not reading whole
450 * entry. */
451 return read(fd, ret, sizeof(*ret)) == sizeof(*ret) ? 0 : -1;
452}
453
454static int
455fetch_auxv32_entry(int fd, Elf64_auxv_t *ret)
456{
457 Elf32_auxv_t auxv;
458 if (read(fd, &auxv, sizeof(auxv)) != sizeof(auxv))
459 return -1;
460
461 ret->a_type = auxv.a_type;
462 ret->a_un.a_val = auxv.a_un.a_val;
463 return 0;
464}
465
466static int (*
Petr Machata929bd572012-12-17 03:20:34 +0100467auxv_fetcher(struct process *proc))(int, Elf64_auxv_t *)
Petr Machata0475ac32012-10-18 15:37:04 +0200468{
469 return select_32_64(proc, fetch_auxv32_entry, fetch_auxv64_entry);
470}
471
Joe Damatof0bd98b2010-11-08 15:47:42 -0800472static void
Petr Machata929bd572012-12-17 03:20:34 +0100473crawl_linkmap(struct process *proc, struct lt_r_debug_64 *dbg)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100474{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800475 debug (DEBUG_FUNCTION, "crawl_linkmap()");
476
477 if (!dbg || !dbg->r_map) {
478 debug(2, "Debug structure or it's linkmap are NULL!");
479 return;
480 }
481
Petr Machataea8eb9a2012-04-17 01:32:07 +0200482 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200483 * arch_addr_t becomes integral type. */
484 arch_addr_t addr = (arch_addr_t)(uintptr_t)dbg->r_map;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800485
Petr Machata6a7997d2012-03-29 18:37:15 +0200486 while (addr != 0) {
Petr Machataefd12cf2013-01-02 16:01:43 +0100487 struct lt_link_map_64 rlm = {};
Petr Machata6a7997d2012-03-29 18:37:15 +0200488 if (lm_fetcher(proc)(proc, addr, &rlm) < 0) {
Petr Machata74d80542012-04-03 12:40:59 +0200489 debug(2, "Unable to read link map");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800490 return;
491 }
492
Petr Machatabac2da52012-05-29 00:42:59 +0200493 arch_addr_t key = addr;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200494 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200495 * arch_addr_t becomes integral type. */
496 addr = (arch_addr_t)(uintptr_t)rlm.l_next;
Petr Machata6a7997d2012-03-29 18:37:15 +0200497 if (rlm.l_name == 0) {
Petr Machata74d80542012-04-03 12:40:59 +0200498 debug(2, "Name of mapped library is NULL");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800499 return;
500 }
501
Petr Machata6a7997d2012-03-29 18:37:15 +0200502 char lib_name[BUFSIZ];
Petr Machataea8eb9a2012-04-17 01:32:07 +0200503 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200504 * arch_addr_t becomes integral type. */
505 umovebytes(proc, (arch_addr_t)(uintptr_t)rlm.l_name,
Petr Machata6a7997d2012-03-29 18:37:15 +0200506 lib_name, sizeof(lib_name));
Joe Damatof0bd98b2010-11-08 15:47:42 -0800507
Petr Machata15595a32012-11-12 21:53:59 +0100508 /* Library name can be an empty string, in which case
509 * the entry represents either the main binary, or a
510 * VDSO. Unfortunately we can't rely on that, as in
511 * recent glibc, that entry is initialized to VDSO
512 * SONAME.
513 *
514 * It's not clear how to detect VDSO in this case. We
515 * can't assume that l_name of real DSOs will be
516 * either absolute or relative (for LD_LIBRARY_PATH=:
517 * it will be neither). We can't compare l_addr with
518 * AT_SYSINFO_EHDR either, as l_addr is bias (which
519 * also means it's not unique, and therefore useless
520 * for this). We could load VDSO from process image
521 * and at least compare actual SONAMEs. For now, this
522 * kludge is about the best that we can do. */
523 if (*lib_name == 0
524 || strcmp(lib_name, "linux-vdso.so.1") == 0
Petr Machatab2cfac82012-11-30 11:30:05 +0100525 || strcmp(lib_name, "linux-gate.so.1") == 0
526 || strcmp(lib_name, "linux-vdso32.so.1") == 0
527 || strcmp(lib_name, "linux-vdso64.so.1") == 0)
Joe Damatof0bd98b2010-11-08 15:47:42 -0800528 continue;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800529
Petr Machata89ac0392012-04-03 02:30:48 +0200530 /* Do we have that library already? */
531 if (proc_each_library(proc, NULL, library_with_key_cb, &key))
532 continue;
533
Petr Machatab5f80ac2012-04-04 01:46:18 +0200534 struct library *lib = malloc(sizeof(*lib));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100535 if (lib == NULL) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200536 fail:
Petr Machata72871662013-10-11 14:29:09 +0200537 free(lib);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200538 fprintf(stderr, "Couldn't load ELF object %s: %s\n",
539 lib_name, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100540 continue;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800541 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200542
Petr Machata72871662013-10-11 14:29:09 +0200543 if (library_init(lib, LT_LIBTYPE_DSO) < 0)
Petr Machatab5f80ac2012-04-04 01:46:18 +0200544 goto fail;
545
Petr Machata72871662013-10-11 14:29:09 +0200546 if (ltelf_read_library(lib, proc, lib_name, rlm.l_addr) < 0) {
547 library_destroy(lib);
548 goto fail;
549 }
550
Petr Machata89ac0392012-04-03 02:30:48 +0200551 lib->key = key;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100552 proc_add_library(proc, lib);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800553 }
554 return;
555}
556
Petr Machata6a7997d2012-03-29 18:37:15 +0200557static int
Petr Machata929bd572012-12-17 03:20:34 +0100558load_debug_struct(struct process *proc, struct lt_r_debug_64 *ret)
Petr Machata6a7997d2012-03-29 18:37:15 +0200559{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800560 debug(DEBUG_FUNCTION, "load_debug_struct");
561
Petr Machata47d70f62012-10-26 23:43:51 +0200562 if (rdebug_fetcher(proc)(proc, proc->os.debug_addr, ret) < 0) {
Petr Machatac06f23d2012-10-30 22:03:31 +0100563 debug(2, "This process does not have a debug structure!");
Petr Machata6a7997d2012-03-29 18:37:15 +0200564 return -1;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800565 }
566
Petr Machata6a7997d2012-03-29 18:37:15 +0200567 return 0;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800568}
569
570static void
Petr Machata929bd572012-12-17 03:20:34 +0100571rdebug_bp_on_hit(struct breakpoint *bp, struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100572{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800573 debug(DEBUG_FUNCTION, "arch_check_dbg");
574
Petr Machata6a7997d2012-03-29 18:37:15 +0200575 struct lt_r_debug_64 rdbg;
576 if (load_debug_struct(proc, &rdbg) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800577 debug(2, "Unable to load debug structure!");
578 return;
579 }
580
Petr Machata6a7997d2012-03-29 18:37:15 +0200581 if (rdbg.r_state == RT_CONSISTENT) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800582 debug(2, "Linkmap is now consistent");
Petr Machata47d70f62012-10-26 23:43:51 +0200583 switch (proc->os.debug_state) {
584 case RT_ADD:
Joe Damatof0bd98b2010-11-08 15:47:42 -0800585 debug(2, "Adding DSO to linkmap");
Petr Machata6a7997d2012-03-29 18:37:15 +0200586 crawl_linkmap(proc, &rdbg);
Petr Machata47d70f62012-10-26 23:43:51 +0200587 break;
588 case RT_DELETE:
Joe Damatof0bd98b2010-11-08 15:47:42 -0800589 debug(2, "Removing DSO from linkmap");
Petr Machata47d70f62012-10-26 23:43:51 +0200590 // XXX unload that library
591 break;
592 default:
Joe Damatof0bd98b2010-11-08 15:47:42 -0800593 debug(2, "Unexpected debug state!");
594 }
595 }
596
Petr Machata47d70f62012-10-26 23:43:51 +0200597 proc->os.debug_state = rdbg.r_state;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800598}
599
Edgar E. Iglesias11c3c3e2012-10-09 12:16:52 +0200600#ifndef ARCH_HAVE_FIND_DL_DEBUG
601int
Petr Machata929bd572012-12-17 03:20:34 +0100602arch_find_dl_debug(struct process *proc, arch_addr_t dyn_addr,
Edgar E. Iglesias11c3c3e2012-10-09 12:16:52 +0200603 arch_addr_t *ret)
604{
605 return proc_find_dynamic_entry_addr(proc, dyn_addr, DT_DEBUG, ret);
606}
607#endif
608
Joe Damatof0bd98b2010-11-08 15:47:42 -0800609int
Petr Machata929bd572012-12-17 03:20:34 +0100610linkmap_init(struct process *proc, arch_addr_t dyn_addr)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100611{
Petr Machatab8fd68d2012-10-18 18:54:27 +0200612 debug(DEBUG_FUNCTION, "linkmap_init(%d, dyn_addr=%p)", proc->pid, dyn_addr);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800613
Petr Machata47d70f62012-10-26 23:43:51 +0200614 if (arch_find_dl_debug(proc, dyn_addr, &proc->os.debug_addr) == -1) {
Petr Machata029171f2012-04-05 02:14:30 +0200615 debug(2, "Couldn't find debug structure!");
Petr Machata47d70f62012-10-26 23:43:51 +0200616 return -1;
Petr Machata029171f2012-04-05 02:14:30 +0200617 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800618
Petr Machata6a7997d2012-03-29 18:37:15 +0200619 int status;
620 struct lt_r_debug_64 rdbg;
621 if ((status = load_debug_struct(proc, &rdbg)) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800622 debug(2, "No debug structure or no memory to allocate one!");
Petr Machata6a7997d2012-03-29 18:37:15 +0200623 return status;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800624 }
625
Petr Machata4e553c02013-01-07 17:16:58 +0100626 crawl_linkmap(proc, &rdbg);
627
Petr Machataea8eb9a2012-04-17 01:32:07 +0200628 /* XXX The double cast should be removed when
Petr Machatabac2da52012-05-29 00:42:59 +0200629 * arch_addr_t becomes integral type. */
630 arch_addr_t addr = (arch_addr_t)(uintptr_t)rdbg.r_brk;
Petr Machatab1492df2012-04-30 21:01:40 +0200631 if (arch_translate_address_dyn(proc, addr, &addr) < 0)
Petr Machata47d70f62012-10-26 23:43:51 +0200632 return -1;
Petr Machata89ac0392012-04-03 02:30:48 +0200633
Petr Machata02a796e2013-10-11 17:24:30 +0200634 struct breakpoint *rdebug_bp = insert_breakpoint_at(proc, addr, NULL);
Petr Machata4e553c02013-01-07 17:16:58 +0100635 if (rdebug_bp == NULL) {
636 /* This is not fatal, the tracing can continue with
637 * reduced functionality. */
638 fprintf(stderr,
639 "Couldn't insert _r_debug breakpoint to %d: %s.\n"
640 "As a result of that, ltrace will not be able to "
641 "detect and trace\nnewly-loaded libraries.\n",
642 proc->pid, strerror(errno));
643 } else {
644 static struct bp_callbacks rdebug_callbacks = {
645 .on_hit = rdebug_bp_on_hit,
646 };
647 rdebug_bp->cbs = &rdebug_callbacks;
648 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800649
Joe Damatof0bd98b2010-11-08 15:47:42 -0800650 return 0;
651}
Petr Machata9a5420c2011-07-09 11:21:23 +0200652
653int
654task_kill (pid_t pid, int sig)
655{
656 // Taken from GDB
657 int ret;
658
659 errno = 0;
660 ret = syscall (__NR_tkill, pid, sig);
661 return ret;
662}
Petr Machatacd972582012-01-07 03:02:07 +0100663
664void
Petr Machata929bd572012-12-17 03:20:34 +0100665process_removed(struct process *proc)
Petr Machatacd972582012-01-07 03:02:07 +0100666{
667 delete_events_for(proc);
668}
Petr Machata0475ac32012-10-18 15:37:04 +0200669
670int
Petr Machata929bd572012-12-17 03:20:34 +0100671process_get_entry(struct process *proc,
Petr Machata0475ac32012-10-18 15:37:04 +0200672 arch_addr_t *entryp,
673 arch_addr_t *interp_biasp)
674{
675 PROC_PID_FILE(fn, "/proc/%d/auxv", proc->pid);
676 int fd = open(fn, O_RDONLY);
Petr Machata4754ea02012-11-08 18:54:19 +0100677 int ret = 0;
Petr Machata0475ac32012-10-18 15:37:04 +0200678 if (fd == -1) {
679 fail:
680 fprintf(stderr, "couldn't read %s: %s", fn, strerror(errno));
Petr Machata4754ea02012-11-08 18:54:19 +0100681 ret = -1;
Petr Machata0475ac32012-10-18 15:37:04 +0200682 done:
683 if (fd != -1)
684 close(fd);
Petr Machata4754ea02012-11-08 18:54:19 +0100685 return ret;
Petr Machata0475ac32012-10-18 15:37:04 +0200686 }
687
688 arch_addr_t at_entry = 0;
689 arch_addr_t at_bias = 0;
690 while (1) {
Petr Machataeb584972012-11-08 18:31:59 +0100691 Elf64_auxv_t entry = {};
Petr Machata0475ac32012-10-18 15:37:04 +0200692 if (auxv_fetcher(proc)(fd, &entry) < 0)
693 goto fail;
694
695 switch (entry.a_type) {
696 case AT_BASE:
697 /* XXX The double cast should be removed when
698 * arch_addr_t becomes integral type. */
699 at_bias = (arch_addr_t)(uintptr_t)entry.a_un.a_val;
700 continue;
701
702 case AT_ENTRY:
703 /* XXX The double cast should be removed when
704 * arch_addr_t becomes integral type. */
705 at_entry = (arch_addr_t)(uintptr_t)entry.a_un.a_val;
706 default:
707 continue;
708
709 case AT_NULL:
710 break;
711 }
712 break;
713 }
714
715 if (entryp != NULL)
716 *entryp = at_entry;
717 if (interp_biasp != NULL)
718 *interp_biasp = at_bias;
719 goto done;
720}
Petr Machata47d70f62012-10-26 23:43:51 +0200721
722int
Petr Machata929bd572012-12-17 03:20:34 +0100723os_process_init(struct process *proc)
Petr Machata47d70f62012-10-26 23:43:51 +0200724{
725 proc->os.debug_addr = 0;
726 proc->os.debug_state = 0;
727 return 0;
728}
729
730void
Petr Machata929bd572012-12-17 03:20:34 +0100731os_process_destroy(struct process *proc)
Petr Machata47d70f62012-10-26 23:43:51 +0200732{
733}
734
735int
Petr Machata929bd572012-12-17 03:20:34 +0100736os_process_clone(struct process *retp, struct process *proc)
Petr Machata47d70f62012-10-26 23:43:51 +0200737{
738 retp->os = proc->os;
739 return 0;
740}
741
742int
Petr Machata929bd572012-12-17 03:20:34 +0100743os_process_exec(struct process *proc)
Petr Machata47d70f62012-10-26 23:43:51 +0200744{
745 return 0;
746}