Initial Commit
[simavr] / simavr / sim / sim_gdb.c
1 /*
2         sim_gdb.c
3
4         Placeholder!
5
6         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
7
8         This file is part of simavr.
9
10         simavr is free software: you can redistribute it and/or modify
11         it under the terms of the GNU General Public License as published by
12         the Free Software Foundation, either version 3 of the License, or
13         (at your option) any later version.
14
15         simavr is distributed in the hope that it will be useful,
16         but WITHOUT ANY WARRANTY; without even the implied warranty of
17         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18         GNU General Public License for more details.
19
20         You should have received a copy of the GNU General Public License
21         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include <netinet/in.h>
25 #include <netinet/tcp.h>
26 #include <arpa/inet.h>
27 #include <sys/socket.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <poll.h>
34 #include "simavr.h"
35
36 typedef struct avr_gdb_t {
37         avr_t * avr;
38         int             sock;
39 } avr_gdb_t;
40
41 int avr_gdb_init(avr_t * avr)
42 {
43         avr_gdb_t * g = malloc(sizeof(avr_gdb_t));
44         memset(g, 0, sizeof(avr_gdb_t));
45
46         avr->gdb = NULL;
47
48         if ((g->sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
49                 fprintf(stderr, "Can't create socket: %s", strerror(errno));
50                 return -1;
51         }
52
53         int i = 1;
54         setsockopt(g->sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
55
56         struct sockaddr_in address = { 0 };
57         address.sin_family = AF_INET;
58         address.sin_port = htons (1234);
59
60         if (bind(g->sock, (struct sockaddr *) &address, sizeof(address))) {
61                 fprintf(stderr, "Can not bind socket: %s", strerror(errno));
62                 return -1;
63         }
64         avr->gdb = g;
65         return 0;
66 }