Wednesday, April 27, 2022

Learn CPP

 This post captures few good materials on cpp

1) This blog has been the best introduction to C++ for me.  I still go back and reference it all the time.  The author keeps the contents updated with recent versions of C++ which is great:

https://www.learncpp.com/

2) cppCon 2017 Bjarne Stroustrup "Learning and teaching modern c++"


3) CppCon started doing a lot of “Back to Basics” talks and I think it’s nice to check them from time to time even if you are an experienced programmer.

4) A Tour of C++ by Bjarne Stroustrup comes to mind. A fairly straightforward C++ intro. I liked it was short (256 pages) and written by the language main designer.

5) cppCon 2015 "Stop teaching c"



Sunday, May 9, 2021

Skeleton of a platform driver with fops

 
struct dummy_drvdata {
struct cdev dummy_cdev;
struct device *dev;
};

static size_t dummy_read(struct file *file, char _user *data, size_t len, lofft *ppos)
{
struct dummy_drvdata = file->private_data;
        .......
}

static int dummy_open(struct inode *inode, struct file *file)
{
struct dummy_drvdata *drvdata = container_of(inode->i_cdev, 
                                                                struct dummy_drvdata, dummy_cdev);
file->private_data = drvdata;
return 0;
}

struct file_operations dummy_fops = {
.open = dummy_open,
.read = dummy_read,
};

static int dummy_devices_register(struct dummy_drvdata *drvdata)
{
dev_id dev;

alloc_chrdev_region(&dev, 0, 1, "dummy-devices");
cdev_init(&drv_data->dummy_cdev, dummy_fops);
cdev_add(&drvdata->dummy_cdev, dev, 1);
}

static int dummy_probe(struct platform_device *pdev)
{
struct dummy_drvdata *drvdata = kmalloc();
drvdata->dev = &pdev->dev;

platform_set_drvdata(pdev, drvdata); //needed in dummy_remove function
dummy_devices_register(drvdata);
}

const struct of_device_id dummy_device_id = {
{.compactible = "qcom,dummy-driver"},
{}
}

static struct platform_driver dummy_platform_driver = {
.probe = dummy_probe,
.remove = dummy_remove,
.device_driver = {
.name = "dummy-driver",
.of_match_table = dummy_match_table,
},
};
static int my_module_init(void)
{
platform_driver_register(
}
module_init(dummy_module_init);
module_exit(dummy_module_exit);

Wednesday, April 14, 2021

DS & Algos


Cheat Sheet for cpp and more: https://hackingcpp.com/cpp/cheat_sheets.html

https://github.com/orrsella/soft-eng-interview-prep

DP:

https://leetcode.com/discuss/general-discussion/475924/my-experience-and-notes-for-learning-dp

Quick Recap:

https://leetcode.com/discuss/career/217868/My-notes-for-the-night-before-interview.

Comprehensive Guide:

Comprehensive-data-structure-and-algorithm-study-guide

  Grouping similar questions - but algorithm or data structure used by the solution might vary.


Group 1

https://www.techiedelight.com/find-elements-array-greater-than-elements-right/

https://www.techiedelight.com/replace-every-element-array-least-greater-element-right/

https://www.geeksforgeeks.org/count-of-larger-elements-on-right-side-of-each-element-in-an-array/

https://www.geeksforgeeks.org/count-smaller-elements-on-right-side/

https://www.techiedelight.com/previous-smaller-element/

https://www.techiedelight.com/next-greater-element-circular-array/

https://www.techiedelight.com/inversion-count-array/


Sorting Logic application

https://www.techiedelight.com/problems-solved-using-partitioning-logic-quicksort/

https://www.techiedelight.com/segregate-positive-negative-integers-using-mergesort/


Dynamic Programing

https://www.techiedelight.com/4-sum-problem/


Trees

https://leetcode.com/problems/binary-tree-maximum-path-sum

https://leetcode.com/problems/path-sum-iv/solution/

Kth Frequent elements.

Kth frequent elements in a stream.

LRU cache Implementation

All of the above can be done in O(1) time


String

https://www.techiedelight.com/check-given-string-rotated-palindrome-not/

https://www.techiedelight.com/longest-palindromic-substring-non-dp-space-optimized-solution/


Custom Sort:

https://www.programmersought.com/article/82237172611/


Thursday, January 10, 2019

Using QEMU to run Linux kernel

We can use qemu to run linux kernel. I ll describe two cases one for arm and other for x86

If you want to use the latest qemu, clone and compile as follows,

To clone and compile qemu for arm64:

$ git clone git://git.qemu.org/qemu.git
$ cd qemu
$ sudo apt-get install libpixman-1-dev
$ ./configure --target-list=aarch64-softmmu
$ make -j4


Second step is to get a initrd. You can create one using the below steps,

## Build Busybox

To make Linux useful, it needs a shell. These following instructions will
construct a file system for the Linux RAM disk with the BusyBox shell as the
init process.


Install prerequisites:

$ sudo apt install make binutils-aarch64-linux-gnu


$ git clone git://busybox.net/busybox.git
$ cd busybox
$ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make defconfig
$ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make menuconfig

At this point you should ensure that the option `Settings > Build static binary
(no shared libs)` is selected. Then you can proceed with the following commands:

ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make -j24
ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make install
cd _install
mkdir proc
mkdir sys
mkdir -p etc/init.d
cat < etc/init.d/rcS
#!bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
EOF
chmod u+x etc/init.d/rcS
grep -v tty ../examples/inittab > ./etc/inittab


## Create a RAM disk for Linux

find . | cpio -o -H newc | gzip > ../initrd.img

To run qemu for arm64:

qemu/aarch64-softmmu/qemu-system-aarch64 -M virt -m 1G -cpu cortex-a57 -nographic -kernel arch/arm64/boot/Image -initrd initrd.img -append "rdinit=/sbin/init

To run qemu for x86_64:
qemu-system-x86_64 -m 1G -nographic -kernel arch/x86/boot/bzImage -initrd initrd.img -append "console=ttyS0 rdinit=/sbin/init"


Friday, May 12, 2017

Lets try recovering a file from rm *

By mistake I deleted a source code which I spend around a day to develop. With the help of extundelete utility it is possible to recover file if same space is not reallocated by file system.

Here is what I did to recover my deleted file info_bootimg.c

sudo src/extundelete --restore-file  workspace/utils/helper_utills/bootinfo/info_bootimg.c  /dev/mapper/vg00-lv_local_mnt

"workspace/utils/helper_utills/bootinfo/" is the path relative to the root of partition

This is where the partition is mounted,

/dev/mapper/vg00-lv_local_mnt on /local/mnt type ext4 (rw)

File's absolute path is "/local/mnt/workspace/utils/helper_utills/bootinfo/info_bootimg.c"

But you have to strip "/local/mnt" to get it working.

Download extundelete from,
http://extundelete.sourceforge.net/

./configure and make

Tuesday, January 3, 2017

Debugging using T32 on a Multi cores/cpus

Contemporary processors are multi core/cpus. This has made life of a developer bit tougher. For eg, for the break point to hit, you need to set it on all the cpus, because you never know where the code is going to run. Another instance is to stop all the core at the same time.

Below is a set of scritps which helps you to make your job easier.

For eg we will consider working on a 4 core processor.

You need to create few cmm files. Contents and file names as shown below,

1) main.cmm
=========
GLOBAL &ScriptPath &vmlinuxPath

