Update years in copyright notice for the GDB files.
[deliverable/binutils-gdb.git] / sim / arm / main.c
1 /* main.c -- top level of ARMulator: ARM6 Instruction Emulator.
2 Copyright (C) 1994 Advanced RISC Machines Ltd.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>. */
16
17 /**********************************************************************/
18 /* Forks the ARMulator and hangs on a socket passing on RDP messages */
19 /* down a pipe to the ARMulator which translates them into RDI calls. */
20 /**********************************************************************/
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <signal.h>
28 #include <netdb.h>
29 #include <unistd.h>
30
31 #include "armdefs.h"
32 #include "dbg_rdi.h"
33 #include "dbg_conf.h"
34
35 #define MAXHOSTNAMELENGTH 64
36
37 /* Read and write routines down sockets and pipes */
38
39 void MYread_chars (int sock, void *p, int n);
40 unsigned char MYread_char (int sock);
41 ARMword MYread_word (int sock);
42 void MYread_FPword (int sock, char *putinhere);
43
44 void MYwrite_word (int sock, ARMword i);
45 void MYwrite_string (int sock, char *s);
46 void MYwrite_FPword (int sock, char *fromhere);
47 void MYwrite_char (int sock, unsigned char c);
48
49 void passon (int source, int dest, int n);
50
51
52 /* Mother and child processes */
53 void parent (void);
54 void kid (void);
55
56 /* The child process id. */
57 pid_t child;
58
59 /* The socket to the debugger */
60 int debugsock;
61
62 /* The pipes between the two processes */
63 int mumkid[2];
64 int kidmum[2];
65
66 /* A pipe for handling SWI return values that goes straight from the */
67 /* parent to the ARMulator host interface, bypassing the childs RDP */
68 /* to RDI interpreter */
69 int DebuggerARMul[2];
70
71 /* The maximum number of file descriptors */
72 int nfds;
73
74 /* The socket handle */
75 int sockethandle;
76
77 /* The machine name */
78 char localhost[MAXHOSTNAMELENGTH + 1];
79
80 /* The socket number */
81 unsigned int socketnumber;
82
83 /**************************************************************/
84 /* Takes one argument: the socket number. */
85 /* Opens a socket to the debugger, and once opened spawns the */
86 /* ARMulator and sets up a couple of pipes. */
87 /**************************************************************/
88 int
89 main (int argc, char *argv[])
90 {
91 int i;
92 struct sockaddr_in devil, isa;
93 struct hostent *hp;
94
95
96 if (argc == 1)
97 {
98 fprintf (stderr, "No socket number\n");
99 return 1;
100 }
101
102 sscanf (argv[1], "%d", &socketnumber);
103 if (!socketnumber || socketnumber > 0xffff)
104 {
105 fprintf (stderr, "Invalid socket number: %d\n", socketnumber);
106 return 1;
107 }
108
109 gethostname (localhost, MAXHOSTNAMELENGTH);
110 hp = gethostbyname (localhost);
111 if (!hp)
112 {
113 fprintf (stderr, "Cannot get local host info\n");
114 return 1;
115 }
116
117 /* Open a socket */
118 sockethandle = socket (hp->h_addrtype, SOCK_STREAM, 0);
119 if (sockethandle == -1)
120 {
121 perror ("socket");
122 return 1;
123 }
124
125 devil.sin_family = hp->h_addrtype;
126 devil.sin_port = htons (socketnumber);
127 devil.sin_addr.s_addr = 0;
128 for (i = 0; i < sizeof (devil.sin_zero); i++)
129 devil.sin_zero[i] = '\000';
130 memcpy (&devil.sin_addr, hp->h_addr_list[0], hp->h_length);
131
132 if (bind (sockethandle, &devil, sizeof (devil)) < 0)
133 {
134 perror ("bind");
135 return 1;
136 }
137
138 /* May only accept one debugger at once */
139
140 if (listen (sockethandle, 0))
141 {
142 perror ("listen");
143 return 1;
144 }
145
146 fprintf (stderr, "Waiting for connection from debugger...");
147
148 debugsock = accept (sockethandle, &isa, &i);
149 if (debugsock == -1)
150 {
151 perror ("accept");
152 return 1;
153 }
154
155 fprintf (stderr, " done.\nConnection Established.\n");
156
157 nfds = getdtablesize ();
158
159 if (pipe (mumkid))
160 {
161 perror ("pipe");
162 return 1;
163 }
164 if (pipe (kidmum))
165 {
166 perror ("pipe");
167 return 1;
168 }
169
170 if (pipe (DebuggerARMul))
171 {
172 perror ("pipe");
173 return 1;
174 }
175
176 #ifdef DEBUG
177 fprintf (stderr, "Created pipes ok\n");
178 #endif
179
180 child = fork ();
181
182 #ifdef DEBUG
183 fprintf (stderr, "fork() ok\n");
184 #endif
185
186 if (child == 0)
187 kid ();
188 if (child != -1)
189 parent ();
190
191 perror ("fork");
192 return 1;
193 }
This page took 0.034805 seconds and 4 git commands to generate.