Showing posts with label DevTips. Show all posts
Showing posts with label DevTips. Show all posts

Saturday, November 25, 2017

[Solved] Android Studio : Android emulator is incompatible with Hyper-V

Have been lately doing Android development. Ran into a minor problem. So I have two machines, a desktop and a laptop. I set the project on my desktop and everything was working fine (read AVD). Checked in to the code to a GIT repo. However when I was checking out the code on my laptop Lenovo T470s, I ran into a problem with starting the AVD (Android Virtual Device) Manager.

It would show me this error.




Intel HAXM is required to run this AVD.
Android Emulator is incompatible with Hyper-V.
Unfortunately, you cannot have Hyper-V running and use the emulator.
Here is what you can do:
  1) Start a command prompt as Administrator
  2) Run the following command: C:\Windows\system32> bcdedit /set hypervisorlaunchtype off
  3) Reboot your machine


The first thing I did, disabled Hyper-V from my BIOS setting after restarting the system. However then I still kept on getting this error. Then I disabled Hyper-V feature from the 'Windows Feature on or off' settings.



Then I finally installed the HAXM tool from intel's Website (Hardware Accelerated Execution Manager) and things started working smoothly for me.
Hope this helps. 

Thursday, March 30, 2017

Block UDP Traffic via IPTables

While testing an application to verify all code flow paths, one of the scenarios demanded that the application handle dropped UDP packet stream. Now we had a recorded UDP stream at hand which we could play at will.

To simulate UDP packet drop, we added a rule in IPTables to drop/block all UDP packets destined for our UDP destination port 10222. The following command does it for you. 

To Block all udp traffic destined for port 10222
[root@paragpc ~]# iptables -A INPUT -p udp -i eth1 --dport 10222 -j DROP
where:
  • -A is for add/Append iptable rule
  • -p is for protocol
  • -i  = --in-interface eth1  (there is similarly -o =--out-interface
  • -j = JUMP
Refer the man page of iptables for more details. 

However don't forget to remove the rule once your test case is over.
Remove iptable rule dropping udp traffic destined for port 10222 [-D is for Delete iptable rule]
[root@paragpc ~]# iptables -D INPUT -p udp -i eth1 --dport 10222 -j DROP
However if you wish to permanently save the rule run the following command(source).
service iptables save



Sunday, September 04, 2016

C++ Identifying threads with names

In multi-threaded applications, many a times during debugging or logging, we wish to identify the threads by name[functionality ]. Simply printing the executing method name in logs does not help if the functions are common between threads doing different tasks. Also you may want to see the currently running threads from either top or ps in your application.

Here is sample program to tag your threads with proper names. Use prctl defined in <sys/prctl.h> , to get/set thread names. Keep in mind that the name can be up to 16 bytes long only. 

#include <stdio.h>
#include <pthread.h>
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <sys/prctl.h>

//compile the program with : -lpthread

using namespace std;

void * f1(void *) {

 try{
  prctl(PR_SET_NAME,"Thread_name_f1",0,0,0);
  printf("f1 : Starting sleep\n");
  // ..
  // Code goes here
  // utils();
  // g();
  // ..
 }
 catch(std::string &s){
  char thread_name[17]={0};
  prctl(PR_GET_NAME,thread_name,0,0,0);
  std::cout << "Thread:" << thread_name <<"crashed. Reason:" << s << std::endl;
 }
}

void * f2(void *) {

 try{
      prctl(PR_SET_NAME,"Thread_name_f2",0,0,0);
  printf("f2 : Starting sleep\n");
  // ..
  // Code goes here
  // a();
  // utils();
  // ..
 }
 catch(std::string &s){

  char thread_name[17]={0};
  prctl(PR_GET_NAME,thread_name,0,0,0);
  std::cout << "Thread:" << thread_name <<"crashed. Reason:" << s << std::endl;
 }
}

int main() {
 pthread_t  f1_thread, f2_thread;
 pthread_create(&f1_thread, NULL, f1, NULL);
 pthread_create(&f2_thread, NULL, f2, NULL);

 printf("Main : Starting sleep\n");
 sleep(50);
 pthread_join( f1_thread, NULL);
 pthread_join( f2_thread, NULL);
 printf("Main : Done sleep\n");
 return 0;

}

Sunday, August 28, 2016

Removing deleted remote branches from local repo

Sometimes we have a POC(Proof of concept) branches, which are forked off the main/feature branches. After a while when POC is complete, we/the dev no longer uses it.
Now over time the no of such POC branches increase and the repo doesnt look good.
So during our periodic maintenance and cleaning of GIT repo, we delete all such POC branches.

How to sync your local branch list.
Simple:

[parag@paragpc: ~/ProjectCodeName] git fetch -p
 x [deleted]  (none) ->origin/POC-webCache
 x [deleted]  (none) ->origin/POC-CDNChange
[parag@paragpc: ~/ProjectCodeName] 
where -p = --prune
                     After fetching, remove any remote-tracking branches which no longer exist on the remote. 


Similarly, if you are have created a local branch and committed some changes while the remote tracking branch is not present at origin, issue:
[parag@paragpc: ~/GitProject] git branch -d <the_dirty_poc_branch>

where -d = --delete
        Delete a branch. The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set with --track or --set-upstream

PS: Before running any delete related operation on branch its better to double check and understand what you are doing!.


Sunday, September 20, 2015

C++: Looking at source code while debugging in GDB

Sometimes when I am debugging my code for issues/bugs, I want to look at the surrounding piece of code where my debugger is executing, or may be I want to look at the some lines of code in a particular file. Its quite time consuming to toggle windows, switch source code editor/IDE locating code(sometimes switch desktops). Worry no more, use the following commands, to help you navigate through the code, while debugging.

Scenario: after you have set your breakpoint and the GDB debugger hits the section, and you want to look around those line, type list

[Parag@paragpc:/home/Parag/Workspace/Utils]gdb sample
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/Parag/Workspace/Utils/sample...done.
(gdb) break countLines
Breakpoint 1 at 0x400cf2: file MultiUseHeader.hpp, line 21.
(gdb) run
Starting program: /home/sid/Parag/Workspace/Utils/samp 
inside registerQuickExitHandlers()

Breakpoint 1, countLines () at MultiUseHeader.hpp:21
warning: Source file is more recent than executable.
21     std::ifstream myfile(strFileName);
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.107.el6.x86_64 libgcc-4.4.7-11.el6.x86_64 libstdc++-4.4.7-11.el6.x86_64
(gdb) list
16  char cArr[2];
17 };
18 
19 int countLines(std::string& strFileName) {
20 
21     std::ifstream myfile(strFileName);
22 
23     // new lines will be skipped unless we stop it from happening:    
24     myfile.unsetf(std::ios_base::skipws);
25 
(gdb) n
24     myfile.unsetf(std::ios_base::skipws);
(gdb) n
30         '\n');
(gdb) list
25 
26     // count the newlines with an algorithm specialized for counting:
27     unsigned line_count = std::count(
28         std::istream_iterator<char>(myfile),
29         std::istream_iterator<char>(), 
30         '\n');
31 
32     std::cout << "Lines: " << line_count << "\n";
33     return 0;
34 }
(gdb) 


