Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Friday, 3 January 2020

Harry
I have been chasing for a tool/compiler option to generate call graph for user space C program.  Some time back i wrote a tool that was dependent on gdb to generate stack frames and generate call graph out of it using python:
https://github.com/tarun27sh/gdb_graphs

The problem with it is it's very slow. One stackoverflow user was kind enough to use it, only to complain it's very slow. So, it's time to explore more options:

1. use gcc -finstrument-functions
2. use LLVM  to write a transform pass that adds profiler instructions to each function.


In this post I'll cover #1, and will try to cover #2 in a future post - .


How to work with gcc -finstrument-functions??

Lets start with a simple hello world.

for hw.c:
$ cat hw.c
#include <stdio.h>
int main()
{
    printf("Hello World\n");
    return 0;
}

comile with:
gcc hw.c    // generates a.out

and dump assembly for main:
objdump -S a.out
. . .
0000000000400526 <main>:
  400526:       55                      push   %rbp
  400527:       48 89 e5                mov    %rsp,%rbp
  40052a:       bf c4 05 40 00          mov    $0x4005c4,%edi
  40052f:       e8 cc fe ff ff          callq  400400 <puts@plt>
  400534:       b8 00 00 00 00          mov    $0x0,%eax
  400539:       5d                      pop    %rbp
  40053a:       c3                      retq
  40053b:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
. . .

This is a normal assembly generated by gcc for our main function.

Now lets compile with -finstrument-functions and see what gets added:

gcc -finstrument-functions hw.c    // generates a.out

and dump assembly for main:
objdump -S a.out
. . .
00000000004005d6 <main>:
  4005d6:       55                      push   %rbp
  4005d7:       48 89 e5                mov    %rsp,%rbp
  4005da:       53                      push   %rbx
  4005db:       48 83 ec 08             sub    $0x8,%rsp
  4005df:       48 8b 45 08             mov    0x8(%rbp),%rax
  4005e3:       48 89 c6                mov    %rax,%rsi
  4005e6:       bf d6 05 40 00          mov    $0x4005d6,%edi
  4005eb:       e8 d0 fe ff ff          callq  4004c0 <__cyg_profile_func_enter@plt>
  4005f0:       bf a4 06 40 00          mov    $0x4006a4,%edi
  4005f5:       e8 96 fe ff ff          callq  400490 <puts@plt>
  4005fa:       bb 00 00 00 00          mov    $0x0,%ebx
  4005ff:       48 8b 45 08             mov    0x8(%rbp),%rax
  400603:       48 89 c6                mov    %rax,%rsi
  400606:       bf d6 05 40 00          mov    $0x4005d6,%edi
  40060b:       e8 a0 fe ff ff          callq  4004b0 <__cyg_profile_func_exit@plt>
  400610:       89 d8                   mov    %ebx,%eax
  400612:       48 83 c4 08             add    $0x8,%rsp
  400616:       5b                      pop    %rbx
  400617:       5d                      pop    %rbp
  400618:       c3                      retq
  400619:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)
. . .

Now we see two additional function calls have been injected by gcc:
1. __cyg_profile_func_enter@plt
2. _cyg_profile_func_exit@plt

@plt just means these functions will be resolved during run time by linker.

If the user doesn't define these functions, gcc has a default definition which just returns:
(gdb) disas __cyg_profile_func_enter
Dump of assembler code for function __cyg_profile_func_enter:
   0x00007ffff7b23200 <+0>:     repz retq
End of assembler dump.
(gdb) disas __cyg_profile_func_exit
Dump of assembler code for function __cyg_profile_func_enter:
   0x00007ffff7b23200 <+0>:     repz retq
End of assembler dump.
(gdb)

Turns out repz retq is an  interesting topic in itself:

By defining these two APIs, one can override the default return behavior.

Define Entry/Exit hook

  1 #include<stdio.h>
  2
  3 static void __attribute__((no_instrument_function))
  4 __cyg_profile_func_enter (void *this_fn,
  5                                void *call_site)
  6 {
  7       printf("[+]\n");
  8 }
  9
 10 static void __attribute__((no_instrument_function))
 11 __cyg_profile_func_exit  (void *this_fn,
 12                                void *call_site)
 13 {
 14     printf("[-]\n");
 15 }
 16
 17 int main()
 18 {
 19     printf("HW\n");
 20     return 0;
 21 }

