Coding and compiling using remote servers

Coding and compiling using remote servers

When working with codebases with larger sizes { say up to 10 Giga Bytes }, we might require a powerful build server and some special tools and practices that could help make our tasks a bit easy. Here we are going to discuss tools like -

  1. git

  2. neovim

  3. cscope along with ctags

  4. ncftp

  5. tmux / screen

  6. few shell commands and scripting

  7. cross-compilation {or compilation on remote servers}


GIT

To share the code or to put code in the review board (for other developers to review), sometimes we require a diff file . A diff file records the changes a user has made, and it could be applied to a repository where those changes are not present so that others could also test them.

applying a patch

To add changes to the repository

patch -p1 < <name_of_patch>

creating a patch

To create a file containing changes done by a user

git diff > <name_of_patch>

undo a commit

git reset HEAD^

Seeing full commit for specific change

git blame <file_name>

take commit-hash in front of the line. Using this commit hash, we can view the full commit made by the user in all files.

git show <commit-hash>

Reset a file in git

git reset <file_name>

Revert a patch

git apply -R <patch>

For viewing changes after doing git add

git diff --staged

To remove from git add

git restore --staged <your_file_name>

neovim

Neovim offers various plugins for coding efficiently.
Some useful plugins-

  1. tpope/vim-commentary - We could comment the code selected in visual mode in vim.

  2. lewis6991/gitsigns.nvim - This plugin offers various features like hopping to hunks , resetting hunks , blaming line and also various colours and symbols are used to display the changes we have added or deleted.

  3. coc.nvim - It gives autocompletion of code based on any language we are using and also displays syntax errors.

using neovim with cscope

We have to add the following line in our bashrc

export EDITOR=nvim
export CSCOPE_EDITOR='/usr/bin/nvim'

If vim opens a small size window

Then quit vim

resize

open vim again

To go to the corresponding bracket in vim

<press_shift> %

To go to starting in line in vim

<press_esc> 0

To go to the end of a line in vim

<press_shift> $

to copy a line in vim

yy

to delete a line in vim

dd

3dd - Delete (cut) three lines, starting from the line where the cursor is positioned

To paste in vim

p

To paste with formatting in vim

:!set paste

While searching in vim, we can use regex

/<something_1>*<something_2>

To select something in vim

<press_shift> *

To enter visual mode

<press_shift> v

To go to the bottom of vim

<press_shift> g

To get hardware info

$ lspci

To go to the function definition in which the current cursor is present (and back)