&ScriptPath="Z:\deleteme\T32"
&vmlinuxPath="Z:\kdevs\3.18\kdev\kobj"

globalon cmd i do &ScriptPath\initcore_all.cmm
globalon cmd pos do &ScriptPath\pos.cmm
globalon cmd load do &ScriptPath\load_all.cmm
globalon cmd ball do &ScriptPath\break_all.cmm


==========

2) initcore_all.cmm
============
intercom.execute localhost:15370 do &ScriptPath\initcore.cmm
intercom.execute localhost:15371 do &ScriptPath\initcore.cmm
intercom.execute localhost:15372 do &ScriptPath\initcore.cmm
intercom.execute localhost:15373 do &ScriptPath\initcore.cmm
============

3) initcore.cmm
==========
sys.m.a
snoop.pc ON
b.d
b.s die  /Onchip
b.s panic  /Onchip
===========

4) loadall.cmm
=========
intercom.execute localhost:15370 do &ScriptPath\load.cmm &vmlinuxPath
intercom.execute localhost:15371 do &ScriptPath\load.cmm &vmlinuxPath
intercom.execute localhost:15372 do &ScriptPath\load.cmm &vmlinuxPath
intercom.execute localhost:15373 do &ScriptPath\load.cmm &vmlinuxPath
==========

5) load.cmm
========
ENTRY &vmlinuxPath
d.load.elf &vmlinuxPath\vmlinux /NOCODE
========

6) pos.cmm
=======
intercom.execute localhost:15370 framepos 0 0 65 13
intercom.execute localhost:15371 framepos 75 0 65 13
intercom.execute localhost:15372 framepos 0 25 65 13
intercom.execute localhost:15373 framepos 75 25 65 13
========

7) break_all.cmm
===========
intercom.execute localhost:15370 break
intercom.execute localhost:15371 break
intercom.execute localhost:15372 break
intercom.execute localhost:15373 break
===========

Open four instances of T32. run main.cmm on T32 associated with core 0.

then type the i in the command line to run intercore_all.cmm




Using GDB to find structure info

As usual  load elf file

$gdb vmlinux

We are going to inspect the address layout of structure platform_device,

struct platform_device {
        const char      *name;
        int             id;
        bool            id_auto;
        struct device   dev;
        u32             num_resources;
        struct resource *resource;

        const struct platform_device_id *id_entry;
        char *driver_override; /* Driver name to force a match */

        /* MFD cell pointer */
        struct mfd_cell *mfd_cell;

        /* arch specific additions */
        struct pdev_archdata    archdata;
};


(gdb) p &((struct platform_device *)0)->name
$2 = (const char **) 0x0
(gdb) p &((struct platform_device *)0)->id
$3 = (int *) 0x8
(gdb) p &((struct platform_device *)0)->id_auto
$4 = (bool *) 0xc
(gdb) p &((struct platform_device *)0)->dev
$5 = (struct device *) 0x10

Above output shows the offset of different variables in the structure.