working on "duff" example led me to futzing with gdb, found out the gdb tutorial is kinda (a lot) outdated
This commit is contained in:
parent
b95730410b
commit
2f315a6353
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
gowrikumar/bin
|
gowrikumar/bin
|
||||||
|
gdbtut/bin
|
||||||
*.i
|
*.i
|
||||||
*.s
|
*.s
|
||||||
|
4
Makefile
4
Makefile
@ -2,16 +2,18 @@ CD = cd
|
|||||||
RM = rm -v
|
RM = rm -v
|
||||||
|
|
||||||
CC := gcc
|
CC := gcc
|
||||||
CFLAGS := -std=c99 -Wall
|
CFLAGS := -std=c99 -Wall -g
|
||||||
|
|
||||||
export CD RM CFLAGS
|
export CD RM CFLAGS
|
||||||
|
|
||||||
|
|
||||||
all:
|
all:
|
||||||
$(CD) gowrikumar && $(MAKE)
|
$(CD) gowrikumar && $(MAKE)
|
||||||
|
$(CD) gdbtut && $(MAKE)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(CD) gowrikumar && $(MAKE) clean
|
$(CD) gowrikumar && $(MAKE) clean
|
||||||
|
$(CD) gdbtut && $(MAKE) clean
|
||||||
|
|
||||||
|
|
||||||
.PHONY: all clean
|
.PHONY: all clean
|
||||||
|
14
gdbtut/Makefile
Normal file
14
gdbtut/Makefile
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# tutorial exercises from http://www.unknownroad.com/rtfm/gdbtut/
|
||||||
|
|
||||||
|
BINDIR := $(PWD)/bin
|
||||||
|
export BINDIR
|
||||||
|
|
||||||
|
|
||||||
|
all:
|
||||||
|
$(CD) src && $(MAKE)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) $(BINDIR)/*
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: all clean
|
0
gdbtut/bin/.keep
Normal file
0
gdbtut/bin/.keep
Normal file
12
gdbtut/src/Makefile
Normal file
12
gdbtut/src/Makefile
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# tutorial exercises from http://www.unknownroad.com/rtfm/gdbtut/
|
||||||
|
|
||||||
|
ALL_BIN := $(patsubst %.c,$(BINDIR)/%,$(wildcard *.c))
|
||||||
|
|
||||||
|
|
||||||
|
$(BINDIR)/%: %.c
|
||||||
|
$(CC) $(CFLAGS) -o $@ $<
|
||||||
|
|
||||||
|
all: $(ALL_BIN)
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: all
|
28
gdbtut/src/segfault.c
Normal file
28
gdbtut/src/segfault.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/**
|
||||||
|
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
char *buf;
|
||||||
|
long long huge = 8000000000000000000;
|
||||||
|
/* Okay, so this is *not* going to segfault because
|
||||||
|
* the way memory is allocated has changed since
|
||||||
|
* the tutorial was written. The segfault is supposed
|
||||||
|
* to happen when more memory is allocated than is
|
||||||
|
* available on the machine. So much for that exercise.
|
||||||
|
*/
|
||||||
|
|
||||||
|
buf = malloc(huge);
|
||||||
|
|
||||||
|
fgets(buf, 1024, stdin);
|
||||||
|
printf("%s\n", buf);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim:filetype=c:fileencoding=utf-8
|
||||||
|
*/
|
37
gowrikumar/src/11-duff.c
Normal file
37
gowrikumar/src/11-duff.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void duff(register char *to, register char *from, register int count)
|
||||||
|
{
|
||||||
|
register int n = (count + 7) / 8;
|
||||||
|
switch (count % 8) {
|
||||||
|
case 0: do { *to++ = *from++;
|
||||||
|
case 7: *to++ = *from++;
|
||||||
|
case 6: *to++ = *from++;
|
||||||
|
case 5: *to++ = *from++;
|
||||||
|
case 4: *to++ = *from++;
|
||||||
|
case 3: *to++ = *from++;
|
||||||
|
case 2: *to++ = *from++;
|
||||||
|
case 1: *to++ = *from++;
|
||||||
|
} while (--n > 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char * to = "dogs cats babies";
|
||||||
|
char * from = "monkey hat pants";
|
||||||
|
int i = 16;
|
||||||
|
printf("to = %s\n", to);
|
||||||
|
printf("from = %s\n", from);
|
||||||
|
/* And here comes the segfault... */
|
||||||
|
duff(to, from, i);
|
||||||
|
printf("to = %s\n", to);
|
||||||
|
printf("from = %s\n", from);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim:filetype=c:fileencoding=utf-8
|
||||||
|
*/
|
40
gowrikumar/src/11b-duff.c
Normal file
40
gowrikumar/src/11b-duff.c
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void duff(register char *to, register char *from, register int count)
|
||||||
|
{
|
||||||
|
register int n;
|
||||||
|
|
||||||
|
int remainder = count % 8;
|
||||||
|
if (remainder == 0) {
|
||||||
|
n = (count + 7) / 8;
|
||||||
|
do {
|
||||||
|
*to++ = *from++;
|
||||||
|
} while (--n > 0);
|
||||||
|
} else if (remainder <= 7 && remainder > 0) {
|
||||||
|
int i;
|
||||||
|
int c;
|
||||||
|
for (i = 0; (c = from[i] && c != EOF); i++) {
|
||||||
|
to[i] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char * to = "dogs cats babies";
|
||||||
|
char * from = "monkey hat pants";
|
||||||
|
int i = 11;
|
||||||
|
printf("to = %s\n", to);
|
||||||
|
printf("from = %s\n", from);
|
||||||
|
/* And here comes the segfault... */
|
||||||
|
duff(to, from, i);
|
||||||
|
printf("to = %s\n", to);
|
||||||
|
printf("from = %s\n", from);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim:filetype=c:fileencoding=utf-8
|
||||||
|
*/
|
Loading…
Reference in New Issue
Block a user