OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or1ksim/] [debug/] [gdbcomm.c] - Blame information for rev 1780

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 321 markom
/* gdbcomm.c -- Communication routines for gdb
2
 
3 1744 jeremybenn
   Copyright (C) 2001 by Marko Mlinar, markom@opencores.org
4
   Copyright (C) 2008 Embecosm Limited
5 1748 jeremybenn
 
6 1744 jeremybenn
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
7 1748 jeremybenn
 
8
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
9
 
10 1744 jeremybenn
   This program is free software; you can redistribute it and/or modify it
11
   under the terms of the GNU General Public License as published by the Free
12
   Software Foundation; either version 3 of the License, or (at your option)
13
   any later version.
14 1748 jeremybenn
 
15 1744 jeremybenn
   This program is distributed in the hope that it will be useful, but WITHOUT
16
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18
   more details.
19 1748 jeremybenn
 
20 1744 jeremybenn
   You should have received a copy of the GNU General Public License along
21 1748 jeremybenn
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22 321 markom
 
23 1748 jeremybenn
/* This program is commented throughout in a fashion suitable for processing
24
   with Doxygen. */
25 321 markom
 
26 1744 jeremybenn
 
27 1748 jeremybenn
/* Autoconf and/or portability configuration */
28
#include "config.h"
29
#include "port.h"
30
 
31
/* System includes */
32 321 markom
#include <stdlib.h>
33 1748 jeremybenn
#include <unistd.h>
34 321 markom
#include <stdio.h>
35
#include <sys/poll.h>
36 1751 jeremybenn
#include <sys/types.h>
37
#include <sys/socket.h>
38
#include <netinet/in.h>
39 321 markom
#include <fcntl.h>
40
#include <netdb.h>
41
#include <netinet/tcp.h>
42 1748 jeremybenn
#include <errno.h>
43 321 markom
 
44 1748 jeremybenn
/* Package includes */
45
#include "sim-config.h"
46 321 markom
#include "gdb.h"
47
#include "gdbcomm.h"
48 1748 jeremybenn
#include "debug-unit.h"
49 439 erez
#include "vapi.h"
50 321 markom
 
51 1308 phoenix
 
52 1748 jeremybenn
/* Forward declaration of static functions */
53
static void  jtag_request ();
54
static void  gdb_request (void);
55
static void  protocol_clean (int, int32_t);
56
static int   get_server_socket (const char *name, const char *proto, int port);
57
static int   gdb_read (void *buf, int len);
58
static int   gdb_write (const void *buf, int len);
59 321 markom
 
60 1748 jeremybenn
/* Data structures for this file */
61
static unsigned int  server_ip   = 0;
62
static unsigned int  server_port = 0;
63
static unsigned int  server_fd   = 0;
64
static unsigned int  gdb_fd      = 0;
65
static int           tcp_level   = 0;
66 321 markom
 
