GSoC 2016 Introduction

Hi everyone,

My name is Pranav Ganorkar and I am a third year student at V.E.S. Institute of Technology, Mumbai pursuing Bachelor’s degree in Computer Engineering.

My very first introduction to Linux was in eighth grade when I booted the Live CD (Yes, CD’s used to be much popular at that time due to low penetration of Internet) of Fedora 13 on my Desktop. I was very much impressed by it’s user interface and started hacking on it. It was my first introduction to GNOME Desktop. At that time , in high school, I was mostly interested in installing various distros and trying out new desktop environments. What a wonderful life back then , wasn’t it ? 🙂

I started following GNOME Development when I was in my first year of Engineering. It was when I learnt about the GTK+ Toolkit and tried to make some simple applications using it.  It’s been almost three years now since I have been using GNOME as my primary desktop environment on Arch Linux. I started following GNOME Logs development from January 2016, got acquainted with it’s source code, started hacking on it and was able to make my first contribution in March 2016 with a lot of help from David, my mentor.

Now, some details regarding my GSoC project. I will be working on improving the search feature in GNOME Logs under the mentorship of David King. Currently, searching in GNOME Logs is only restricted to the front end part. Meaning, the actual search results are not fetched from sd-journal API (the API which GNOME Logs uses to fetch the entries from the journal) but using the filter function for GtkListBox in the already fetched results. So, my first task is to move the searching to the back-end part. That is, when the user types text in the search bar, the results will actually be fetched from the sd-journal API.

I will also be improving the GUI for the search interface. Currently, GNOME Logs does not have any option to graphically restrict the journal fields by which user entered text will be filtered. So, I will be adding a drop down menu besides the search bar to let the user select the various journal fields by which to restrict the search results from. Currently I have done a GUI mockup for it which looks like this:

gnome-logs-gsoc

Also, the advanced options menu button will provide the user with a dialog box which will provide with advanced journalctl filtering options like “since” and “until”. Also, user can specify the type of search : exact or substring which he wants to perform. Currently, the GUI mockup for advanced search dialog box looks like this:

advanced-search-dialog-box

Along with the above tasks, I will also be writing a search provider for GNOME Logs. So, GNOME Shell will be getting a new search provider. Yay 🙂

Finally, testing will be done to test all the above implemented functionalities. I hope that after the completion of my project, GNOME Logs will be able to match up with journalctl tool in terms of searching and filtering functionality.This is a short summary of my GSoC project.

Looking forward to an exciting summer and learning a lot in process about GNOME Development and it’s vibrant community 🙂

Also, feel free to comment about what you think of the current GUI mockups.

See you in next post. Until then,

Happy Coding !

Constant Pointers in C

Recently, I was working on a bug of Gnome-Logs. It involved some operations on pointers. More specifically, the pointer variables were used to determine the index of the newline character (\n) in a string and determine the substring between two newline characters (\n). I successfully submitted my patch solving the bug. But the reviewer mentioned that the following char pointers should be kept const.

gchar   *newline_index;
gchar   *prevpos;

I asked him why should they be const because we are modifying them on the following lines in the loop:

prevpos = newline_index + 1;
newline_index = strchr (prevpos, ‘\n’);

The reviewer then explained to me that the const modifier acts differently on pointers. Specifically, const modifier on a pointer allows us to change the memory location it is pointing to but does not allow us to change the value at that memory location. So, The above modifying code is perfectly valid even in the case of const pointer. I thank to the reviewer, David King, for clearing this misconception for me.

Here,  In this post I will explain about the different types of const pointers used in C:

There are basically three types of const pointers:

  1. Constant Pointers
  2. Pointer to Constant
  3. Constant Pointer to a Constant

Let us understand each of these one by one. We will first start with the Constant Pointers.


 

Constant Pointer

A constant pointer is a pointer that cannot change the address it is holding. We can also say that once a constant pointer starts pointing to a variable then it cannot point to any other variable. The syntax for const pointer is as follows:

<type of pointer>  * const <name of pointer>

An example declaration is:

char * const ptr;

Let us take a small program to illustrate these type of pointers:

#include<stdio.h>

int main(void)
{
int var1 = 0, var2 = 0;
int *const ptr = &var1;
ptr = &var2;
printf("%d\n", *ptr);

return 0;
}

Here , we have two int variables var1 and var2 and and a constant pointer ptr.
we first initialize the constant pointer ptr variable to point to var1 and then to var2.

So, we assigned an address to a constant pointer and then we tried to change the address by assigning the address of some other variable to the same constant pointer.

If we compile the above program, we will get the following error:

$ gcc -Wall constptr.c -o constptr
constptr.c: In function ‘main’:
constptr.c:7: error: assignment of read-only variable ‘ptr’

We see that while compiling the compiler complains about ptr being a
read only variable. This means we cannot change the value ptr holds. Hence, a constant variable which points to some variable cannot be made to point to another variable.

Now, we discuss about the next type of const pointers: Pointer to Constant.


Pointer to Constant

A pointer through which one cannot change the value of variable it points to is known as a pointer to a constant. These type of pointers can change the address they point to but cannot change the value kept at those address. It was this type of constant pointer that I had misconception about while working on my patch.

The syntax for a pointer to a constant is given as:

const <type of pointer>* <name of pointer>

An example definition could be:

const char* ptr;

Let us understand the working of pointer to constant through a small program:

#include<stdio.h>

int main(void)
{
int var1 = 0;
const int* ptr = &var1;
*ptr = 1;
printf("%d\n", *ptr);

return 0;
}

