]> Git Repo - binutils.git/blob - gdb/infcmd.c
gdb-3.3
[binutils.git] / gdb / infcmd.c
1 /* Memory-access and commands for inferior process, for GDB.
2    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "param.h"
22 #include "symtab.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "environ.h"
26 #include "value.h"
27
28 #include <stdio.h>
29 #include <signal.h>
30 #include <sys/param.h>
31
32 extern char *sys_siglist[];
33
34 #define ERROR_NO_INFERIOR \
35    if (inferior_pid == 0) error ("The program is not being run.");
36
37 /* String containing arguments to give to the program,
38    with a space added at the front.  Just a space means no args.  */
39
40 static char *inferior_args;
41
42 /* File name for default use for standard in/out in the inferior.  */
43
44 char *inferior_io_terminal;
45
46 /* Pid of our debugged inferior, or 0 if no inferior now.  */
47
48 int inferior_pid;
49
50 /* Last signal that the inferior received (why it stopped).  */
51
52 int stop_signal;
53
54 /* Address at which inferior stopped.  */
55
56 CORE_ADDR stop_pc;
57
58 /* Stack frame when program stopped.  */
59
60 FRAME_ADDR stop_frame_address;
61
62 /* Number of breakpoint it stopped at, or 0 if none.  */
63
64 int stop_breakpoint;
65
66 /* Nonzero if stopped due to a step command.  */
67
68 int stop_step;
69
70 /* Nonzero if stopped due to completion of a stack dummy routine.  */
71
72 int stop_stack_dummy;
73
74 /* Nonzero if stopped due to a random (unexpected) signal in inferior
75    process.  */
76
77 int stopped_by_random_signal;
78
79 /* Range to single step within.
80    If this is nonzero, respond to a single-step signal
81    by continuing to step if the pc is in this range.  */
82
83 CORE_ADDR step_range_start; /* Inclusive */
84 CORE_ADDR step_range_end; /* Exclusive */
85
86 /* Stack frame address as of when stepping command was issued.
87    This is how we know when we step into a subroutine call,
88    and how to set the frame for the breakpoint used to step out.  */
89
90 FRAME_ADDR step_frame_address;
91
92 /* 1 means step over all subroutine calls.
93    -1 means step over calls to undebuggable functions.  */
94
95 int step_over_calls;
96
97 /* If stepping, nonzero means step count is > 1
98    so don't print frame next time inferior stops
99    if it stops due to stepping.  */
100
101 int step_multi;
102
103 /* Environment to use for running inferior,
104    in format described in environ.h.  */
105
106 struct environ *inferior_environ;
107
108 CORE_ADDR read_pc ();
109 struct command_line *get_breakpoint_commands ();
110
111 \f
112 int
113 have_inferior_p ()
114 {
115   return inferior_pid != 0;
116 }
117
118 static void 
119 set_args_command (args)
120      char *args;
121 {
122   free (inferior_args);
123   if (!args) args = "";
124   inferior_args = concat (" ", args, "");
125 }
126
127 void
128 tty_command (file, from_tty)
129      char *file;
130      int from_tty;
131 {
132   if (file == 0)
133     error_no_arg ("terminal name for running target process");
134
135   inferior_io_terminal = savestring (file, strlen (file));
136 }
137
138 static void
139 run_command (args, from_tty)
140      char *args;
141      int from_tty;
142 {
143   extern char **environ;
144   register int i;
145   char *exec_file;
146   char *allargs;
147
148   extern int sys_nerr;
149   extern char *sys_errlist[];
150   extern int errno;
151
152   dont_repeat ();
153
154   if (inferior_pid)
155     {
156       if (
157           !query ("The program being debugged has been started already.\n\
158 Start it from the beginning? "))
159         error ("Program not restarted.");
160       kill_inferior ();
161     }
162
163   exec_file = (char *) get_exec_file (1);
164
165   if (remote_debugging)
166     {
167       if (from_tty)
168         {
169           printf ("Starting program: %s\n", exec_file);
170           fflush (stdout);
171         }
172     }
173   else
174     {
175       if (args)
176         set_args_command (args);
177
178       if (from_tty)
179         {
180           printf ("Starting program: %s%s\n",
181                   exec_file, inferior_args);
182           fflush (stdout);
183         }
184
185       allargs = concat ("exec ", exec_file, inferior_args);
186       inferior_pid = create_inferior (allargs, environ_vector (inferior_environ));
187     }
188
189   clear_proceed_status ();
190
191   start_inferior ();
192 }
193 \f
194 void
195 cont_command (proc_count_exp, from_tty)
196      char *proc_count_exp;
197      int from_tty;
198 {
199   ERROR_NO_INFERIOR;
200
201   clear_proceed_status ();
202
203   /* If have argument, set proceed count of breakpoint we stopped at.  */
204
205   if (stop_breakpoint > 0 && proc_count_exp)
206     {
207       set_ignore_count (stop_breakpoint,
208                         parse_and_eval_address (proc_count_exp) - 1,
209                         from_tty);
210       if (from_tty)
211         printf ("  ");
212     }
213
214   if (from_tty)
215     printf ("Continuing.\n");
216
217   proceed (-1, -1, 0);
218 }
219 \f
220 /* Step until outside of current statement.  */
221 static void step_1 ();
222
223 static void
224 step_command (count_string)
225 {
226   step_1 (0, 0, count_string);
227 }
228
229 /* Likewise, but skip over subroutine calls as if single instructions.  */
230
231 static void
232 next_command (count_string)
233 {
234   step_1 (1, 0, count_string);
235 }
236
237 /* Likewise, but step only one instruction.  */
238
239 static void
240 stepi_command (count_string)
241 {
242   step_1 (0, 1, count_string);
243 }
244
245 static void
246 nexti_command (count_string)
247 {
248   step_1 (1, 1, count_string);
249 }
250
251 static void
252 step_1 (skip_subroutines, single_inst, count_string)
253      int skip_subroutines;
254      int single_inst;
255      char *count_string;
256 {
257   register int count = 1;
258
259   ERROR_NO_INFERIOR;
260   count = count_string ? parse_and_eval_address (count_string) : 1;
261
262   for (; count > 0; count--)
263     {
264       clear_proceed_status ();
265
266       step_frame_address = FRAME_FP (get_current_frame ());
267
268       if (! single_inst)
269         {
270           find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
271           if (step_range_end == 0)
272             {
273               int misc;
274
275               misc = find_pc_misc_function (stop_pc);
276               terminal_ours ();
277               printf ("Current function has no line number information.\n");
278               fflush (stdout);
279
280               /* No info or after _etext ("Can't happen") */
281               if (misc == -1 || misc == misc_function_count - 1)
282                 error ("No data available on pc function.");
283
284               printf ("Single stepping until function exit.\n");
285               fflush (stdout);
286
287               step_range_start = misc_function_vector[misc].address;
288               step_range_end = misc_function_vector[misc + 1].address;
289             }
290         }
291       else
292         {
293           /* Say we are stepping, but stop after one insn whatever it does.
294              Don't step through subroutine calls even to undebuggable
295              functions.  */
296           step_range_start = step_range_end = 1;
297           if (!skip_subroutines)
298             step_over_calls = 0;
299         }
300
301       if (skip_subroutines)
302         step_over_calls = 1;
303
304       step_multi = (count > 1);
305       proceed (-1, -1, 1);
306       if (! stop_step)
307         break;
308     }
309 }
310 \f
311 /* Continue program at specified address.  */
312
313 static void
314 jump_command (arg, from_tty)
315      char *arg;
316      int from_tty;
317 {
318   register CORE_ADDR addr;
319   struct symtabs_and_lines sals;
320   struct symtab_and_line sal;
321
322   ERROR_NO_INFERIOR;
323
324   if (!arg)
325     error_no_arg ("starting address");
326
327   sals = decode_line_spec_1 (arg, 1);
328   if (sals.nelts != 1)
329     {
330       error ("Unreasonable jump request");
331     }
332
333   sal = sals.sals[0];
334   free (sals.sals);
335
336   if (sal.symtab == 0 && sal.pc == 0)
337     error ("No source file has been specified.");
338
339   if (sal.pc == 0)
340     sal.pc = find_line_pc (sal.symtab, sal.line);
341
342   {
343     struct symbol *fn = get_frame_function (get_current_frame ());
344     struct symbol *sfn = find_pc_function (sal.pc);
345     if (fn != 0 && sfn != fn
346         && ! query ("Line %d is not in `%s'.  Jump anyway? ",
347                     sal.line, SYMBOL_NAME (fn)))
348       error ("Not confirmed.");
349   }
350
351   if (sal.pc == 0)
352     error ("No line %d in file \"%s\".", sal.line, sal.symtab->filename);
353
354   addr = sal.pc;
355
356   clear_proceed_status ();
357
358   if (from_tty)
359     printf ("Continuing at 0x%x.\n", addr);
360
361   proceed (addr, 0, 0);
362 }
363
364 /* Continue program giving it specified signal.  */
365
366 static void
367 signal_command (signum_exp, from_tty)
368      char *signum_exp;
369      int from_tty;
370 {
371   register int signum;
372
373   dont_repeat ();               /* Too dangerous.  */
374   ERROR_NO_INFERIOR;
375
376   if (!signum_exp)
377     error_no_arg ("signal number");
378
379   signum = parse_and_eval_address (signum_exp);
380
381   clear_proceed_status ();
382
383   if (from_tty)
384     printf ("Continuing with signal %d.\n", signum);
385
386   proceed (stop_pc, signum, 0);
387 }
388
389 /* Execute a "stack dummy", a piece of code stored in the stack
390    by the debugger to be executed in the inferior.
391
392    To call: first, do PUSH_DUMMY_FRAME.
393    Then push the contents of the dummy.  It should end with a breakpoint insn.
394    Then call here, passing address at which to start the dummy.
395
396    The contents of all registers are saved before the dummy frame is popped
397    and copied into the buffer BUFFER.
398
399    The dummy's frame is automatically popped whenever that break is hit.
400    If that is the first time the program stops, run_stack_dummy
401    returns to its caller with that frame already gone.
402    Otherwise, the caller never gets returned to.  */
403
404 /* 4 => return instead of letting the stack dummy run.  */
405
406 static int stack_dummy_testing = 0;
407
408 void
409 run_stack_dummy (addr, buffer)
410      CORE_ADDR addr;
411      REGISTER_TYPE *buffer;
412 {
413   /* Now proceed, having reached the desired place.  */
414   clear_proceed_status ();
415   if (stack_dummy_testing & 4)
416     {
417       POP_FRAME;
418       return;
419     }
420   proceed (addr, 0, 0);
421
422   if (!stop_stack_dummy)
423     error ("Cannot continue previously requested operation.");
424
425   /* On return, the stack dummy has been popped already.  */
426
427   bcopy (stop_registers, buffer, sizeof stop_registers);
428 }
429 \f
430 /* Proceed until we reach the given line as argument or exit the
431    function.  When called with no argument, proceed until we reach a
432    different source line with pc greater than our current one or exit
433    the function.  We skip calls in both cases.
434
435    The effect of this command with an argument is identical to setting
436    a momentary breakpoint at the line specified and executing
437    "finish".
438
439    Note that eventually this command should probably be changed so
440    that only source lines are printed out when we hit the breakpoint
441    we set.  I'm going to postpone this until after a hopeful rewrite
442    of wait_for_inferior and the proceed status code. -- randy */
443
444 void
445 until_next_command (arg, from_tty)
446      char *arg;
447      int from_tty;
448 {
449   FRAME frame;
450   CORE_ADDR pc;
451   struct symbol *func;
452   struct symtab_and_line sal;
453     
454   clear_proceed_status ();
455
456   frame = get_current_frame ();
457
458   /* Step until either exited from this function or greater
459      than the current line (if in symbolic section) or pc (if
460      not). */
461
462   pc = read_pc ();
463   func = find_pc_function (pc);
464   
465   if (!func)
466     {
467       int misc_func = find_pc_misc_function (pc);
468       
469       if (misc_func != -1)
470         error ("Execution is not within a known function.");
471       
472       step_range_start = misc_function_vector[misc_func].address;
473       step_range_end = pc;
474     }
475   else
476     {
477       sal = find_pc_line (pc, 0);
478       
479       step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
480       step_range_end = sal.end;
481     }
482   
483   step_over_calls = 1;
484   step_frame_address = FRAME_FP (frame);
485   
486   step_multi = 0;               /* Only one call to proceed */
487   
488   proceed (-1, -1, 1);
489 }
490
491 void 
492 until_command (arg, from_tty)
493      char *arg;
494      int from_tty;
495 {
496   if (!have_inferior_p ())
497     error ("The program is not being run.");
498
499   if (arg)
500     until_break_command (arg, from_tty);
501   else
502     until_next_command (arg, from_tty);
503 }
504 \f
505 /* "finish": Set a temporary breakpoint at the place
506    the selected frame will return to, then continue.  */
507
508 static void
509 finish_command (arg, from_tty)
510      char *arg;
511      int from_tty;
512 {
513   struct symtab_and_line sal;
514   register FRAME frame;
515   struct frame_info *fi;
516   register struct symbol *function;
517
518   if (!have_inferior_p ())
519     error ("The program is not being run.");
520   if (arg)
521     error ("The \"finish\" command does not take any arguments.");
522
523   frame = get_prev_frame (selected_frame);
524   if (frame == 0)
525     error ("\"finish\" not meaningful in the outermost frame.");
526
527   clear_proceed_status ();
528
529   fi = get_frame_info (frame);
530   sal = find_pc_line (fi->pc, 0);
531   sal.pc = fi->pc;
532   set_momentary_breakpoint (sal, frame);
533
534   /* Find the function we will return from.  */
535
536   fi = get_frame_info (selected_frame);
537   function = find_pc_function (fi->pc);
538
539   if (from_tty)
540     {
541       printf ("Run till exit from ");
542       print_selected_frame ();
543     }
544
545   proceed (-1, -1, 0);
546
547   if (stop_breakpoint == -3 && function != 0)
548     {
549       struct type *value_type;
550       register value val;
551       CORE_ADDR funcaddr;
552
553       value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
554       if (!value_type)
555         fatal ("internal: finish_command: function has no target type");
556       
557       if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
558         return;
559
560       funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
561
562       val = value_being_returned (value_type, stop_registers,
563                                   using_struct_return (function,
564                                                        funcaddr,
565                                                        value_type));
566
567       printf ("Value returned is $%d = ", record_latest_value (val));
568       value_print (val, stdout, 0, Val_no_prettyprint);
569       putchar ('\n');
570     }
571 }
572 \f
573 static void
574 program_info ()
575 {
576   if (inferior_pid == 0)
577     {
578       printf ("The program being debugged is not being run.\n");
579       return;
580     }
581
582   printf ("Program being debugged is in process %d, stopped at 0x%x.\n",
583           inferior_pid, stop_pc);
584   if (stop_step)
585     printf ("It stopped after being stepped.\n");
586   else if (stop_breakpoint > 0)
587     printf ("It stopped at breakpoint %d.\n", stop_breakpoint);
588   else if (stop_signal)
589     printf ("It stopped with signal %d (%s).\n",
590             stop_signal, sys_siglist[stop_signal]);
591
592   printf ("\nType \"info stack\" or \"info reg\" for more information.\n");
593 }
594 \f
595 static void
596 environment_info (var)
597      char *var;
598 {
599   if (var)
600     {
601       register char *val = get_in_environ (inferior_environ, var);
602       if (val)
603         printf ("%s = %s\n", var, val);
604       else
605         printf ("Environment variable \"%s\" not defined.\n", var);
606     }
607   else
608     {
609       register char **vector = environ_vector (inferior_environ);
610       while (*vector)
611         printf ("%s\n", *vector++);
612     }
613 }
614
615 static void
616 set_environment_command (arg)
617      char *arg;
618 {
619   register char *p, *val, *var;
620   int nullset = 0;
621
622   if (arg == 0)
623     error_no_arg ("environment variable and value");
624
625   /* Find seperation between variable name and value */
626   p = (char *) index (arg, '=');
627   val = (char *) index (arg, ' ');
628
629   if (p != 0 && val != 0)
630     {
631       /* We have both a space and an equals.  If the space is before the
632          equals and the only thing between the two is more space, use
633          the equals */
634       if (p > val)
635         while (*val == ' ')
636           val++;
637
638       /* Take the smaller of the two.  If there was space before the
639          "=", they will be the same right now. */
640       p = arg + min (p - arg, val - arg);
641     }
642   else if (val != 0 && p == 0)
643     p = val;
644
645   if (p == arg)
646     error_no_arg ("environment variable to set");
647
648   if (p == 0 || p[1] == 0)
649     {
650       nullset = 1;
651       if (p == 0)
652         p = arg + strlen (arg); /* So that savestring below will work */
653     }
654   else
655     {
656       /* Not setting variable value to null */
657       val = p + 1;
658       while (*val == ' ' || *val == '\t')
659         val++;
660     }
661
662   while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
663
664   var = savestring (arg, p - arg);
665   if (nullset)
666     {
667       printf ("Setting environment variable \"%s\" to null value.\n", var);
668       set_in_environ (inferior_environ, var, "");
669     }
670   else
671     set_in_environ (inferior_environ, var, val);
672   free (var);
673 }
674
675 static void
676 unset_environment_command (var, from_tty)
677      char *var;
678      int from_tty;
679 {
680   if (var == 0)
681     /* If there is no argument, delete all environment variables.
682        Ask for confirmation if reading from the terminal.  */
683     if (!from_tty || query ("Delete all environment variables? "))
684       {
685         free_environ (inferior_environ);
686         inferior_environ = make_environ ();
687       }
688
689   unset_in_environ (inferior_environ, var);
690 }
691 \f
692 /* Read an integer from debugged memory, given address and number of bytes.  */
693
694 long
695 read_memory_integer (memaddr, len)
696      CORE_ADDR memaddr;
697      int len;
698 {
699   char cbuf;
700   short sbuf;
701   int ibuf;
702   long lbuf;
703   int result_err;
704   extern int sys_nerr;
705   extern char *sys_errlist[];
706
707   if (len == sizeof (char))
708     {
709       result_err = read_memory (memaddr, &cbuf, len);
710       if (result_err)
711         error ("Error reading memory address 0x%x: %s (%d).",
712                memaddr, (result_err < sys_nerr ?
713                          sys_errlist[result_err] :
714                          "uknown error"), result_err);
715       return cbuf;
716     }
717   if (len == sizeof (short))
718     {
719       result_err = read_memory (memaddr, &sbuf, len);
720       if (result_err)
721         error ("Error reading memory address 0x%x: %s (%d).",
722                memaddr, (result_err < sys_nerr ?
723                          sys_errlist[result_err] :
724                          "uknown error"), result_err);
725       return sbuf;
726     }
727   if (len == sizeof (int))
728     {
729       result_err = read_memory (memaddr, &ibuf, len);
730       if (result_err)
731         error ("Error reading memory address 0x%x: %s (%d).",
732                memaddr, (result_err < sys_nerr ?
733                          sys_errlist[result_err] :
734                          "uknown error"), result_err);
735       return ibuf;
736     }
737   if (len == sizeof (lbuf))
738     {
739       result_err = read_memory (memaddr, &lbuf, len);
740       if (result_err)
741         error ("Error reading memory address 0x%x: %s (%d).",
742                memaddr, (result_err < sys_nerr ?
743                          sys_errlist[result_err] :
744                          "uknown error"), result_err);
745       return lbuf;
746     }
747   error ("Cannot handle integers of %d bytes.", len);
748 }
749 \f
750 CORE_ADDR
751 read_pc ()
752 {
753   return (CORE_ADDR) read_register (PC_REGNUM);
754 }
755
756 void
757 write_pc (val)
758      CORE_ADDR val;
759 {
760   write_register (PC_REGNUM, (long) val);
761 #ifdef NPC_REGNUM
762   write_register (NPC_REGNUM, (long) val+4);
763 #endif
764 }
765
766 char *reg_names[] = REGISTER_NAMES;
767
768 static void
769 registers_info (addr_exp)
770      char *addr_exp;
771 {
772   register int i;
773   int regnum;
774
775   if (!have_inferior_p () && !have_core_file_p ())
776     error ("No inferior or core file");
777
778   if (addr_exp)
779     {
780       if (*addr_exp >= '0' && *addr_exp <= '9')
781         regnum = atoi (addr_exp);
782       else
783         {
784           register char *p = addr_exp;
785           if (p[0] == '$')
786             p++;
787           for (regnum = 0; regnum < NUM_REGS; regnum++)
788             if (!strcmp (p, reg_names[regnum]))
789               break;
790           if (regnum == NUM_REGS)
791             error ("%s: invalid register name.", addr_exp);
792         }
793     }
794   else
795     printf_filtered (
796       "Register       Contents (relative to selected stack frame)\n\n");
797
798   for (i = 0; i < NUM_REGS; i++)
799     {
800       unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
801       unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
802       REGISTER_TYPE val;
803
804       if (addr_exp != 0 && i != regnum)
805         continue;
806
807       /* Get the data in raw format, then convert also to virtual format.  */
808       read_relative_register_raw_bytes (i, raw_buffer);
809       REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
810
811       fputs_filtered (reg_names[i], stdout);
812       print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
813
814       /* If virtual format is floating, print it that way.  */
815       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
816           && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
817         val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0,
818                    stdout, 0, 1, 0, Val_pretty_default);
819       /* Else if virtual format is too long for printf,
820          print in hex a byte at a time.  */
821       else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
822         {
823           register int j;
824           printf_filtered ("0x");
825           for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
826             printf_filtered ("%02x", virtual_buffer[j]);
827         }
828       /* Else print as integer in hex and in decimal.  */
829       else
830         {
831           long val;
832
833           bcopy (virtual_buffer, &val, sizeof (long));
834           if (val == 0)
835             printf_filtered ("0");
836           else
837             printf_filtered ("0x%08x  %d", val, val);
838         }
839
840       /* If register has different raw and virtual formats,
841          print the raw format in hex now.  */
842
843       if (REGISTER_CONVERTIBLE (i))
844         {
845           register int j;
846
847           printf_filtered ("  (raw 0x");
848           for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
849             printf_filtered ("%02x", raw_buffer[j]);
850           printf_filtered (")");
851         }
852       printf_filtered ("\n");
853     }
854 }
855 \f
856 #ifdef ATTACH_DETACH
857 #define PROCESS_ATTACH_ALLOWED 1
858 #else
859 #define PROCESS_ATTACH_ALLOWED 0
860 #endif
861 /*
862  * TODO:
863  * Should save/restore the tty state since it might be that the
864  * program to be debugged was started on this tty and it wants
865  * the tty in some state other than what we want.  If it's running
866  * on another terminal or without a terminal, then saving and
867  * restoring the tty state is a harmless no-op.
868  * This only needs to be done if we are attaching to a process.
869  */
870
871 /*
872  * attach_command --
873  * takes a program started up outside of gdb and ``attaches'' to it.
874  * This stops it cold in its tracks and allows us to start tracing it.
875  * For this to work, we must be able to send the process a
876  * signal and we must have the same effective uid as the program.
877  */
878 static void
879 attach_command (args, from_tty)
880      char *args;
881      int from_tty;
882 {
883   char *exec_file;
884   int pid;
885   int remote = 0;
886
887   dont_repeat();
888
889   if (!args)
890     error_no_arg ("process-id or device file to attach");
891
892   while (*args == ' ' || *args == '\t') args++;
893
894   if (args[0] == '/')
895     remote = 1;
896   else
897 #ifndef ATTACH_DETACH
898     error ("Can't attach to a process on this machine.");
899 #else
900     pid = atoi (args);
901 #endif
902
903   if (inferior_pid)
904     {
905       if (query ("A program is being debugged already.  Kill it? "))
906         kill_inferior ();
907       else
908         error ("Inferior not killed.");
909     }
910
911   exec_file = (char *) get_exec_file (1);
912
913   if (from_tty)
914     {
915       if (remote)
916         printf ("Attaching remote machine\n");
917       else
918         printf ("Attaching program: %s pid %d\n",
919                 exec_file, pid);
920       fflush (stdout);
921     }
922
923 #ifdef ATTACH_DETACH
924   if (remote)
925     {
926 #endif
927       remote_open (args, from_tty);
928       start_remote ();
929 #ifdef ATTACH_DETACH
930     }
931   else
932     attach_program (pid);
933 #endif
934 }
935
936 /*
937  * detach_command --
938  * takes a program previously attached to and detaches it.
939  * The program resumes execution and will no longer stop
940  * on signals, etc.  We better not have left any breakpoints
941  * in the program or it'll die when it hits one.  For this
942  * to work, it may be necessary for the process to have been
943  * previously attached.  It *might* work if the program was
944  * started via the normal ptrace (PTRACE_TRACEME).
945  */
946
947 static void
948 detach_command (args, from_tty)
949      char *args;
950      int from_tty;
951 {
952   int signal = 0;
953
954 #ifdef ATTACH_DETACH
955   if (inferior_pid)
956     {
957       if (from_tty)
958         {
959           char *exec_file = (char *)get_exec_file (0);
960           if (exec_file == 0)
961             exec_file = "";
962           printf ("Detaching program: %s pid %d\n",
963                   exec_file, inferior_pid);
964           fflush (stdout);
965         }
966       if (args)
967         signal = atoi (args);
968       
969       detach (signal);
970       inferior_pid = 0;
971     }
972   else
973 #endif
974     {
975       if (!remote_debugging)
976         error ("Not currently attached to subsidiary or remote process.");
977
978       if (args)
979         error ("Argument given to \"detach\" when remotely debugging.");
980       
981       remote_close (from_tty);
982     }
983 }
984
985 /* ARGUSUED */
986 static void
987 float_info (addr_exp)
988      char *addr_exp;
989 {
990 #ifdef FLOAT_INFO
991         FLOAT_INFO;
992 #else
993         printf ("No floating point info available for this processor.\n");
994 #endif
995 }
996 \f
997 extern struct cmd_list_element *setlist, *deletelist;
998
999 void
1000 _initialize_infcmd ()
1001 {
1002   add_com ("tty", class_run, tty_command,
1003            "Set terminal for future runs of program being debugged.");
1004
1005   add_cmd ("args", class_run, set_args_command,
1006            "Specify arguments to give program being debugged when it is started.\n\
1007 Follow this command with any number of args, to be passed to the program.",
1008            &setlist);
1009
1010   add_info ("environment", environment_info,
1011             "The environment to give the program, or one variable's value.\n\
1012 With an argument VAR, prints the value of environment variable VAR to\n\
1013 give the program being debugged.  With no arguments, prints the entire\n\
1014 environment to be given to the program.");
1015
1016   add_cmd ("environment", class_run, unset_environment_command,
1017            "Cancel environment variable VAR for the program.\n\
1018 This does not affect the program until the next \"run\" command.",
1019            &deletelist);
1020
1021   add_cmd ("environment", class_run, set_environment_command,
1022            "Set environment variable value to give the program.\n\
1023 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
1024 VALUES of environment variables are uninterpreted strings.\n\
1025 This does not affect the program until the next \"run\" command.",
1026            &setlist);
1027  
1028 #ifdef ATTACH_DETACH
1029  add_com ("attach", class_run, attach_command,
1030            "Attach to a process that was started up outside of GDB.\n\
1031 This command may take as argument a process id or a device file.\n\
1032 For a process id, you must have permission to send the process a signal,\n\
1033 and it must have the same effective uid as the debugger.\n\
1034 For a device file, the file must be a connection to a remote debug server.\n\n\
1035 Before using \"attach\", you must use the \"exec-file\" command\n\
1036 to specify the program running in the process,\n\
1037 and the \"symbol-file\" command to load its symbol table.");
1038 #else
1039  add_com ("attach", class_run, attach_command,
1040            "Attach to a process that was started up outside of GDB.\n\
1041 This commands takes as an argument the name of a device file.\n\
1042 This file must be a connection to a remote debug server.\n\n\
1043 Before using \"attach\", you must use the \"exec-file\" command\n\
1044 to specify the program running in the process,\n\
1045 and the \"symbol-file\" command to load its symbol table.");
1046 #endif
1047   add_com ("detach", class_run, detach_command,
1048            "Detach the process previously attached.\n\
1049 The process is no longer traced and continues its execution.");
1050
1051   add_com ("signal", class_run, signal_command,
1052            "Continue program giving it signal number SIGNUMBER.");
1053
1054   add_com ("stepi", class_run, stepi_command,
1055            "Step one instruction exactly.\n\
1056 Argument N means do this N times (or till program stops for another reason).");
1057   add_com_alias ("si", "stepi", class_alias, 0);
1058
1059   add_com ("nexti", class_run, nexti_command,
1060            "Step one instruction, but proceed through subroutine calls.\n\
1061 Argument N means do this N times (or till program stops for another reason).");
1062   add_com_alias ("ni", "nexti", class_alias, 0);
1063
1064   add_com ("finish", class_run, finish_command,
1065            "Execute until selected stack frame returns.\n\
1066 Upon return, the value returned is printed and put in the value history.");
1067
1068   add_com ("next", class_run, next_command,
1069            "Step program, proceeding through subroutine calls.\n\
1070 Like the \"step\" command as long as subroutine calls do not happen;\n\
1071 when they do, the call is treated as one instruction.\n\
1072 Argument N means do this N times (or till program stops for another reason).");
1073   add_com_alias ("n", "next", class_run, 1);
1074
1075   add_com ("step", class_run, step_command,
1076            "Step program until it reaches a different source line.\n\
1077 Argument N means do this N times (or till program stops for another reason).");
1078   add_com_alias ("s", "step", class_run, 1);
1079
1080   add_com ("until", class_run, until_command,
1081            "Execute until the program reaches a source line greater than the current\n\
1082 or a specified line or address or function (same args as break command).\n\
1083 Execution will also stop upon exit from the current stack frame.");
1084   add_com_alias ("u", "until", class_run, 1);
1085   
1086   add_com ("jump", class_run, jump_command,
1087            "Continue program being debugged at specified line or address.\n\
1088 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
1089 for an address to start at.");
1090
1091   add_com ("cont", class_run, cont_command,
1092            "Continue program being debugged, after signal or breakpoint.\n\
1093 If proceeding from breakpoint, a number N may be used as an argument:\n\
1094 then the same breakpoint won't break until the Nth time it is reached.");
1095   add_com_alias ("c", "cont", class_run, 1);
1096
1097   add_com ("run", class_run, run_command,
1098            "Start debugged program.  You may specify arguments to give it.\n\
1099 Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
1100 Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
1101 With no arguments, uses arguments last specified (with \"run\" or \"set args\".\n\
1102 To cancel previous arguments and run with no arguments,\n\
1103 use \"set args\" without arguments.");
1104   add_com_alias ("r", "run", class_run, 1);
1105
1106   add_info ("registers", registers_info,
1107             "List of registers and their contents, for selected stack frame.\n\
1108 Register name as argument means describe only that register.");
1109
1110   add_info ("program", program_info,
1111             "Execution status of the program.");
1112
1113   add_info ("float", float_info,
1114             "Print the status of the floating point unit\n");
1115
1116   inferior_args = savestring (" ", 1); /* By default, no args.  */
1117   inferior_environ = make_environ ();
1118   init_environ (inferior_environ);
1119 }
1120
This page took 0.089826 seconds and 4 git commands to generate.