[[

''

To mount a remote desktop on your pc to open files in your local pc neovim

sshfs <username>@<ip_address>:<path_to_mount> <path_where_it_will_be_mounted>

Path of .gitconfig file (to set the name of committer)

/home/<user_name>/.gitconfig

vimdiff

vimdiff <path_to_file1> <path_to_file2>

Replace a line with 2 lines in vim

Note: In vim \r is treated as newline

:%s/<line_to_be_replaced>/<line_to_be_added_1>\r<line_to_be_added_2>/g

For replacement of a specific line present in a block of code seleted in visual mode

:'<,'>s/<line_to_be_replaced>/<line_to_be_added_1>\r<line_to_be_added_2>/g

To see file path in the airline in neovim

Ctrl + G

For fixing indentation/tab/spaces issue

Enter the following command in neovim

:set list

Select the block of code using the visual mode and enter the following command in neovim

:retab

cscope along with ctags

cscope could be used with various programming languages. cscope helps us with the following queries by taking us to that particular line in the codebase-

  1. Find this C symbol:

  2. Find this global definition:

  3. Find functions called by this function:

  4. Find functions calling this function:

  5. Find this text string:

  6. Change this text string:

  7. Find this egrep pattern:

  8. Find this file:

  9. Find files #including this file: Find assignments to this symbol:

set cscope

find . -name "*.rs" >> cscope.files 
cscope -b -q -k 
ctags -L cscope.files

Alternate for scope

cscope -R 
cscope -dC

ncftp

To copy using ncftp

ncftpput -R -u<username> -p<password> <ip> <destination_path> <source_path>

To copy files to other PC

ncftpget -R -u<username> -p<password> <ip_of_other_pc> <relative_path_of_file_to_be_copied>

screen / tmux

Screen / Tmux - Tmux commands assist in compiling the code after disconnecting from the session. Usually, if we log in to a remote server to execute a command and the session disconnects to a remote server, the command execution stops. To prevent the command execution from stopping, we use screens.

To make a new screen ->tmux new -s <your_screen_name>

To detach from screen -> Ctrl + B + D

To attach to a screen ->

tmux attach-session -t <your_screen_name>

To view your screens

tmux ls

Alternatively, we could use the following commands-

To make new screen

screen -S <your_screen_name>

To detach from the screen -> Ctrl + A + D

To attach to the screen

screen -x <your_screen_name>

To kill a screen => Ctrl + A + K


few shell commands and scripting

Kill process on specific port number

sudo netstat -Ipn | grep :<your_port_number>
kill -9

Command to fix apt

sudo apt upgrade

above output will give a list of packages with unmet dependencies

Remove all of them using the below command

sudo dpkg --purge --force-depends

/etc/fstab

This file is used for mounting the server, containing libraries and cross-compilation tools. We can mount the server using the following command.

sudo mount -a

To find a file

find . -name "<your_file_name>"

To unmount the remote desktop

fusermount -u <path_to_unmount>

To check the pointer to a file

$ ps -ef | grep <daemon_which_is_using_ponter>

root .....

$ cd /proc

$ cd

$ cd fd

$ ls -la

Commands for gdb {use sudo for gdb}

$ ps -ef | grep <daemon_name>

root .....

$ gdb -p <pid>

To insert a breakpoint use following

(gdb) b <function_where_breakpoint_is_to_be_added>

(gdb) b {file_name_with_extension}:{line_number_in_file}

After inserting breakpoint press c to continue

(gdb) c

When the break point is hit we could print the back trace using -

(gdb) bt

To apply back trace for all threads use -

(gdb) thread apply all bt

(gdb) s

(gdb) finish

(gdb) delete

(gdb) n

(Asking gdb to list all functions which could be called using the call command)

(gdb) info functions

(calling a function in gdb with arguments)

(gdb) call ({your_function_return_type})your_function_name(your_function_arguements_values)

(gdb) info registers

(To get all breakpoints)

(gdb) info b

(To delete breakpoint 3)

(gdb) d 3

(To print hexadecimal values)

(gdb) x /100bx {your_pointer_name}

(To print a pointer)

(gdb) p *your_pointer_name

To watch when a variable gets modified

(gdb) watch {your_variable_name}

To make a watch point conditional

(gdb) cond {your_watchpoint_number} {your_variable_name}>={a_value}

To record the terminal screen

script

see time in server

$ date

/<something_1>.*<something_2>

To copy using scp

scp -r <source_username>@<source_ip>:<path_of_source_file_or_directory> <destination_username>@<destination_ip>:<path_where_files_are_to_be_copied>

usage of find and grep

$ grep -roFn '' .

$ find . -name <name_of_file_or_directory>

$ find . -name "name_of_directory" -type d

Script for deleting certain files in different directories

cd /etc/abc/def/;rm -fr ;cd - 
cd /etc/ghi/;rm -fr ;cd - 
cd /usr/jkl/mno/;rm file.txt;cd -

Kill a process

ps -ef | grep <process_name> 
kill -9

Cross-compilation

Suppose we have a clone of our repository in our local PC and our build server {which is used for compilation}. We make changes to code in our local PC; however, for compilation purposes, we must add our changes to the code present in our build server, for we could take a patch {from local pc} and apply it to the server code. We could write the following script for this process.

Note: below is the local PC script

cd <path_to_repository_in_local_pc>

rm -rf <name_of_diff_patch>

git diff > <name_of_diff_patch>

sshpass -p "<passowrd>" scp <name_of_diff_patch> <server_username>@<server_ip>:<path_where_files_are_to_be_copied>

sshpass -p "password" ssh <server_username>@<server_ip> <path_of_server_script>

Note: below is the server script which is called via the last line of the local PC script

cd <path_to_repository_in_server>
git stash
patch -p1 < <name_of_diff_patch>
rm -rf <name_of_diff_patch>

Refernces :

https://stackoverflow.com/a/3099725

http://www.unknownroad.com/rtfm/gdbtut/gdbwatch.html