- line# 3,10 - tell gcc to not inject calls to enter/exit apis in profiler functions

Compile and run
$ gcc -finstrument-functions hw.c  // generates a.out

$ ./a.out
[+]
HW
[-]

Great!
Now we are able to make use of injected functions. Next step would be to printf function name from where it is called and generate some kind of command line function graph. Something similar to what ftrace does.

But first where are these functions declared/defined?

I searched in the gcc source code, found following references to the enter function, but none of them point to its definition where it sets repz retq instructions.

Text string: __cyg_profile_func_enter

  File                                       Line
0 gcc/testsuite/g++.dg/pr49718.C                 5 /* { dg-final { scan-assembler-times "__cyg_profile_func_enter" 1 { target { ! { hppa*-*-hpux* } } } } } */
1 gcc/testsuite/g++.dg/pr49718.C                 6 /* { dg-final { scan-assembler-times "__cyg_profile_func_enter,%r" 1 { target hppa*-*-hpux* } } } */
2 testsuite/gcc.c-torture/execute/eeprof-1.c    65 void __cyg_profile_func_enter (void*, void*) NOCHK;
3 testsuite/gcc.c-torture/execute/eeprof-1.c    69 void __cyg_profile_func_enter (void *fn, void *parent)
4 gcc/testsuite/gcc.dg/20001117-1.c             31 __cyg_profile_func_enter(void *this_fn, void *call_site)
5 gcc/testsuite/gcc.dg/instrument-1.c            6 /* { dg-final { scan-assembler "__cyg_profile_func_enter" } } */
6 gcc/testsuite/gcc.dg/instrument-2.c            6 /* { dg-final { scan-assembler-not "__cyg_profile_func_enter" } } */
7 gcc/testsuite/gcc.dg/instrument-3.c            6 /* { dg-final { scan-assembler-not "__cyg_profile_func_enter" } } */
8 gcc/testsuite/gcc.dg/pr78333.c                 4 /* Add empty implementations of __cyg_profile_func_enter() and
9 gcc/testsuite/gcc.dg/pr78333.c                 8 __cyg_profile_func_enter(void *this_fn, void *call_site)
a gcc/tree.c                                 10683 local_define_builtin ("__cyg_profile_func_enter", ftype,
b gcc/tree.c                                 10685 "__cyg_profile_func_enter", 0);

May be this is something arch dependent.
Let me know if you know how to find the place where gcc sets its definition.

How to add code to generate call stacks?

Now the only thing that our program has to do is to define what these hooks do when called:


  1 #define _GNU_SOURCE
  2 #include <dlfcn.h>
  3
  4 static void __attribute__((no_instrument_function))
  5 __cyg_profile_func_enter (void *this_fn,
  6                                void *call_site)
  7 {
  8     Dl_info info;
  9     dladdr(__builtin_return_address(0), &info);
 10     printf("[+] %s\n", info.dli_sname);
 11 }
 12
 13 static void __attribute__((no_instrument_function))
 14 __cyg_profile_func_exit  (void *this_fn,
 15                                void *call_site)
 16 {
 17     Dl_info info;
 18     dladdr(__builtin_return_address(0), &info);
 19     printf("[-] %s\n", info.dli_sname);
 20 }



- line #1,2 include headers for dl* apis needed to get symbol name from address

- line #4 - tell gcc to not inject calls to enter/exit apis in profiler functions

- line #9 - get current stack frame address and pass it to dladdr to get symbol name. From gcc docs:
Built-in Function: void * __builtin_return_address (unsigned int level)
This function returns the return address of the current function, or of one of its callers. The level argument is number of frames to scan up the call stack. A value of 0 yields the return address of the current function, a value of 1 yields the return address of the caller of the current function, and so forth. 


Now compile with:
$ gcc -finstrument-functions hw.c -ldl -rdynamic // generates a.out

or to compile and link separately:
$ gcc -finstrument-functions -c hw.c -o hw.o   // generates hw.o
$ gcc  hw.o  -ldl -rdynamic                             // generates a.out


Finally run the executable:

$ ./a.out
[+] main
HW
[-] main

Now we get symbols too and overhead is much less :)

I added some sample code on how to add code for main, shared objects - check it out at:

References:

1. https://lwn.net/Articles/370423/



Sunday, 7 April 2019

Learn PostgreSQL - Full Course for Beginners By freeCodeCamp

Harry


Learn PostgreSQL - Full Course for Beginners By freeCodeCamp.

Learn How To Use PostgreSQL In This Full Course. PostgreSQL Is A General Purpose And Object-Relational Database Management System. It Is The Most Advanced Open Source Database System Widely Used To Build Back-End Systems.


SHARE BY GK

JavaScript Full Course | JavaScript Tutorial For Beginners By Edureka

Harry


JavaScript Full Course | JavaScript Tutorial For Beginners By Edureka.

This Edureka Video Is A Complete Guide To Javascript Which Covers In-Depth Knowledge About Data Types, Variables, Functions, Event Handling, Form Validation, Etc. Using Javascript.

Topics Covered In This Video Are:
  • What Is Javascript
  • Data Types In Javascript
  • Operators In Javascript
  • Conditional Statements In Javascript
  • Loops In Javascript
  • Functions In Javascript
  • Event Handling In Javascript
  • Bowser Object Model
  • Document Object Model
  • Form Validation

SHARE BY GK

ReasonML Programming - Full Course for Beginners By freeCodeCamp

Harry


ReasonML Programming - Full Course for Beginners By freeCodeCamp.

Reasonml Is An Object-Functional Programming Language Created At Facebook. Reasonml Lets You Write Simple, Fast And Quality Type Safe Code While Leveraging Both The Javascript & Ocaml Ecosystems.

In This Full Tutorial Course, You Will Learn Everything You Need To Know About Reasonml (Aka Reason) And You Will Be Able To Start Building Awesome Things With It.


SHARE BY GK

Monday, 25 March 2019

Microsoft Excel Tutorial for Beginners By Edureka

Harry


Microsoft Excel Tutorial for Beginners By Edureka.

The Beginner's Guide to Excel | Microsoft Excel Tutorial for Beginners | Excel Training | Edureka

This Edureka Video Is A Complete Guide To Microsoft Excel Which Covers In-Depth Knowledge About Different Formulas, Conditional Formatting, Lookup, Macros & VBA In Ms Excel.


SHARE BY GK

Tuesday, 19 March 2019

How to Make Appointment Booking Website with WordPress - For Doctors, Lawyers By Nayyar Shaikh

Harry


How to Make Appointment Booking Website with WordPress - For Doctors, Lawyers By Nayyar Shaikh.

How to Make an Appointment Booking Website with WordPress & OnSchedule Theme - For Doctors, Lawyers, Barbers, Beauty Parlors & Spa, Business Consultants etc. Accept Payments Online and also Sync you Appointments with Google Calendar, Apple Calendar, Yahoo & Outlook Calendar 2019.

SiteGround :- https://bloggdude.com/sg (Get 60% OFF Link)

Tmd Hosting :- https://bloggdude.com/tmd (Use NAYYAR7 for 7% Discount)

OnSchedule Theme :- https://bloggdude.com/os

Video Step By Step :
  • Introduction
  • Getting Fastest Hosting
  • Installing FREE SSL
  • Instaing WordPress
  • Important WordPress Settings
  • Theme & Plugins
  • WooCommerce Quick Setup
  • Booking Settings
  • WooCommerce Settings
  • Tax Settings
  • Payment Setup
  • Home Page Creation
  • Home Page 1st Section
  • Home Page 2nd Section
  • Home Page 3rd Section
  • Home Page 4th Section
  • Home Page 5th Section
  • Home Page 6th Section
  • About Page
  • Contact Page
  • FAQ Page
  • Menu
  • Footer
  • Demo & Sync Calendar with Google & Apple

SHARE BY GK

Free Online Sales Training & Selling Techniques By Intellectual Indies

Harry



Free Online Sales Training & Selling Techniques By Intellectual Indies.

Sales Training | Selling Techniques | Straight Line Persuasion | Selling From The Heart

Sales Training Involves The Personal Development Of Skills And Techniques Related To Creating And Exploring New Sales Opportunities, As Well As Closing Sales For An Organization.


SHARE BY GK
Computer Knowledge

Monday, 18 March 2019

Complete Data Science Course | Data Science for Beginners By Edureka

Harry


Complete Data Science Course | Data Science for Beginners By Edureka.

This Edureka Video On "Data Science" Provides An End To End, Detailed And Comprehensive Knowledge On Data Science. This Data Science Video Will Start With Basics Of Statistics And Probability And Then Move To Machine Learning And Finally End The Journey With Deep Learning And AI.


SHARE BY GK
Computer Knowledge

Friday, 15 March 2019

Arduino Tutorial For Beginners By ProgrammingKnowledge

Harry




Arduino Tutorial For Beginners By ProgrammingKnowledge.

Welcome To This Course On Arduino Tutorial For Beginners. This Video Provides The Introduction To Arduino. Arduino Is An Open-Source Hardware Platform Based On An Atmel AVR 8-Bit Microcontroller And A C++ Based IDE. Arduino Is The Go-To Gear For Artists, Hobbyists, Students, And Anyone With A Gadgetry Dream. Arduino Microcontrollers Are Programmed Using The Arduino IDE (Integrated Development Environment). Arduino IDE Can Be Downloaded For Free From http://arduino.cc/en/ Main/Software.

Arduino Programs, Called “Sketches”, Are Written In A Programming Language Similar To C And C++. Many Sensors And Other Hardware Devices Come With Prewritten Software – Look On-Line For Sample Code, Libraries (Of Functions), And Tutorials. Learn The Arduino Platform And Programming Language To Create Robots, Interactive Art Displays, Electronic Gadgets And Much More.

Click Here To Enroll Now For Free:- https://goo.gl/iFjY4e


SHARE BY GK
Computer Knowledge

How To Make A Listing, Directory & Classified Website With WordPress & ListingPro 2019 Tutorial By Nayyar Shaikh

Harry


How To Make A Listing, Directory & Classified Website With WordPress & ListingPro 2019 Tutorial By Nayyar Shaikh.

How to Make a Listing, Directory & Classified Website with WordPress & ListingPro 2019 Tutorial. Make Money Online! Website Like Olx, Yelp and Foursquare - ListingPro, Listify WordPress theme Tutorial.

SiteGround :- https://bloggdude.com/sg (Get 60% OFF Link)

Tmd Hosting :- https://bloggdude.com/tmd (Use NAYYAR7 for 7% Discount)

ListingPro Theme :- https://bloggdude.com/listing

A2 Hosting :- https://bloggdude.com/a2 (Get 51% OFF Link)

Video Step By Step :
  • Introduction
  • Getting Best Domain & Hosting
  • Installing FREE SSL
  • Installing WordPress
  • Important Settings
  • ListingPro Theme Setup
  • Creating Home Page
  • Listing Categories
  • Listing Icons
  • Features
  • Locations
  • Form Fields
  • Getting Google API
  • Add Listing
  • Home Locations
  • Creating Blogs
  • Contact Page
  • Blog Page
  • Menu & Header
  • Email Management
  • Payment Integration
  • PayPal Integration
  • Pricing Plan
  • Demos & Ad Management

SHARE BY GK

React Native Web Full App Tutorial - Build A Workout App for iOS, Android, And Web By freeCodeCamp

Harry


React Native Web Full App Tutorial - Build A Workout App for iOS, Android, And Web By freeCodeCamp.

Learn To Use React Native For Web To Create A Workout App That Works On Android, Ios, And The Web. The App Uses Mobx, Typescript, React Navigation, React Hooks, Asyncstorage / Localstorage, And More. Once The App Is Complete, You Will Learn How To Deploy It To Netlify.

Code: https://github.com/benawad/react-native-web-series

Contents
  • Setting Up a React Native Web Project
  • Setting Up React Native Hooks
  • Setting Up a React Native Web Monorepo
  • Configuring React Native for Yarn Workspaces
  • How to use Mobx with React Hooks
  • Navigation in React Native Web
  • Styling a Component in React Native Web
  • Mobx Root Store
  • Workout Timer with Mobx
  • React Router with React Native Web
  • How to Persist Mobx Stores
  • Storing and Displaying Workout History
  • React Router Params in React Native Web
  • Floating Action Button React Native Web
  • Deploy React Native Web to Netlify

SHARE BY GK

The Complete VeraCrypt Encryption Tutorial By HackerSploit

Harry


The Complete VeraCrypt Encryption Tutorial By HackerSploit.

In This Video, HackerSploit Will Be Showing You How To Use Veracrypt To Create Encrypted Volumes And How To Encrypt Drives.

Links Used In Video: https://veracrypt.ft/en/


SHARE BY GK

How To Make A Custom Website from Scratch Using WordPress (Theme Development) - 2019 By freeCodeCamp

Harry


How To Make A Custom Website from Scratch Using WordPress (Theme Development) - 2019 By freeCodeCamp.

Learn To Create A Completely Responsive Website From Scratch Using WordPress, HTML, CSS, Java Script, And PHP. Also, Learn To Create A Custom WordPress Theme.

After Watching This Course You Will Have A Solid Understanding Of How WordPress Works And How To Create Your Own Custom Theme In WordPress.

Design Source Code: https://github.com/Godsont/Responsive-Website-Design

Theme Source Code: https://gtcoding.net/product/gtcoding-wp-custom-theme/

Contents
  • Intro and Demo
  • Research & Planning
  • UI Design - The Front Page
  • UI Design - CSS
  • UI Design - JavaScript
  • UI Design - More Pages
  • PHP Basics
  • Installing WordPress locally
  • WordPress - style.css file
  • WordPress - linking CSS and JS
  • WordPress - Custom Post Type
  • WordPress - Template Hierarchy
  • WordPress - while loop
  • WordPress - the Front Page
  • WordPress - Blogs Index Page
  • WordPress - Single Post Page & Comments
  • WordPress - Sidebar & Search
  • WordPress - Contact Form
  • WordPress - Custom 404 Page
  • Finishing Up

SHARE BY GK

Saturday, 9 March 2019

How To Create An E-Commerce Website With WordPress & WooCommerce - Online Store - Divi 2019 By Nayyar Shaikh

Harry


How To Create An E-Commerce Website With WordPress & WooCommerce - Online Store - Divi 2019 By Nayyar Shaikh.

Important Links:

SiteGround :- https://bloggdude.com/sg (Get 60% OFF Link)

Divi Theme Download :- https://bloggdude.com/divi

Divi WooCommerce Builder :- https://bloggdude.com/diviwc

TMD Hosting :- https://bloggdude.com/tmd (Use NAYYAR7 for 7% Discount)

A2 Hosting :- https://bloggdude.com/a2 (Get 51% OFF Link)

Video Step By Step :
  • Introduction
  • Getting Best Hosting
  • Installing WordPress
  • Settings to do after installing WP
  • Installing Divi Theme
  • WooCommerce Quick Setup
  • General WooCommerce Settings
  • Tax Settings
  • Shipping Setup
  • Payment Gateways
  • Accounts & Privacy
  • WooCommerce Emails
  • Advanced WooCommerce Settings
  • Creating Products
  • Creating Variable Products
  • Creating Home Page
  • Slider on Home
  • Home 2nd Section
  • Home 3rd Section
  • Home 4th Section
  • Newsletter Section
  • Blog
  • Footer
  • About Page
  • Contact
  • Shop Page
  • Product Page Design
  • Menu
  • Header & Footer
  • Demo Payment

SHARE BY GK

Intro To Data Science - Crash Course for Beginners By freeCodeCamp

Harry


Intro To Data Science - Crash Course for Beginners By freeCodeCamp.

Learn The Basic Components Of Data Science In This Crash Course For Beginners. You Will Learn About:

1. Statistics: We talk about the Types of Data You'll Encounter, Types of Averages, Variance, Standard Deviation, Correlation, and And More.

2. Data Visualization: We Talk About Why We Need To Visualize Our Data, And The Different Ways Of Doing It (1 Variable Graphs, 2 Variable Graphs And 3 Variable Graphs.)

3. Programming: We Talk About Why Programming Helps Us With Data Science Including The Ease Of Automation And Recommended Python Libraries For You To Get Started With Data Science.

Contents :
  • Introduction
  • Statistical Data Types
  • Types of Averages
  • Spread of Data
  • Quantiles and Percentiles
  • Importance of Data Visualization
  • One Variable Graphs
  • Two Variable Graphs
  • Three and Higher Variable Graphs
  • Programming

SHARE BY GK

Friday, 1 March 2019

Big Data Engineer Job, Resume, Salary | Big Data Certification By Edureka

Harry


Big Data Engineer Job, Resume, Salary | Big Data Certification By Edureka.

This Edureka Video On "Big Data Engineer Job, Resume, Salary" Will Tell You Various Salary Perspectives Of Big Data Engineer, Their Job Responsibilities & How To Prepare A Resume.

It Includes The Following Topics:
  • Big Data Engineer
  • Big Data Engineer Job Trends
  • Big Data Engineer Salary
  • Big Data Engineer Responsibilities
  • Big Data Engineer Skills
  • How to Acquire Those Skills?

SHARE BY GK