* gdbarch.sh (info_proc): New callback.
[platform/upstream/binutils.git] / gdb / linux-tdep.c
1 /* Target-dependent code for GNU/Linux, architecture independent.
2
3    Copyright (C) 2009-2012 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20 #include "defs.h"
21 #include "gdbtypes.h"
22 #include "linux-tdep.h"
23 #include "auxv.h"
24 #include "target.h"
25 #include "elf/common.h"
26 #include "inferior.h"
27 #include "cli/cli-utils.h"
28
29 #include <ctype.h>
30
31 static struct gdbarch_data *linux_gdbarch_data_handle;
32
33 struct linux_gdbarch_data
34   {
35     struct type *siginfo_type;
36   };
37
38 static void *
39 init_linux_gdbarch_data (struct gdbarch *gdbarch)
40 {
41   return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
42 }
43
44 static struct linux_gdbarch_data *
45 get_linux_gdbarch_data (struct gdbarch *gdbarch)
46 {
47   return gdbarch_data (gdbarch, linux_gdbarch_data_handle);
48 }
49
50 /* This function is suitable for architectures that don't
51    extend/override the standard siginfo structure.  */
52
53 struct type *
54 linux_get_siginfo_type (struct gdbarch *gdbarch)
55 {
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;
61   struct type *type;
62
63   linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
64   if (linux_gdbarch_data->siginfo_type != NULL)
65     return linux_gdbarch_data->siginfo_type;
66
67   int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
68                                 0, "int");
69   uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
70                                  1, "unsigned int");
71   long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
72                                  0, "long");
73   void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
74
75   /* sival_t */
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);
80
81   /* __pid_t */
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;
86
87   /* __uid_t */
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;
92
93   /* __clock_t */
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;
98
99   /* _sifields */
100   sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
101
102   {
103     const int si_max_size = 128;
104     int si_pad_size;
105     int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
106
107     /* _pad */
108     if (gdbarch_ptr_bit (gdbarch) == 64)
109       si_pad_size = (si_max_size / size_of_int) - 4;
110     else
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));
114   }
115
116   /* _kill */
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);
121
122   /* _timer */
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);
128
129   /* _rt */
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);
135
136   /* _sigchld */
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);
144
145   /* _sigfault */
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);
149
150   /* _sigpoll */
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);
155
156   /* struct siginfo */
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));
165
166   linux_gdbarch_data->siginfo_type = siginfo_type;
167
168   return siginfo_type;
169 }
170
171 int
172 linux_has_shared_address_space (void)
173 {
174   /* Determine whether we are running on uClinux or normal Linux
175      kernel.  */
176   CORE_ADDR dummy;
177   int target_is_uclinux;
178
179   target_is_uclinux
180     = (target_auxv_search (&current_target, AT_NULL, &dummy) > 0
181        && target_auxv_search (&current_target, AT_PAGESZ, &dummy) == 0);
182
183   return target_is_uclinux;
184 }
185
186 /* This is how we want PTIDs from core files to be printed.  */
187
188 static char *
189 linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
190 {
191   static char buf[80];
192
193   if (ptid_get_lwp (ptid) != 0)
194     {
195       snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
196       return buf;
197     }
198
199   return normal_pid_to_str (ptid);
200 }
201
202 /* Service function for corefiles and info proc.  */
203
204 static void
205 read_mapping (const char *line,
206               ULONGEST *addr, ULONGEST *endaddr,
207               const char **permissions, size_t *permissions_len,
208               ULONGEST *offset,
209               const char **device, size_t *device_len,
210               ULONGEST *inode,
211               const char **filename)
212 {
213   const char *p = line;
214
215   *addr = strtoulst (p, &p, 16);
216   if (*p == '-')
217     p++;
218   *endaddr = strtoulst (p, &p, 16);
219
220   while (*p && isspace (*p))
221     p++;
222   *permissions = p;
223   while (*p && !isspace (*p))
224     p++;
225   *permissions_len = p - *permissions;
226
227   *offset = strtoulst (p, &p, 16);
228
229   while (*p && isspace (*p))
230     p++;
231   *device = p;
232   while (*p && !isspace (*p))
233     p++;
234   *device_len = p - *device;
235
236   *inode = strtoulst (p, &p, 10);
237
238   while (*p && isspace (*p))
239     p++;
240   *filename = p;
241 }
242
243 /* Implement the "info proc" command.  */
244
245 static void
246 linux_info_proc (struct gdbarch *gdbarch, char *args,
247                  enum info_proc_what what)
248 {
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.  */
251   long pid;
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);
258   char filename[100];
259   gdb_byte *data;
260   int target_errno;
261
262   if (args && isdigit (args[0]))
263     pid = strtoul (args, &args, 10);
264   else
265     {
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."));
270
271       pid = current_inferior ()->pid;
272     }
273
274   args = skip_spaces (args);
275   if (args && args[0])
276     error (_("Too many parameters: %s"), args);
277
278   printf_filtered (_("process %ld\n"), pid);
279   if (cmdline_f)
280     {
281       xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
282       data = target_fileio_read_stralloc (filename);
283       if (data)
284         {
285           struct cleanup *cleanup = make_cleanup (xfree, data);
286           printf_filtered ("cmdline = '%s'\n", data);
287           do_cleanups (cleanup);
288         }
289       else
290         warning (_("unable to open /proc file '%s'"), filename);
291     }
292   if (cwd_f)
293     {
294       xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
295       data = target_fileio_readlink (filename, &target_errno);
296       if (data)
297         {
298           struct cleanup *cleanup = make_cleanup (xfree, data);
299           printf_filtered ("cwd = '%s'\n", data);
300           do_cleanups (cleanup);
301         }
302       else
303         warning (_("unable to read link '%s'"), filename);
304     }
305   if (exe_f)
306     {
307       xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
308       data = target_fileio_readlink (filename, &target_errno);
309       if (data)
310         {
311           struct cleanup *cleanup = make_cleanup (xfree, data);
312           printf_filtered ("exe = '%s'\n", data);
313           do_cleanups (cleanup);
314         }
315       else
316         warning (_("unable to read link '%s'"), filename);
317     }
318   if (mappings_f)
319     {
320       xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
321       data = target_fileio_read_stralloc (filename);
322       if (data)
323         {
324           struct cleanup *cleanup = make_cleanup (xfree, data);
325           char *line;
326
327           printf_filtered (_("Mapped address spaces:\n\n"));
328           if (gdbarch_addr_bit (gdbarch) == 32)
329             {
330               printf_filtered ("\t%10s %10s %10s %10s %s\n",
331                            "Start Addr",
332                            "  End Addr",
333                            "      Size", "    Offset", "objfile");
334             }
335           else
336             {
337               printf_filtered ("  %18s %18s %10s %10s %s\n",
338                            "Start Addr",
339                            "  End Addr",
340                            "      Size", "    Offset", "objfile");
341             }
342
343           for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
344             {
345               ULONGEST addr, endaddr, offset, inode;
346               const char *permissions, *device, *filename;
347               size_t permissions_len, device_len;
348
349               read_mapping (line, &addr, &endaddr,
350                             &permissions, &permissions_len,
351                             &offset, &device, &device_len,
352                             &inode, &filename);
353
354               if (gdbarch_addr_bit (gdbarch) == 32)
355                 {
356                   printf_filtered ("\t%10s %10s %10s %10s %s\n",
357                                    paddress (gdbarch, addr),
358                                    paddress (gdbarch, endaddr),
359                                    hex_string (endaddr - addr),
360                                    hex_string (offset),
361                                    *filename? filename : "");
362                 }
363               else
364                 {
365                   printf_filtered ("  %18s %18s %10s %10s %s\n",
366                                    paddress (gdbarch, addr),
367                                    paddress (gdbarch, endaddr),
368                                    hex_string (endaddr - addr),
369                                    hex_string (offset),
370                                    *filename? filename : "");
371                 }
372             }
373
374           do_cleanups (cleanup);
375         }
376       else
377         warning (_("unable to open /proc file '%s'"), filename);
378     }
379   if (status_f)
380     {
381       xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
382       data = target_fileio_read_stralloc (filename);
383       if (data)
384         {
385           struct cleanup *cleanup = make_cleanup (xfree, data);
386           puts_filtered (data);
387           do_cleanups (cleanup);
388         }
389       else
390         warning (_("unable to open /proc file '%s'"), filename);
391     }
392   if (stat_f)
393     {
394       xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
395       data = target_fileio_read_stralloc (filename);
396       if (data)
397         {
398           struct cleanup *cleanup = make_cleanup (xfree, data);
399           const char *p = data;
400           const char *ep;
401           ULONGEST val;
402
403           printf_filtered (_("Process: %s\n"),
404                            pulongest (strtoulst (p, &p, 10)));
405
406           while (*p && isspace (*p))
407             p++;
408           if (*p == '(' && (ep = strchr (p, ')')) != NULL)
409             {
410               printf_filtered ("Exec file: %.*s\n", (int) (ep - p - 1), p + 1);
411               p = ep + 1;
412             }
413
414           while (*p && isspace (*p))
415             p++;
416           if (*p)
417             printf_filtered (_("State: %c\n"), *p++);
418
419           if (*p)
420             printf_filtered (_("Parent process: %s\n"),
421                              pulongest (strtoulst (p, &p, 10)));
422           if (*p)
423             printf_filtered (_("Process group: %s\n"),
424                              pulongest (strtoulst (p, &p, 10)));
425           if (*p)
426             printf_filtered (_("Session id: %s\n"),
427                              pulongest (strtoulst (p, &p, 10)));
428           if (*p)
429             printf_filtered (_("TTY: %s\n"),
430                              pulongest (strtoulst (p, &p, 10)));
431           if (*p)
432             printf_filtered (_("TTY owner process group: %s\n"),
433                              pulongest (strtoulst (p, &p, 10)));
434
435           if (*p)
436             printf_filtered (_("Flags: %s\n"),
437                              hex_string (strtoulst (p, &p, 10)));
438           if (*p)
439             printf_filtered (_("Minor faults (no memory page): %s\n"),
440                              pulongest (strtoulst (p, &p, 10)));
441           if (*p)
442             printf_filtered (_("Minor faults, children: %s\n"),
443                              pulongest (strtoulst (p, &p, 10)));
444           if (*p)
445             printf_filtered (_("Major faults (memory page faults): %s\n"),
446                              pulongest (strtoulst (p, &p, 10)));
447           if (*p)
448             printf_filtered (_("Major faults, children: %s\n"),
449                              pulongest (strtoulst (p, &p, 10)));
450           if (*p)
451             printf_filtered (_("utime: %s\n"),
452                              pulongest (strtoulst (p, &p, 10)));
453           if (*p)
454             printf_filtered (_("stime: %s\n"),
455                              pulongest (strtoulst (p, &p, 10)));
456           if (*p)
457             printf_filtered (_("utime, children: %s\n"),
458                              pulongest (strtoulst (p, &p, 10)));
459           if (*p)
460             printf_filtered (_("stime, children: %s\n"),
461                              pulongest (strtoulst (p, &p, 10)));
462           if (*p)
463             printf_filtered (_("jiffies remaining in current "
464                                "time slice: %s\n"),
465                              pulongest (strtoulst (p, &p, 10)));
466           if (*p)
467             printf_filtered (_("'nice' value: %s\n"),
468                              pulongest (strtoulst (p, &p, 10)));
469           if (*p)
470             printf_filtered (_("jiffies until next timeout: %s\n"),
471                              pulongest (strtoulst (p, &p, 10)));
472           if (*p)
473             printf_filtered (_("jiffies until next SIGALRM: %s\n"),
474                              pulongest (strtoulst (p, &p, 10)));
475           if (*p)
476             printf_filtered (_("start time (jiffies since "
477                                "system boot): %s\n"),
478                              pulongest (strtoulst (p, &p, 10)));
479           if (*p)
480             printf_filtered (_("Virtual memory size: %s\n"),
481                              pulongest (strtoulst (p, &p, 10)));
482           if (*p)
483             printf_filtered (_("Resident set size: %s\n"),
484                              pulongest (strtoulst (p, &p, 10)));
485           if (*p)
486             printf_filtered (_("rlim: %s\n"),
487                              pulongest (strtoulst (p, &p, 10)));
488           if (*p)
489             printf_filtered (_("Start of text: %s\n"),
490                              hex_string (strtoulst (p, &p, 10)));
491           if (*p)
492             printf_filtered (_("End of text: %s\n"),
493                              hex_string (strtoulst (p, &p, 10)));
494           if (*p)
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".  */
499           if (*p)
500             printf_filtered (_("Kernel stack pointer: %s\n"),
501                              hex_string (strtoulst (p, &p, 10)));
502           if (*p)
503             printf_filtered (_("Kernel instr pointer: %s\n"),
504                              hex_string (strtoulst (p, &p, 10)));
505           if (*p)
506             printf_filtered (_("Pending signals bitmap: %s\n"),
507                              hex_string (strtoulst (p, &p, 10)));
508           if (*p)
509             printf_filtered (_("Blocked signals bitmap: %s\n"),
510                              hex_string (strtoulst (p, &p, 10)));
511           if (*p)
512             printf_filtered (_("Ignored signals bitmap: %s\n"),
513                              hex_string (strtoulst (p, &p, 10)));
514           if (*p)
515             printf_filtered (_("Catched signals bitmap: %s\n"),
516                              hex_string (strtoulst (p, &p, 10)));
517           if (*p)
518             printf_filtered (_("wchan (system call): %s\n"),
519                              hex_string (strtoulst (p, &p, 10)));
520 #endif
521           do_cleanups (cleanup);
522         }
523       else
524         warning (_("unable to open /proc file '%s'"), filename);
525     }
526 }
527
528 /* To be called from the various GDB_OSABI_LINUX handlers for the
529    various GNU/Linux architectures and machine types.  */
530
531 void
532 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
533 {
534   set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
535   set_gdbarch_info_proc (gdbarch, linux_info_proc);
536 }
537
538 void
539 _initialize_linux_tdep (void)
540 {
541   linux_gdbarch_data_handle =
542     gdbarch_data_register_post_init (init_linux_gdbarch_data);
543 }