In the above code, we defined a variable named var1 and initialized it to 0. Then we defined a pointer to constant ptr and intialize it to point to the address of var1. Next we change the value of var1 to 1 through the use of *ptr. Finally , we print the new value using printf.

When we compile the above code, we get the following error:

$ gcc -Wall constptr.c -o constptr
constptr.c: In function ‘main’:
constptr.c:7: error: assignment of read-only location ‘*ptr’

We see that the compiler complains about ‘*ptr’ being read only. It means that we cannot change the value using the ‘ptr’ variable as it is defined to be a pointer to a constant.

Next we will discuss the last type: Constant Pointer to a Constant


 

Constant Pointer to a Constant

This type is a mixture of the above two types. A constant pointer to a constant is a pointer that can neither change the address it is pointing to nor it can change the value present at that address.

The syntax for defining a constant pointer to a constant is:

const <type of pointer>* const <name of pointer>

An example definition can be given like:

const char* const ptr;

Let us understand the working of constant pointer to a constant through a small program:

#include<stdio.h>

int main(void)
{
int var1 = 0,var2 = 0;
const int* const ptr = &var1;
*ptr = 1;
ptr = &var2;
printf("%d\n", *ptr);

return 0;
}

In the above program, we declared two integer variables var1 and var2 and initialize
them to 0. Next, we declared a constant pointer to a const ptr and intialized it to point to
var1. We then change the value and address of the pointer ptr in the next two lines. Finally, we print the value at the address pointed by ptr.

When we compile the above program, we get the following error:

$ gcc -Wall constptr.c -o constptr
constptr.c: In function ‘main’:
constptr.c:7: error: assignment of read-only location ‘*ptr’
constptr.c:8: error: assignment of read-only variable ‘ptr’

 

So we see that the compiler complains about both the value and address being changed.Hence, we infer that a constant pointer to a constant cannot change the address and value pointed by it.

I hope you have understood the concept of constant pointers in C and your misconceptions about it just like me must have been cleared through this post.

Looking forward to see you all, until then,

Happy Programming !!!!

 

 

 

 

Detect a loop in a Linked List

Suppose you are given a linked list like this:

linked-list-loop

And you want to remove the loop in it.

A very trivial way can be storing a boolean “visited” variable which stores true or false depending on whether a node is visited or not. But we need to store this info in each and every node. This will cost us some space.

A very clever way is to use two pointer for traversing the linked list. one pointer will travel one node at a time and other node will travel two nodes at a time. we can also call the pointers as slowpointer and fastpointer. Now, if there is a loop in a linked list , these two pointers will meet at a certain node.

The above mentioned algorithm was proposed by Robert Floyd and is also called Floyd’s loop detection algorithm.

so, this is how we detect a loop in a Linked List.

GDB – a useful tool

I have just started hacking with Libreoffice. For that I downloaded the source code of libreoffice and tried building it on my ArchLinux and Windows 8. But the build was failing in both the cases somewhere in the unit test portion. I posted my problem on libreoffice mailing list about the segmentation fault I was getting during the build. One of the developers mentioned to me about using “gdb” to print the backtrace of the segmentation fault. That’s where I learnt about this new tool called “gdb” or GNU debugger which is a very useful tool for debugging programs. so, here’s how to debug programs with gdb.

First, into a terminal type:

gdb program-name

After running it , you will enter into the gdb interactive prompt:

at the prompt type “run” to start the execution of the program.

Now , if at any point, your program crashes, gdb will stop the execution of the program at that point.

now , type “bt” command at the prompt to generate the backtrace of execution and get the exact file which caused the problem.

Happy Debugging…!!!

 

 

Strace – a useful debugger in linux

I recently started working on a Project to port the GTK based pavucontrol to Qt.Not only this port will add a good gui in Qt to control pulseaudio but also I plan on adding some new features like equalizer to it.I have named it lxmixer as I am developing it primarily for LXQT Desktop Environment.It will be a nice addition to gui-toolkits of LXQT.For this, I started with the source code of pavucontrol.I cloned it from the pavucontrol’s git repo and then compiled it with the following commands:


./autogen.sh
make

In first attempt, configure worked fine but make started giving error about not having the rule to make README file. I solved this by looking at AUR package of pavucontrol-git in archlinux. It included a command to create README file in /doc folder using touch command. After that , make compiled it successfully.

But after I ran the compiled pavucontrol binary, it gave me an error “terminate due to exception thrown by Glib:FileError.Core Dumped”.
I googled a lot about this.
Most people having such issues use gdb to debug the program.
I also used gdb to debug the program and generate backtrace using it.


gdb ./pavucontrol
>> run
>> backtrace
>> quit

Gdb backtrace info was not much Helpful.
But after some more googling, I found a forum post containing the same error as mine and the author had used strace command to generate backtrace.
At first, I thought it was some kind of command used in gdb interpreter.
but then i found that it was seperate application from gdb.
I quickly installed it using pacman
and generated trace output using strace.


strace ./pavucontrol

And Wholla !!!! It gave me the exact reason why the error occured and where it occured.It was due to
some file not found error in importing a .glade(gtk UI Designer file).It was searching the glade file in a location where the .glade file was not present.I fixed the error by making changes to source code to search in the current build directory.
And, after making this change, the compiled pavucontrol binary ran successfully with no error or warnings at all.
Now, I can start working on porting it to Qt.I hope to complete it in 2-3 weeks.