67
/* Added by CZ 24/05/01 */
68 1748 jeremybenn
static int
69
get_server_socket (const char *name, const char *proto, int port)
70 321 markom
{
71
  struct servent *service;
72
  struct protoent *protocol;
73
  struct sockaddr_in sa;
74 1748 jeremybenn
  struct hostent *hp;
75 321 markom
  int sockfd;
76
  char myname[256];
77
  int flags;
78
  char sTemp[256];
79 1557 nogj
  socklen_t len;
80 321 markom
 
81
  /* First, get the protocol number of TCP */
82 1748 jeremybenn
  if (!(protocol = getprotobyname (proto)))
83 321 markom
    {
84 1748 jeremybenn
      sprintf (sTemp, "Unable to load protocol \"%s\"", proto);
85
      perror (sTemp);
86 321 markom
      return 0;
87
    }
88 1748 jeremybenn
  tcp_level = protocol->p_proto;        /* Save for later */
89 321 markom
 
90
  /* If we weren't passed a non standard port, get the port
91
     from the services directory. */
92 1748 jeremybenn
  if (!port)
93 321 markom
    {
94 1748 jeremybenn
      if ((service = getservbyname (name, protocol->p_name)))
95
        port = ntohs (service->s_port);
96 321 markom
    }
97 1748 jeremybenn
 
98 321 markom
  /* Create the socket using the TCP protocol */
99 1748 jeremybenn
  if ((sockfd = socket (PF_INET, SOCK_STREAM, protocol->p_proto)) < 0)
100 321 markom
    {
101 1748 jeremybenn
      perror ("Unable to create socket");
102 321 markom
      return 0;
103
    }
104 1748 jeremybenn
 
105 321 markom
  flags = 1;
106 1748 jeremybenn
  if (setsockopt
107
      (sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &flags,
108
       sizeof (int)) < 0)
109 321 markom
    {
110 1748 jeremybenn
      sprintf (sTemp, "Can not set SO_REUSEADDR option on socket %d", sockfd);
111
      perror (sTemp);
112
      close (sockfd);
113 321 markom
      return 0;
114
    }
115
 
116
  /* The server should also be non blocking. Get the current flags. */
117 1751 jeremybenn
  flags = fcntl (sockfd, F_GETFL, flags);
118
  if (flags < 0)
119 321 markom
    {
120 1748 jeremybenn
      sprintf (sTemp, "Unable to get flags for socket %d", sockfd);
121
      perror (sTemp);
122
      close (sockfd);
123 321 markom
      return 0;
124
    }
125
 
126
  /* Set the nonblocking flag */
127 1748 jeremybenn
  if (fcntl (sockfd, F_SETFL, flags | O_NONBLOCK) < 0)
128 321 markom
    {
129 1748 jeremybenn
      sprintf (sTemp, "Unable to set flags for socket %d to value 0x%08x",
130
               sockfd, flags | O_NONBLOCK);
131
      perror (sTemp);
132
      close (sockfd);
133 321 markom
      return 0;
134
    }
135
 
136
  /* Find out what our address is */
137 1748 jeremybenn
  memset (&sa, 0, sizeof (struct sockaddr_in));
138
  gethostname (myname, sizeof (myname));
139
  if (!(hp = gethostbyname (myname)))
140 321 markom
    {
141 1748 jeremybenn
      perror ("Unable to read hostname");
142
      close (sockfd);
143 321 markom
      return 0;
144
    }
145 1748 jeremybenn
 
146 321 markom
  /* Bind our socket to the appropriate address */
147
  sa.sin_family = hp->h_addrtype;
148 1748 jeremybenn
  sa.sin_port = htons (port);
149
  if (bind (sockfd, (struct sockaddr *) &sa, sizeof (struct sockaddr_in)) < 0)
150 321 markom
    {
151 1748 jeremybenn
      sprintf (sTemp, "Unable to bind socket %d to port %d", sockfd, port);
152
      perror (sTemp);
153
      close (sockfd);
154 321 markom
      return 0;
155
    }
156 1748 jeremybenn
  server_ip = sa.sin_addr.s_addr;
157
  len = sizeof (struct sockaddr_in);
158
  if (getsockname (sockfd, (struct sockaddr *) &sa, &len) < 0)
159 321 markom
    {
160 1748 jeremybenn
      sprintf (sTemp, "Unable to get socket information for socket %d",
161
               sockfd);
162
      perror (sTemp);
163
      close (sockfd);
164 321 markom
      return 0;
165
    }
166 1748 jeremybenn
  server_port = ntohs (sa.sin_port);
167 321 markom
 
168
  /* Set the backlog to 1 connections */
169 1748 jeremybenn
  if (listen (sockfd, 1) < 0)
170 321 markom
    {
171 1748 jeremybenn
      sprintf (sTemp, "Unable to set backlog on socket %d to %d", sockfd, 1);
172
      perror (sTemp);
173
      close (sockfd);
174 321 markom
      return 0;
175
    }
176
 
177
  return sockfd;
178
}
179
 
