1 /* Target-dependent code for GNU/Linux, architecture independent.
3 Copyright (C) 2009-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "linux-tdep.h"
25 #include "elf/common.h"
27 #include "cli/cli-utils.h"
31 static struct gdbarch_data *linux_gdbarch_data_handle;
33 struct linux_gdbarch_data
35 struct type *siginfo_type;
39 init_linux_gdbarch_data (struct gdbarch *gdbarch)
41 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
44 static struct linux_gdbarch_data *
45 get_linux_gdbarch_data (struct gdbarch *gdbarch)
47 return gdbarch_data (gdbarch, linux_gdbarch_data_handle);
50 /* This function is suitable for architectures that don't
51 extend/override the standard siginfo structure. */
54 linux_get_siginfo_type (struct gdbarch *gdbarch)
56 struct linux_gdbarch_data *linux_gdbarch_data;
57 struct type *int_type, *uint_type, *long_type, *void_ptr_type;
58 struct type *uid_type, *pid_type;
59 struct type *sigval_type, *clock_type;
60 struct type *siginfo_type, *sifields_type;
63 linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
64 if (linux_gdbarch_data->siginfo_type != NULL)
65 return linux_gdbarch_data->siginfo_type;
67 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
69 uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
71 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
73 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
76 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
77 TYPE_NAME (sigval_type) = xstrdup ("sigval_t");
78 append_composite_type_field (sigval_type, "sival_int", int_type);
79 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
82 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
83 TYPE_LENGTH (int_type), "__pid_t");
84 TYPE_TARGET_TYPE (pid_type) = int_type;
85 TYPE_TARGET_STUB (pid_type) = 1;
88 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
89 TYPE_LENGTH (uint_type), "__uid_t");
90 TYPE_TARGET_TYPE (uid_type) = uint_type;
91 TYPE_TARGET_STUB (uid_type) = 1;
94 clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
95 TYPE_LENGTH (long_type), "__clock_t");
96 TYPE_TARGET_TYPE (clock_type) = long_type;
97 TYPE_TARGET_STUB (clock_type) = 1;
100 sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
103 const int si_max_size = 128;
105 int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
108 if (gdbarch_ptr_bit (gdbarch) == 64)
109 si_pad_size = (si_max_size / size_of_int) - 4;
111 si_pad_size = (si_max_size / size_of_int) - 3;
112 append_composite_type_field (sifields_type, "_pad",
113 init_vector_type (int_type, si_pad_size));
117 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
118 append_composite_type_field (type, "si_pid", pid_type);
119 append_composite_type_field (type, "si_uid", uid_type);
120 append_composite_type_field (sifields_type, "_kill", type);
123 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
124 append_composite_type_field (type, "si_tid", int_type);
125 append_composite_type_field (type, "si_overrun", int_type);
126 append_composite_type_field (type, "si_sigval", sigval_type);
127 append_composite_type_field (sifields_type, "_timer", type);
130 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
131 append_composite_type_field (type, "si_pid", pid_type);
132 append_composite_type_field (type, "si_uid", uid_type);
133 append_composite_type_field (type, "si_sigval", sigval_type);
134 append_composite_type_field (sifields_type, "_rt", type);
137 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
138 append_composite_type_field (type, "si_pid", pid_type);
139 append_composite_type_field (type, "si_uid", uid_type);
140 append_composite_type_field (type, "si_status", int_type);
141 append_composite_type_field (type, "si_utime", clock_type);
142 append_composite_type_field (type, "si_stime", clock_type);
143 append_composite_type_field (sifields_type, "_sigchld", type);
146 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
147 append_composite_type_field (type, "si_addr", void_ptr_type);
148 append_composite_type_field (sifields_type, "_sigfault", type);
151 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
152 append_composite_type_field (type, "si_band", long_type);
153 append_composite_type_field (type, "si_fd", int_type);
154 append_composite_type_field (sifields_type, "_sigpoll", type);
157 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
158 TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
159 append_composite_type_field (siginfo_type, "si_signo", int_type);
160 append_composite_type_field (siginfo_type, "si_errno", int_type);
161 append_composite_type_field (siginfo_type, "si_code", int_type);
162 append_composite_type_field_aligned (siginfo_type,
163 "_sifields", sifields_type,
164 TYPE_LENGTH (long_type));
166 linux_gdbarch_data->siginfo_type = siginfo_type;
172 linux_has_shared_address_space (void)
174 /* Determine whether we are running on uClinux or normal Linux
177 int target_is_uclinux;
180 = (target_auxv_search (¤t_target, AT_NULL, &dummy) > 0
181 && target_auxv_search (¤t_target, AT_PAGESZ, &dummy) == 0);
183 return target_is_uclinux;
186 /* This is how we want PTIDs from core files to be printed. */
189 linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
193 if (ptid_get_lwp (ptid) != 0)
195 snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
199 return normal_pid_to_str (ptid);
202 /* Service function for corefiles and info proc. */
205 read_mapping (const char *line,
206 ULONGEST *addr, ULONGEST *endaddr,
207 const char **permissions, size_t *permissions_len,
209 const char **device, size_t *device_len,
211 const char **filename)
213 const char *p = line;
215 *addr = strtoulst (p, &p, 16);
218 *endaddr = strtoulst (p, &p, 16);
220 while (*p && isspace (*p))
223 while (*p && !isspace (*p))
225 *permissions_len = p - *permissions;
227 *offset = strtoulst (p, &p, 16);
229 while (*p && isspace (*p))
232 while (*p && !isspace (*p))
234 *device_len = p - *device;
236 *inode = strtoulst (p, &p, 10);
238 while (*p && isspace (*p))
243 /* Implement the "info proc" command. */
246 linux_info_proc (struct gdbarch *gdbarch, char *args,
247 enum info_proc_what what)
249 /* A long is used for pid instead of an int to avoid a loss of precision
250 compiler warning from the output of strtoul. */
252 int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
253 int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
254 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
255 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
256 int status_f = (what == IP_STATUS || what == IP_ALL);
257 int stat_f = (what == IP_STAT || what == IP_ALL);
262 if (args && isdigit (args[0]))
263 pid = strtoul (args, &args, 10);
266 if (!target_has_execution)
267 error (_("No current process: you must name one."));
268 if (current_inferior ()->fake_pid_p)
269 error (_("Can't determine the current process's PID: you must name one."));
271 pid = current_inferior ()->pid;
274 args = skip_spaces (args);
276 error (_("Too many parameters: %s"), args);
278 printf_filtered (_("process %ld\n"), pid);
281 xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
282 data = target_fileio_read_stralloc (filename);
285 struct cleanup *cleanup = make_cleanup (xfree, data);
286 printf_filtered ("cmdline = '%s'\n", data);
287 do_cleanups (cleanup);
290 warning (_("unable to open /proc file '%s'"), filename);
294 xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
295 data = target_fileio_readlink (filename, &target_errno);
298 struct cleanup *cleanup = make_cleanup (xfree, data);
299 printf_filtered ("cwd = '%s'\n", data);
300 do_cleanups (cleanup);
303 warning (_("unable to read link '%s'"), filename);
307 xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
308 data = target_fileio_readlink (filename, &target_errno);
311 struct cleanup *cleanup = make_cleanup (xfree, data);
312 printf_filtered ("exe = '%s'\n", data);
313 do_cleanups (cleanup);
316 warning (_("unable to read link '%s'"), filename);
320 xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
321 data = target_fileio_read_stralloc (filename);
324 struct cleanup *cleanup = make_cleanup (xfree, data);
327 printf_filtered (_("Mapped address spaces:\n\n"));
328 if (gdbarch_addr_bit (gdbarch) == 32)
330 printf_filtered ("\t%10s %10s %10s %10s %s\n",
333 " Size", " Offset", "objfile");
337 printf_filtered (" %18s %18s %10s %10s %s\n",
340 " Size", " Offset", "objfile");
343 for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
345 ULONGEST addr, endaddr, offset, inode;
346 const char *permissions, *device, *filename;
347 size_t permissions_len, device_len;
349 read_mapping (line, &addr, &endaddr,
350 &permissions, &permissions_len,
351 &offset, &device, &device_len,
354 if (gdbarch_addr_bit (gdbarch) == 32)
356 printf_filtered ("\t%10s %10s %10s %10s %s\n",
357 paddress (gdbarch, addr),
358 paddress (gdbarch, endaddr),
359 hex_string (endaddr - addr),
361 *filename? filename : "");
365 printf_filtered (" %18s %18s %10s %10s %s\n",
366 paddress (gdbarch, addr),
367 paddress (gdbarch, endaddr),
368 hex_string (endaddr - addr),
370 *filename? filename : "");
374 do_cleanups (cleanup);
377 warning (_("unable to open /proc file '%s'"), filename);
381 xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
382 data = target_fileio_read_stralloc (filename);
385 struct cleanup *cleanup = make_cleanup (xfree, data);
386 puts_filtered (data);
387 do_cleanups (cleanup);
390 warning (_("unable to open /proc file '%s'"), filename);
394 xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
395 data = target_fileio_read_stralloc (filename);
398 struct cleanup *cleanup = make_cleanup (xfree, data);
399 const char *p = data;
403 printf_filtered (_("Process: %s\n"),
404 pulongest (strtoulst (p, &p, 10)));
406 while (*p && isspace (*p))
408 if (*p == '(' && (ep = strchr (p, ')')) != NULL)
410 printf_filtered ("Exec file: %.*s\n", (int) (ep - p - 1), p + 1);
414 while (*p && isspace (*p))
417 printf_filtered (_("State: %c\n"), *p++);
420 printf_filtered (_("Parent process: %s\n"),
421 pulongest (strtoulst (p, &p, 10)));
423 printf_filtered (_("Process group: %s\n"),
424 pulongest (strtoulst (p, &p, 10)));
426 printf_filtered (_("Session id: %s\n"),
427 pulongest (strtoulst (p, &p, 10)));
429 printf_filtered (_("TTY: %s\n"),
430 pulongest (strtoulst (p, &p, 10)));
432 printf_filtered (_("TTY owner process group: %s\n"),
433 pulongest (strtoulst (p, &p, 10)));
436 printf_filtered (_("Flags: %s\n"),
437 hex_string (strtoulst (p, &p, 10)));
439 printf_filtered (_("Minor faults (no memory page): %s\n"),
440 pulongest (strtoulst (p, &p, 10)));
442 printf_filtered (_("Minor faults, children: %s\n"),
443 pulongest (strtoulst (p, &p, 10)));
445 printf_filtered (_("Major faults (memory page faults): %s\n"),
446 pulongest (strtoulst (p, &p, 10)));
448 printf_filtered (_("Major faults, children: %s\n"),
449 pulongest (strtoulst (p, &p, 10)));
451 printf_filtered (_("utime: %s\n"),
452 pulongest (strtoulst (p, &p, 10)));
454 printf_filtered (_("stime: %s\n"),
455 pulongest (strtoulst (p, &p, 10)));
457 printf_filtered (_("utime, children: %s\n"),
458 pulongest (strtoulst (p, &p, 10)));
460 printf_filtered (_("stime, children: %s\n"),
461 pulongest (strtoulst (p, &p, 10)));
463 printf_filtered (_("jiffies remaining in current "
465 pulongest (strtoulst (p, &p, 10)));
467 printf_filtered (_("'nice' value: %s\n"),
468 pulongest (strtoulst (p, &p, 10)));
470 printf_filtered (_("jiffies until next timeout: %s\n"),
471 pulongest (strtoulst (p, &p, 10)));
473 printf_filtered (_("jiffies until next SIGALRM: %s\n"),
474 pulongest (strtoulst (p, &p, 10)));
476 printf_filtered (_("start time (jiffies since "
477 "system boot): %s\n"),
478 pulongest (strtoulst (p, &p, 10)));
480 printf_filtered (_("Virtual memory size: %s\n"),
481 pulongest (strtoulst (p, &p, 10)));
483 printf_filtered (_("Resident set size: %s\n"),
484 pulongest (strtoulst (p, &p, 10)));
486 printf_filtered (_("rlim: %s\n"),
487 pulongest (strtoulst (p, &p, 10)));
489 printf_filtered (_("Start of text: %s\n"),
490 hex_string (strtoulst (p, &p, 10)));
492 printf_filtered (_("End of text: %s\n"),
493 hex_string (strtoulst (p, &p, 10)));
495 printf_filtered (_("Start of stack: %s\n"),
496 hex_string (strtoulst (p, &p, 10)));
497 #if 0 /* Don't know how architecture-dependent the rest is...
498 Anyway the signal bitmap info is available from "status". */
500 printf_filtered (_("Kernel stack pointer: %s\n"),
501 hex_string (strtoulst (p, &p, 10)));
503 printf_filtered (_("Kernel instr pointer: %s\n"),
504 hex_string (strtoulst (p, &p, 10)));
506 printf_filtered (_("Pending signals bitmap: %s\n"),
507 hex_string (strtoulst (p, &p, 10)));
509 printf_filtered (_("Blocked signals bitmap: %s\n"),
510 hex_string (strtoulst (p, &p, 10)));
512 printf_filtered (_("Ignored signals bitmap: %s\n"),
513 hex_string (strtoulst (p, &p, 10)));
515 printf_filtered (_("Catched signals bitmap: %s\n"),
516 hex_string (strtoulst (p, &p, 10)));
518 printf_filtered (_("wchan (system call): %s\n"),
519 hex_string (strtoulst (p, &p, 10)));
521 do_cleanups (cleanup);
524 warning (_("unable to open /proc file '%s'"), filename);
528 /* To be called from the various GDB_OSABI_LINUX handlers for the
529 various GNU/Linux architectures and machine types. */
532 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
534 set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
535 set_gdbarch_info_proc (gdbarch, linux_info_proc);
539 _initialize_linux_tdep (void)
541 linux_gdbarch_data_handle =
542 gdbarch_data_register_post_init (init_linux_gdbarch_data);