Similarly if you want to look at some specific lines in a source file, type:

(gdb) list MultiUseHeader.hpp:32
27     unsigned line_count = std::count(
28         std::istream_iterator<char>(myfile),
29         std::istream_iterator<char>(), 
30         '\n');
31 
32     std::cout << "Lines: " << line_count << "\n";
33     return 0;
34 }
35 
36 
(gdb) 

Sunday, November 30, 2014

Debugging with GDB: Examining Call Stack and Call Stack Frames


If you are debugging a programming with gdb and want to see the call stack, and the call stack is really big (spanning the whole page), it makes sense to see only the top (innermost) 5-6 stack frames rather than the entire stack.


(gdb) backtrace
#0  0x00007ffff7bc2620 in thirdPartyFFTransform () from ./liblzo2.so
#1  0x00007ffff7108e43 in useMD5Checksum (karlCoef=0x7ffff2cd0ff0, ffCoef=0x7ffff2cd10c0) at ThirdParty/HardFFTrf.c:99
#2  0x00007ffff71091d5 in FastCheckSum (gamma=3426, delta=0x7ffff730e4c8 <acc_no> "x^2+5y  ", capillaryRise=0x7ffff2cd1120, 
    pCheckSumList=0x7ffff2cd10c0) at ThirdParty/HardFFTrf.c:152
#3  0x00007ffff7107349 in compute_fourier (gamma=40127, delta=0x7ffff2cd1120, CoefA=0x7ffff2cd123a) at ThirdPartyFF.c:1125
#4  0x0000000000491c6e in example::MathCalculator::FastFourierHelper::getCoefficient(FastFourier::Coefficient const*) ()
#5  0x0000000000492a44 in example::MathCalculator::FastFourierHelper::createFFDescriptor(std::vector<example::MathCalculator::ComputeIterationN*, std::allocator<example::MathCalculator::ComputeIterationN*> > const&, int) ()
#6  0x0000000000478767 in example::MathCalculator::FTransform::FTransform(example::MathCalculator::MessageQueue<example::MathCalculator::QMessg, 1024ul>*, example::MathCalculator::MessageQueue<example::MathCalculator::QueueMessage, 1024ul>*, std::vector<example::MathCalculator::ComputeIterationN*, std::allocator<example::MathCalculator::ComputeIterationN*> >&, example::MathCalculator::Accumulator*) ()
#7  0x0000000000474f23 in example::MathCalculator::MathEngine::createJobFactory(FastFourier::EquationDescription*) ()
#8  0x0000000000476856 in example::MathCalculator::MathEngine::processQueue() ()
#9  0x00000000004770b1 in example::MathCalculator::MathEngine::checkAssignments() ()
#10 0x00000000004a7d2e in example::MathCalculator::MathComputeEngine::Calculate() ()
#11 0x000000000046072c in example::MathCalculator::MMain::runMathLibThread(int) ()
#12 0x00007ffff7787b53 in thread_proxy () from ./libboost_thread.so.1.48.0
#13 0x00007ffff5ecadc5 in start_thread () from /lib64/libpthread.so.0
#14 0x00007ffff63de73d in clone () from /lib64/libc.so.6
(gdb) 
(gdb) 
(gdb) bt 4
#0  0x00007ffff7bc2620 in thirdPartyFFTransform () from ./liblzo2.so
#1  0x00007ffff7108e43 in useMD5Checksum (karlCoef=0x7ffff2cd0ff0, ffCoef=0x7ffff2cd10c0) at ThirdParty/HardFFTrf.c:99
#2  0x00007ffff71091d5 in FastCheckSum (gamma=3426, delta=0x7ffff730e4c8 <acc_no> "x^2+5y  ", capillaryRise=0x7ffff2cd1120, 
    pCheckSumList=0x7ffff2cd10c0) at ThirdParty/HardFFTrf.c:152