180 1748 jeremybenn
void
181
block_jtag ()
182 321 markom
{
183
  struct pollfd fds[2];
184
  int n = 0;
185
 
186
  fds[n].fd = server_fd;
187
  fds[n].events = POLLIN;
188
  fds[n++].revents = 0;
189 1748 jeremybenn
  if (gdb_fd)
190 321 markom
    {
191
      fds[n].fd = gdb_fd;
192
      fds[n].events = POLLIN;
193
      fds[n++].revents = 0;
194
    }
195 1748 jeremybenn
  poll (fds, n, -1);
196 321 markom
}
197
 
198 1748 jeremybenn
void
199
handle_server_socket (enum boolean block)
200 321 markom
{
201
  struct pollfd fds[3];
202
  int n = 0;
203
  int timeout = block ? -1 : 0;
204 1748 jeremybenn
  enum boolean data_on_stdin = FALSE;
205 321 markom
  int o_serv_fd = server_fd;
206 1748 jeremybenn
 
207
  if (!o_serv_fd && !gdb_fd)
208 321 markom
    return;
209
 
210 1748 jeremybenn
  if (o_serv_fd)
211 321 markom
    {
212
      fds[n].fd = o_serv_fd;
213
      fds[n].events = POLLIN;
214
      fds[n++].revents = 0;
215
    }
216 1748 jeremybenn
  if (gdb_fd)
217 321 markom
    {
218
      fds[n].fd = gdb_fd;
219
      fds[n].events = POLLIN;
220
      fds[n++].revents = 0;
221
    }
222 1748 jeremybenn
  if (block)
223 321 markom
    {
224
      fds[n].fd = 0;
225
      fds[n].events = POLLIN;
226
      fds[n++].revents = 0;
227
    }
228
 
229 1748 jeremybenn
  while (!data_on_stdin)
230 321 markom
    {
231 1748 jeremybenn
      fds[0].revents = 0;
232
      fds[1].revents = 0;
233
      fds[2].revents = 0;
234
      switch (poll (fds, n, timeout))
235
        {
236
        case -1:
237
          if (errno == EINTR)
238
            continue;
239
          perror ("poll");
240
          server_fd = 0;
241
          break;
242
        case 0:          /* Nothing interesting going on */
243
          data_on_stdin = TRUE; /* Can only get here if nonblocking */
244
          break;
245
        default:
246
          /* Make sure to handle the gdb port first! */
247
          if ((fds[0].revents && gdb_fd && !o_serv_fd) ||
248
              (fds[1].revents && server_fd && gdb_fd))
249
            {
250
              int revents = o_serv_fd ? fds[1].revents : fds[0].revents;
251 321 markom
 
252 1748 jeremybenn
              if (revents & POLLIN)
253
                gdb_request ();
254
              else              /* Error Occurred */
255
                {
256
                  fprintf (stderr,
257
                           "Received flags 0x%08x on gdb socket. Shutting down.\n",
258
                           revents);
259
                  close (gdb_fd);
260
                  gdb_fd = 0;
261
                }
262
            }
263
          if (fds[0].revents && o_serv_fd)
264
            {
265
              if (fds[0].revents & POLLIN)
266
                jtag_request ();
267
              else              /* Error Occurred */
268
                {
269
                  fprintf (stderr,
270
                           "Received flags 0x%08x on server. Shutting down.\n",
271
                           fds[0].revents);
272
                  close (o_serv_fd);
273
                  server_fd = 0;
274
                  server_port = 0;
275
                  server_ip = 0;
276
                }
277
            }
278
          if (fds[2].revents || (fds[1].revents && !gdb_fd))
279
            data_on_stdin = TRUE;
280
          break;
281
        }                       /* End of switch statement */
282
    }                           /* End of while statement */
283 321 markom
}
284
 
