Showing posts with label Programação. Show all posts
Showing posts with label Programação. Show all posts

Saturday, May 29, 2021

XV6 - Very Simple Unix implementation for educational purposes from MIT

In linux terminal 1

$ sudo apt-get install qemu-kvm qemu virt-manager virt-viewer
$ git clone git@github.com:mit-pdos/xv6-public.git
$ cd xv6-public
$ make
$ make qemu
$ make qemu-nox-gdb

In linux terminal 2

$ gdb kernel
> target remote localhost:26000
> b exec
> c
> file _cat
> b main
> c

In linux terminal 1

$ cat README

look gdb stoped in breakpoint in terminal 2

Monday, March 1, 2021

(Debug) GDB with full colored dashboard

$ pip install pygments

$ wget -P ~ https://git.io/.gdbinit

If gdb version prior to 7.7 and python prior to 2.7 execute this

$ mkdir -p ~/.gdbinit.d/
$ wget 'https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob_plain;f=gdb/python/lib/gdb/FrameDecorator.py' -O ~/.gdbinit.d/FrameDecorator.py
$ sed -i '1s/^/python gdb.COMPLETE_EXPRESSION = gdb.COMPLETE_SYMBOL\n/' .gdbinit
$ sed -i "s/gdb_breakpoint.temporary/None/g" .gdbinit

$ cat >>~/.gdbinit <<EOF
python

import imp
gdb.FrameDecorator = imp.new_module('FrameDecorator')
gdb.FrameDecorator.FrameDecorator = FrameDecorator
end
EOF

You can also send module to be shown on different terminals

start gdb 

$ gdb my_program

in another terminal

$ tty
> /dev/pts/3

in gdb to move Registers to this other terminal 

> dashboard breakpoints -output /dev/pts/3
> dashboard variables -output /dev/pts/3
> dashboard -layout variables stack source breakpoints

to Get help 

>>> help dashboard

Saturday, June 6, 2020

Struct with defined number of bits for each member in C

typedef struct _bitfield {
     unsigned one_bit0:1;
     unsigned one_bit1:1;
     unsigned two_bits:2;
     unsigned four_bits:4;
} BITFIELD;


#include
#include

typedef struct _xpto {
int x;
struct {
int p;
unsigned t:1;
}PT;
char o;

} XPTO;

union {
int result;
char byte[2];
struct {
unsigned b0:1;
unsigned b1:1;
unsigned b2:1;
unsigned b3:1;
unsigned b8:28;
} ST;
} adc;

int main(int argc, char *argv[]) {
XPTO myxpto = { .x=10, .PT = { .p=2, .t=1 }, .o='A'};
printf(".x = %d, .PT { .p = %d, .t = %d }, .o = %c\n",myxpto.x,myxpto.PT.p,myxpto.PT.t,myxpto.o);

adc.byte[0] = 1;
adc.byte[1] = 2;
printf("%x\n", adc.result);
adc.ST.b0 = 0;
printf("%x\n", adc.result);
return 0;
}

Overflow problem - Curious case case of Binary Search

https://thebittheories.com/the-curious-case-of-binary-search-the-famous-bug-that-remained-undetected-for-20-years-973e89fc212

When calculating the mid position in the array

This operation can result in a overflow problem when low + high is too much high

mid = (low + high) / 2;

Replacing by this operation we no longer have the same problem
mid = low + (high - low)/2;


.............................................
Other example of overflow problem
int x = 256;
int y = 256;

unsigned long  result;
result = x * 256 * 256 + y*256*256;
the result of the operation will be calculated with size of the highest operand that is integer, and 256 * 256 is already the maximum value of a integer, multiplied by an integer with value 256, the result will not fit in Integer space.

To fix this, we increase the size of the highest operando
result = x * 256 * 256ul + y*256*256ul;
or
result = ((unsigned long)x) * 256 * 256 + ((unsigned long)y)*256*256;



Blog Archive