#3  0x00007ffff7107349 in compute_fourier (gamma=40127, delta=0x7ffff2cd1120, CoefA=0x7ffff2cd123a) at ThirdPartyFF.c:1125
#4  0x0000000000491c6e in example::MathCalculator::FastFourierHelper::getCoefficient(FastFourier::Coefficient const*) ()
(gdb) 
(gdb) 
(gdb) bt -4
#11 0x000000000046072c in example::MathCalculator::MMain::runMathLibThread(int) ()
#12 0x00007ffff7787b53 in thread_proxy () from ./libboost_thread.so.1.48.0
#13 0x00007ffff5ecadc5 in start_thread () from /lib64/libpthread.so.0
#14 0x00007ffff63de73d in clone () from /lib64/libc.so.6

Stack frames

Consider the following stack dump


(gdb) backtrace
#0  0x0000000000401449 in autoPtrExample (rm=3) at MultiUseHeader.hpp:81
#1  0x0000000000401254 in registerQuickExitHandlers (k=3, comment="called from main") at MultiUseHeader.hpp:66
#2  0x00000000004015aa in registeringExitHandlers () at samAutoPtr.cpp:22
#3  0x0000000000401610 in main () at samAutoPtr.cpp:30
(gdb) backtrace -2
#2  0x00000000004015aa in registeringExitHandlers () at samAutoPtr.cpp:22
#3  0x0000000000401610 in main () at samAutoPtr.cpp:30
(gdb) bt 2
#0  0x0000000000401449 in autoPtrExample (rm=3) at MultiUseHeader.hpp:81
#1  0x0000000000401254 in registerQuickExitHandlers (k=3, comment="called from main") at MultiUseHeader.hpp:66
(More stack frames follow...)

You can select your frame of interest using the frame command.


(gdb) frame 0
#0  0x0000000000401449 in autoPtrExample (rm=3) at MultiUseHeader.hpp:81
81  cout << "Printing Rollno: p " << m->roll << " q " << n->roll << "\n"; // <- Seg fault
(gdb) frame 1
#1  0x0000000000401254 in registerQuickExitHandlers (k=3, comment="called from main") at MultiUseHeader.hpp:66
66  autoPtrExample(3);
(gdb) 

Similarly to traverse one frame in either direction one at a time use the up/down command


(gdb) up
#2  0x00000000004015aa in registeringExitHandlers () at samAutoPtr.cpp:22
22  registerQuickExitHandlers(3, "called from main");
(gdb) up
#3  0x0000000000401610 in main () at samAutoPtr.cpp:30
30  registeringExitHandlers();
(gdb) up
Initial frame selected; you cannot go up.
(gdb) down
#2  0x00000000004015aa in registeringExitHandlers () at samAutoPtr.cpp:22
22  registerQuickExitHandlers(3, "called from main");
(gdb) down
#1  0x0000000000401254 in registerQuickExitHandlers (k=3, comment="called from main") at MultiUseHeader.hpp:66
66  autoPtrExample(3);
(gdb) down
#0  0x0000000000401449 in autoPtrExample (rm=3) at MultiUseHeader.hpp:81
81  cout << "Printing Rollno: p " << m->roll << " q " << n->roll << "\n"; // <- Seg fault
(gdb) down
Bottom (innermost) frame selected; you cannot go down.

To see the arguments passed in a particular frame use the info args command once the frame of interest is selected

(gdb) frame
#0  0x0000000000401449 in autoPtrExample (rm=3) at MultiUseHeader.hpp:81
81  cout << "Printing Rollno: p " << m->roll << " q " << n->roll << "\n"; // <- Seg fault
(gdb) up
#1  0x0000000000401254 in registerQuickExitHandlers (k=3, comment="called from main") at MultiUseHeader.hpp:66
66  autoPtrExample(3);
(gdb) info args
k = 3
comment = "called from main"
(gdb) 

To see the local arguments of the selected frame use the info locals command to print the variables:


(gdb) info locals
filename = "./StateWisePopulationRecord.csv"
(gdb) 

[C#] Mutliple Concurrent Producers and Consumers pattern for a Task Queue

If you are in need of a basic concurrent Producers consumers pattern to be used in your application, here is a sample C# program to refer to...