285 1748 jeremybenn
static void
286
jtag_request (void)
287 321 markom
{
288
  struct sockaddr_in sa;
289 1748 jeremybenn
  struct sockaddr *addr = (struct sockaddr *) &sa;
290
  socklen_t len = sizeof (struct sockaddr_in);
291
  int fd = accept (server_fd, addr, &len);
292
  int on_off = 0;                /* Turn off Nagel's algorithm on the socket */
293 321 markom
  int flags;
294
  char sTemp[256];
295
 
296 1748 jeremybenn
  if (fd < 0)
297 321 markom
    {
298
      /* This is valid, because a connection could have started,
299 479 markom
         and then terminated due to a protocol error or user
300
         initiation before the accept could take place. */
301 1748 jeremybenn
      if (errno != EWOULDBLOCK && errno != EAGAIN)
302
        {
303
          perror ("accept");
304
          close (server_fd);
305
          server_fd = 0;
306
          server_port = 0;
307
          server_ip = 0;
308
        }
309 321 markom
      return;
310
    }
311
 
312 1748 jeremybenn
  if (gdb_fd)
313 321 markom
    {
314 1748 jeremybenn
      close (fd);
315 321 markom
      return;
316
    }
317
 
318 1748 jeremybenn
  if ((flags = fcntl (fd, F_GETFL)) < 0) /* JPB */
319 321 markom
    {
320 1748 jeremybenn
      sprintf (sTemp, "Unable to get flags for gdb socket %d", fd);
321
      perror (sTemp);
322
      close (fd);
323 321 markom
      return;
324
    }
325 1748 jeremybenn
 
326
  if (fcntl (fd, F_SETFL, flags | O_NONBLOCK) < 0)
327 321 markom
    {
328 1748 jeremybenn
      sprintf (sTemp, "Unable to set flags for gdb socket %d to value 0x%08x",
329
               fd, flags | O_NONBLOCK);
330
      perror (sTemp);
331
      close (fd);
332 321 markom
      return;
333
    }
334
 
335 1748 jeremybenn
  if (setsockopt (fd, tcp_level, TCP_NODELAY, &on_off, sizeof (int)) < 0)
336 321 markom
    {
337 1748 jeremybenn
      sprintf (sTemp,
338
               "Unable to disable Nagel's algorithm for socket %d.\nsetsockopt",
339
               fd);
340
      perror (sTemp);
341
      close (fd);
342 321 markom
      return;
343
    }
344
 
345
  gdb_fd = fd;
346
}
347
 
