Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working gdb-server #1

Merged
18 commits merged into from
Feb 27, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Martin Capitanio <m@capitanio.org>
Spencer Oliver <spen@spen-soft.co.uk>
Le Mentec Fabien <fabien.lementec@gmail.com>
Peter Zotov <whitequark@whitequark.org>
50 changes: 46 additions & 4 deletions README
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
cd build
make
HOWTO
=====

edit Makefile
make run
To run the gdb server, do (you do not need sudo if you have set up
permissions correctly):
$ make -C build && sudo ./build/st-util 1234 /dev/sg1

Then, in gdb:
(gdb) target remote :1234

Have fun!

Running programs from SRAM
==========================

You can run your firmware directly from SRAM if you want to. Just link
it at 0x20000000 and do
(gdb) load firmware.elf

It will be loaded, and pc will be adjusted to point to start of the
code, if it is linked correctly (i.e. ELF has correct entry point).

Writing to flash
================

The GDB stub ships with a correct memory map, including the flash area.
If you would link your executable to 0x08000000 and then do
(gdb) load firmware.elf
then it would be written to the memory.

FAQ
===

Q: My breakpoints do not work at all or only work once.

A: Optimizations can cause severe instruction reordering. For example,
if you are doing something like `REG = 0x100;' in a loop, the code may
be split into two parts: loading 0x100 into some intermediate register
and moving that value to REG. When you set up a breakpoint, GDB will
hook to the first instruction, which may be called only once if there are
enough unused registers. In my experience, -O3 causes that frequently.

Q: At some point I use GDB command `next', and it hangs.

A: Sometimes when you will try to use GDB `next' command to skip a loop,
it will use a rather inefficient single-stepping way of doing that.
Set up a breakpoint manually in that case and do `continue'.
3 changes: 3 additions & 0 deletions build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.o
*.d
st-util
19 changes: 7 additions & 12 deletions build/Makefile
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
PRG := stlink-access-test
DEV := /dev/sg2
PRG := st-util
DEBUG := #-DDEBUG

all: $(PRG)

LIBS := \
-lsgutils2

OBJS += \
stlink-access-test.o
stlink-hw.o gdb-remote.o gdb-server.o

$(PRG): $(OBJS)
@echo 'Invoking: GCC C Linker'
gcc -o$(PRG) $(OBJS) $(LIBS)
gcc -o $(PRG) $(OBJS) $(LIBS)

%.o: ../src/%.c
@echo 'Building file: $<'
gcc -O0 -g3 -Wall -c -fmessage-length=0 -std=gnu99 -MMD -MP \
gcc -O3 -g3 -Wall -Werror -c -std=gnu99 -MMD -MP \
-fno-strict-aliasing -Wno-unused $(DEBUG) \
-MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)"\
-o"$@" "$<"
-o "$@" "$<"

clean:
@rm -vf *.d *.o $(PRG)

run: all
cp $(PRG) /tmp/
sudo /tmp/$(PRG) $(DEV)
156 changes: 156 additions & 0 deletions src/gdb-remote.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/* -*- tab-width:8 -*- */

/*
Copyright (C) 2011 Peter Zotov <whitequark@whitequark.org>
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/poll.h>

static const char hex[] = "0123456789abcdef";

int gdb_send_packet(int fd, char* data) {
unsigned length = strlen(data) + 5;
char* packet = malloc(length); /* '$' data (hex) '#' cksum (hex) */

memset(packet, 0, length);

packet[0] = '$';

uint8_t cksum = 0;
for(int i = 0; i < strlen(data); i++) {
packet[i + 1] = data[i];
cksum += data[i];
}

packet[length - 4] = '#';
packet[length - 3] = hex[cksum >> 4];
packet[length - 2] = hex[cksum & 0xf];

while(1) {
if(write(fd, packet, length) != length) {
free(packet);
return -2;
}

char ack;
if(read(fd, &ack, 1) != 1) {
free(packet);
return -2;
}

if(ack == '+') {
free(packet);
return 0;
}
}
}

#define ALLOC_STEP 1024

int gdb_recv_packet(int fd, char** buffer) {
unsigned packet_size = ALLOC_STEP + 1, packet_idx = 0;
uint8_t cksum = 0;
char recv_cksum[3] = {0};
char* packet_buffer = malloc(packet_size);
unsigned state;

start:
state = 0;
/*
* 0: waiting $
* 1: data, waiting #
* 2: cksum 1
* 3: cksum 2
* 4: fin
*/

char c;
while(state != 4) {
if(read(fd, &c, 1) != 1) {
return -2;
}

switch(state) {
case 0:
if(c != '$') {
// ignore
} else {
state = 1;
}
break;

case 1:
if(c == '#') {
state = 2;
} else {
packet_buffer[packet_idx++] = c;
cksum += c;

if(packet_idx == packet_size) {
packet_size += ALLOC_STEP;
packet_buffer = realloc(packet_buffer, packet_size);
}
}
break;

case 2:
recv_cksum[0] = c;
state = 3;
break;

case 3:
recv_cksum[1] = c;
state = 4;
break;
}
}

uint8_t recv_cksum_int = strtoul(recv_cksum, NULL, 16);
if(recv_cksum_int != cksum) {
char nack = '-';
if(write(fd, &nack, 1) != 1) {
return -2;
}

goto start;
} else {
char ack = '+';
if(write(fd, &ack, 1) != 1) {
return -2;
}
}

packet_buffer[packet_idx] = 0;
*buffer = packet_buffer;

return packet_size;
}

// Here we skip any characters which are not \x03, GDB interrupt.
// As we use the mode with ACK, in a (very unlikely) situation of a packet
// lost because of this skipping, it will be resent anyway.
int gdb_check_for_interrupt(int fd) {
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN;

if(poll(&pfd, 1, 0) != 0) {
char c;

if(read(fd, &c, 1) != 1)
return -2;

if(c == '\x03') // ^C
return 1;
}

return 0;
}

8 changes: 8 additions & 0 deletions src/gdb-remote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _GDB_REMOTE_H_
#define _GDB_REMOTE_H_

int gdb_send_packet(int fd, char* data);
int gdb_recv_packet(int fd, char** buffer);
int gdb_check_for_interrupt(int fd);

#endif
Loading