348 1748 jeremybenn
static void
349
gdb_request (void)
350 321 markom
{
351 1748 jeremybenn
  struct jtr_read_message          msg_read;
352
  struct jtr_write_message         msg_write;
353
  struct jtr_read_block_message    msg_bread;
354
  struct jtr_write_block_message  *msg_bwrite;
355
  struct jtr_chain_message         msg_chain;
356
  struct jtr_read_response         resp_read;
357
  struct jtr_write_response        resp_write;
358
  struct jtr_read_block_response  *resp_bread;
359
  struct jtr_write_block_response  resp_bwrite;
360
  struct jtr_chain_response        resp_chain;
361
 
362 321 markom
  char *buf;
363
  int err = 0;
364 1748 jeremybenn
  uint32_t command, length;
365
  int len, i;
366 321 markom
 
367
  /* First, we must read the incomming command */
368 1748 jeremybenn
  if (gdb_read (&command, sizeof (uint32_t)) < 0)
369 321 markom
    {
370 1748 jeremybenn
      if (gdb_fd)
371
        {
372
          perror ("gdb socket - 1");
373
          close (gdb_fd);
374
          gdb_fd = 0;
375
        }
376 321 markom
      return;
377
    }
378 1748 jeremybenn
  if (gdb_read (&length, sizeof (uint32_t)) < 0)
379 321 markom
    {
380 1748 jeremybenn
      if (gdb_fd)
381
        {
382
          perror ("gdb socket - 2");
383
          close (gdb_fd);
384
          gdb_fd = 0;
385
        }
386 321 markom
      return;
387
    }
388 1748 jeremybenn
  length = ntohl (length);
389 321 markom
 
390
  /* Now, verify the protocol and implement the command */
391 1748 jeremybenn
  switch (ntohl (command))
392 321 markom
    {
393 1748 jeremybenn
    case OR1K_JTAG_COMMAND_WRITE:
394
      if (length != sizeof (msg_write) - 8)
395
        {
396
          protocol_clean (length, JTAG_PROXY_PROTOCOL_ERROR);
397
          return;
398
        }
399
      buf = (char *) &msg_write;
400
      if (gdb_read (&buf[8], length) < 0)
401
        {
402
          if (gdb_fd)
403
            {
404
              perror ("gdb socket - 3");
405
              close (gdb_fd);
406
              gdb_fd = 0;
407
            }
408
          return;
409
        }
410
      msg_write.address = ntohl (msg_write.address);
411
      msg_write.data_h = ntohl (msg_write.data_h);
412
      msg_write.data_l = ntohl (msg_write.data_l);
413
      err = debug_set_register (msg_write.address, msg_write.data_l);
414
      resp_write.status = htonl (err);
415
      if (gdb_write (&resp_write, sizeof (resp_write)) < 0)
416
        {
417
          if (gdb_fd)
418
            {
419
              perror ("gdb socket - 4");
420
              close (gdb_fd);
421
              gdb_fd = 0;
422
            }
423
          return;
424
        }
425 321 markom
      break;
426 1748 jeremybenn
    case OR1K_JTAG_COMMAND_READ:
427
      if (length != sizeof (msg_read) - 8)
428
        {
429
          protocol_clean (length, JTAG_PROXY_PROTOCOL_ERROR);
430
          return;
431
        }
432
      buf = (char *) &msg_read;
433
      if (gdb_read (&buf[8], length) < 0)
434
        {
435
          if (gdb_fd)
436
            {
437
              perror ("gdb socket - 5");
438
              close (gdb_fd);
439
              gdb_fd = 0;
440
            }
441
          return;
442
        }
443
      msg_read.address = ntohl (msg_read.address);
444
      err = debug_get_register (msg_read.address, &resp_read.data_l);
445
      resp_read.status = htonl (err);
446
      resp_read.data_h = 0;
447
      resp_read.data_l = htonl (resp_read.data_l);
448
      if (gdb_write (&resp_read, sizeof (resp_read)) < 0)
449
        {
450
          if (gdb_fd)
451
            {
452
              perror ("gdb socket - 6");
453
              close (gdb_fd);
454
              gdb_fd = 0;
455
            }
456
          return;
457
        }
458 321 markom
      break;
459 1748 jeremybenn
    case OR1K_JTAG_COMMAND_WRITE_BLOCK:
460
      if (length < sizeof (struct jtr_write_block_message) - 8)
461
        {
462
          protocol_clean (length, JTAG_PROXY_PROTOCOL_ERROR);
463
          return;
464
        }
465
      if (!(buf = (char *) malloc (8 + length)))
466
        {
467
          protocol_clean (length, JTAG_PROXY_OUT_OF_MEMORY);
468
          return;
469
        }
470
      msg_bwrite = (struct jtr_write_block_message *) buf;
471
      if (gdb_read (&buf[8], length) < 0)
472
        {
473
          if (gdb_fd)
474
            {
475
              perror ("gdb socket - 5");
476
              close (gdb_fd);
477
              gdb_fd = 0;
478
            }
479
          free (buf);
480
          return;
481
        }
482
      msg_bwrite->address = ntohl (msg_bwrite->address);
483
      msg_bwrite->num_regs = ntohl (msg_bwrite->num_regs);
484
      for (i = 0; i < msg_bwrite->num_regs; i++)
485
        {
486
          int t_err = 0;
487 321 markom
 
488 1748 jeremybenn
          msg_bwrite->data[i] = ntohl (msg_bwrite->data[i]);
489
          t_err =
490
            debug_set_register (msg_bwrite->address + 4 * i,
491
                                msg_bwrite->data[i]);
492
          err = err ? err : t_err;
493
        }
494
      resp_bwrite.status = htonl (err);
495
      free (buf);
496 321 markom
      buf = NULL;
497
      msg_bwrite = NULL;
498 1748 jeremybenn
      if (gdb_write (&resp_bwrite, sizeof (resp_bwrite)) < 0)
499
        {
500
          if (gdb_fd)
501
            {
502
              perror ("gdb socket - 4");
503
              close (gdb_fd);
504
              gdb_fd = 0;
505
            }
506
          return;
507
        }
508 321 markom
      break;
509 1748 jeremybenn
    case OR1K_JTAG_COMMAND_READ_BLOCK:
510
      if (length != sizeof (msg_bread) - 8)
511
        {
512
          protocol_clean (length, JTAG_PROXY_PROTOCOL_ERROR);
513
          return;
514
        }
515
      buf = (char *) &msg_bread;
516
      if (gdb_read (&buf[8], length) < 0)
517
        {
518
          if (gdb_fd)
519
            {
520
              perror ("gdb socket - 5");
521
              close (gdb_fd);
522
              gdb_fd = 0;
523
            }
524
          return;
525
        }
526
      msg_bread.address = ntohl (msg_bread.address);
527
      msg_bread.num_regs = ntohl (msg_bread.num_regs);
528
      len =
529
        sizeof (struct jtr_read_block_response) + 4 * (msg_bread.num_regs - 1);
530
      if (!(buf = (char *) malloc (len)))
531
        {
532
          protocol_clean (0, JTAG_PROXY_OUT_OF_MEMORY);
533
          return;
534
        }
535
      resp_bread = (struct jtr_read_block_response *) buf;
536
      for (i = 0; i < msg_bread.num_regs; i++)
537
        {
538
          int t_err;
539 321 markom
 
540 1748 jeremybenn
          t_err =
541
            debug_get_register (msg_bread.address + 4 * i,
542
                                &resp_bread->data[i]);
543
          resp_bread->data[i] = htonl (resp_bread->data[i]);
544
          err = err ? err : t_err;
545
        }
546
      resp_bread->status = htonl (err);
547
      resp_bread->num_regs = htonl (msg_bread.num_regs);
548
      if (gdb_write (resp_bread, len) < 0)
549
        {
550
          if (gdb_fd)
551
            {
552
              perror ("gdb socket - 6");
553
              close (gdb_fd);
554
              gdb_fd = 0;
555
            }
556
          free (buf);
557
          return;
558
        }
559
      free (buf);
560 321 markom
      buf = NULL;
561
      resp_bread = NULL;
562
      break;
563 1748 jeremybenn
    case OR1K_JTAG_COMMAND_CHAIN:
564
      if (length != sizeof (msg_chain) - 8)
565
        {
566
          protocol_clean (length, JTAG_PROXY_PROTOCOL_ERROR);
567
          return;
568
        }
569
      buf = (char *) &msg_chain;
570
      if (gdb_read (&buf[8], sizeof (msg_chain) - 8) < 0)
571
        {
572
          if (gdb_fd)
573
            {
574
              perror ("gdb socket - 7");
575
              close (gdb_fd);
576
              gdb_fd = 0;
577
            }
578
          return;
579
        }
580
      msg_chain.chain = htonl (msg_chain.chain);
581
      err = debug_set_chain (msg_chain.chain);
582
      resp_chain.status = htonl (err);
583
      if (gdb_write (&resp_chain, sizeof (resp_chain)) < 0)
584
        {
585
          if (gdb_fd)
586
            {
587
              perror ("gdb socket - 8");
588
              close (gdb_fd);
589
              gdb_fd = 0;
590
            }
591
          return;
592
        }
593 321 markom
      break;
594
    default:
595 1748 jeremybenn
      protocol_clean (length, JTAG_PROXY_COMMAND_NOT_IMPLEMENTED);
596 321 markom
      break;
597
    }
598
}
599
 
600 1748 jeremybenn
static void
601
protocol_clean (int length, int32_t err)
602 321 markom
{
603
  char buf[4096];
604
 
605 1748 jeremybenn
  err = htonl (err);
606
  if ((gdb_read (buf, length) < 0) ||
607
      ((gdb_write (&err, sizeof (err)) < 0) && gdb_fd))
608 321 markom
    {
609 1748 jeremybenn
      perror ("gdb socket - 9");
610
      close (gdb_fd);
611 321 markom
      gdb_fd = 0;
612
    }
613
}
614
 
615 1748 jeremybenn
static int
616
gdb_write (const void *buf, int len)
617 321 markom
{
618 439 erez
  int n, log_n = 0;
619 1748 jeremybenn
  const char *w_buf = (const char *) buf;
620
  const uint32_t *log_buf = (const uint32_t *) buf;
621 321 markom
  struct pollfd block;
622
 
623 1748 jeremybenn
  while (len)
624
    {
625
      if ((n = write (gdb_fd, w_buf, len)) < 0)
626
        {
627
          switch (errno)
628
            {
629
            case EWOULDBLOCK:   /* or EAGAIN */
630
              /* We've been called on a descriptor marked
631
                 for nonblocking I/O. We better simulate
632
                 blocking behavior. */
633
              block.fd = gdb_fd;
634
              block.events = POLLOUT;
635
              block.revents = 0;
636
              poll (&block, 1, -1);
637
              continue;
638
            case EINTR:
639
              continue;
640
            case EPIPE:
641
              close (gdb_fd);
642
              gdb_fd = 0;
643
              return -1;
644
            default:
645
              return -1;
646
            }
647
        }
648
      else
649
        {
650
          len -= n;
651
          w_buf += n;
652
          if (config.debug.vapi_id)
653
            for (log_n += n; log_n >= 4; log_n -= 4, ++log_buf)
654
              vapi_write_log_file (VAPI_COMMAND_SEND, config.debug.vapi_id,
655
                                   ntohl (*log_buf));
656
        }
657 321 markom
    }
658
  return 0;
659
}
660
 
661 1748 jeremybenn
static int
662
gdb_read (void *buf, int len)
663 321 markom
{
664 439 erez
  int n, log_n = 0;
665 1748 jeremybenn
  char *r_buf = (char *) buf;
666
  uint32_t *log_buf = (uint32_t *) buf;
667 321 markom
  struct pollfd block;
668
 
669 1748 jeremybenn
  while (len)
670
    {
671
      if ((n = read (gdb_fd, r_buf, len)) < 0)
672
        {
673
          switch (errno)
674
            {
675
            case EWOULDBLOCK:   /* or EAGAIN */
676
              /* We've been called on a descriptor marked
677
                 for nonblocking I/O. We better simulate
678
                 blocking behavior. */
679
              block.fd = gdb_fd;
680
              block.events = POLLIN;
681
              block.revents = 0;
682
              poll (&block, 1, -1);
683
              continue;
684
            case EINTR:
685
              continue;
686
            default:
687
              return -1;
688
            }
689
        }
690
      else if (n == 0)
691
        {
692
          close (gdb_fd);
693
          gdb_fd = 0;
694
          return -1;
695
        }
696
      else
697
        {
698
          len -= n;
699
          r_buf += n;
700
          if (config.debug.vapi_id)
701
            for (log_n += n; log_n >= 4; log_n -= 4, ++log_buf)
702
              vapi_write_log_file (VAPI_COMMAND_REQUEST, config.debug.vapi_id,
703
                                   ntohl (*log_buf));
704
        }
705 321 markom
    }
706
  return 0;
707
}
708
 
709 1748 jeremybenn
void
710
gdbcomm_init ()
711 321 markom
{
712 1748 jeremybenn
  server_port = config.debug.server_port;
713
  if ((server_fd = get_server_socket ("or1ksim", "tcp", server_port)))
714
    PRINTF ("JTAG Proxy server started on port %d\n", server_port);
715 479 markom
  else
716 1748 jeremybenn
    PRINTF ("Cannot start JTAG proxy server on port %d\n", server_port);
717 321 markom
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.