<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mahesh_attarde</title>
    <description>The latest articles on DEV Community by mahesh_attarde (@maheshattarde).</description>
    <link>https://dev.to/maheshattarde</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F21260%2F3bd0a9db-094b-400a-8daf-9a03ce74bc59.jpg</url>
      <title>DEV Community: mahesh_attarde</title>
      <link>https://dev.to/maheshattarde</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maheshattarde"/>
    <language>en</language>
    <item>
      <title>Essense of Machine Learning Hardware</title>
      <dc:creator>mahesh_attarde</dc:creator>
      <pubDate>Wed, 05 Apr 2023 06:12:30 +0000</pubDate>
      <link>https://dev.to/maheshattarde/essense-of-machine-learning-hardware-flg</link>
      <guid>https://dev.to/maheshattarde/essense-of-machine-learning-hardware-flg</guid>
      <description>&lt;p&gt;Interesting Notes from my work.&lt;/p&gt;

&lt;p&gt;Here are few design considerations for machine learning asic. Design traits will achieve efficiency/effectiveness per watts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Design Paramter : ML Model
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Feed-forward ANN are faster since no data feedback simplfied desgin&lt;/li&gt;
&lt;li&gt;LSTM require accumlation of partial results, increases complexity&lt;/li&gt;
&lt;li&gt;Type of Neural Network affects simplifications to hardware, Usage of domain knowledge.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Design Parameter :  Hardware use
&lt;/h3&gt;

&lt;p&gt;Common Sense Idea : Inference and Training hardware are different (most time) &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hardware for training  includes  input with large dataset, cost function , optimizations function and  model 

&lt;ul&gt;
&lt;li&gt;Large dataset implies more memory for features, bag of features&lt;/li&gt;
&lt;li&gt;cost and optimzation function use are feedback to improve model with indentifying error. implies "latency of feedback".&lt;/li&gt;
&lt;li&gt;Support for debugging model to look for training errors, generalization errors  implies "profiling interfaces" &lt;/li&gt;
&lt;li&gt;Should have support for workflows&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Hardware for inference

&lt;ul&gt;
&lt;li&gt;It includes input as dataset, not as large as training dataset and model.&lt;/li&gt;
&lt;li&gt;Model can be compressed based on domain of use.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Design Parameter:  Input Dataset
&lt;/h3&gt;

&lt;p&gt;Common Sense Idea: Use known information to minimize data set&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use of Encoding.&lt;/li&gt;
&lt;li&gt;Use of  Precise data storage. Less Register Size  implies better energy/storage efficiency
E.g. ML Model with human Age as input parameter. Ideally is 32 bit int.
but age of human is &amp;lt; 100  implies 7 bit size.
After understanding use cases, model only understand age ranges with widht of 10. implies 4 bit data.&lt;/li&gt;
&lt;li&gt;Use of Data quantization.
e.g. bfloat16 or MSFP (great common sense idea)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Design Paramter : Signal Data Processing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;For Vision, audio signals as input, FFT has saved computation significantly!!! ( WOOWWWW!) &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Design Parameter : ALU (PE) Computation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;List All Unit Operations and minimize to bare minimum required. implies minimum area&lt;/li&gt;
&lt;li&gt;For each operation minimize register size -&amp;gt; aguments datatype &lt;/li&gt;
&lt;li&gt;For each operation minimize microcode with basics
e.g. MAC Design  (Multiply and Add)
MAC for  N bit x 2 regs, with result if 2N muls and add to N again
Less Register Size, less microcode implies better energy/storage efficiency&lt;/li&gt;
&lt;li&gt;Unit Operations in ALU

&lt;ul&gt;
&lt;li&gt;Granularity of Unit Operation designed  in hardware should be Maximizing paralization,
at cost of space, later in time.&lt;/li&gt;
&lt;li&gt;Examples of Unit Operation&lt;/li&gt;
&lt;li&gt;Convolution aka (dot product) , Matrix Multiply.  (Learnings from CPU based MatMul)
lead to systolic execution of MatMul (MAC) over General MatMul (GMEM).&lt;/li&gt;
&lt;li&gt;Pooling  aka ( Normalization) , thresholding  (Learning from Comparator design in HDL)
Creating minimized Comparator logic ckt. (Spatial Reduction Case)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Design Parameter  : MOV data
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; More Cycles to get data, implies More stalls in ALU, implies high Energy&lt;/li&gt;
&lt;li&gt; More frequent access to  to distant memory in Memory heirarchy, Explosively increases energy consumption&lt;/li&gt;
&lt;li&gt; Always Pipeline data, Move data in Spatial and Temporal Ways.

&lt;ul&gt;
&lt;li&gt;Machine Learning Programming Langauge needs Tensor Loops for Time and Space maximization&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt; Decoding Cost of MOV

&lt;ul&gt;
&lt;li&gt;Reg &amp;lt; Cache &amp;lt;  Buffer &amp;lt; DRAM &lt;/li&gt;
&lt;li&gt;Large Register size better&lt;/li&gt;
&lt;li&gt;Double Buffering is good&lt;/li&gt;
&lt;li&gt;RAM technology. SRAM, DRAM, HBM&lt;/li&gt;
&lt;li&gt;Buffer Specialization, Read only buffers and write only buffer perform better than R/W&lt;/li&gt;
&lt;li&gt;Quantifiable Design Concept&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Co-designing PE-MEM
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; For Typical Operation, 2x DDR MOV RD to Reg, 1 MAC, 1x DDR MOV WR from Reg&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Considering NN Type, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Calculate Order of Execution and Data Memory Access Patterns 

&lt;ul&gt;
&lt;li&gt;idea check : Why Build Simulator that identifies Patterns of Data movements!!!&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;For Accumlation PE Pattern,  ADDER Tree implementation and SYSTOLIC accumlation for matrices&lt;/li&gt;

&lt;li&gt;For Non-Accumation Pattern, Direct Wiring multicast and SYSTOLIC multicast for matrices&lt;/li&gt;

&lt;li&gt;Keep Stationary Data in closest register,without movements if possible

&lt;ul&gt;
&lt;li&gt;idea check : Register Allocation feedback based based on Spill factor and Movement factor.&lt;/li&gt;
&lt;li&gt;Move Input into Register, Keep Weights stationary or  Keep Partial Sums and Output Stationary.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Scale MAC and LOCAL Memory up.&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Design parameter : ISA Decoder
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;CPU/GPU Decoders are complex for ML Ops.inefficiency into  Pipelining Operations  CKT&lt;/li&gt;
&lt;li&gt;Short Decoder sequences better, small, efficient for only Unit Ops.&lt;/li&gt;
&lt;li&gt;SIMD decoder &amp;lt;&amp;gt; VLIW decoder ckt&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Design Parameter : Compiler Support
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Input (Tensor) Slicing at programming lang and backend level &lt;/li&gt;
&lt;li&gt;Re-organize Loop order, polyhydrals for better memory movements, split loops into more loops.&lt;/li&gt;
&lt;li&gt;Optimize for Area x Energy &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Design Paramter : Choice of Hardware transistor tech
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;High Frequency, high throughput results in heating&lt;/li&gt;
&lt;li&gt;Low frequency, high thougput, optimal area serves better.&lt;/li&gt;
&lt;li&gt;Diaelectric Silicon technology in nm &lt;/li&gt;
&lt;li&gt;Photonic Technology&lt;/li&gt;
&lt;li&gt;Calculate Compute Density , Compute to data movement ratio to classify IO dominant or Compute Dominant&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>mlhardware</category>
    </item>
    <item>
      <title>Notes on Code Review</title>
      <dc:creator>mahesh_attarde</dc:creator>
      <pubDate>Fri, 17 Mar 2023 10:51:43 +0000</pubDate>
      <link>https://dev.to/maheshattarde/notes-on-code-review-10pd</link>
      <guid>https://dev.to/maheshattarde/notes-on-code-review-10pd</guid>
      <description>&lt;p&gt;"Build thing right" is motivation for code reviews. Depending on context, each of following point can be extended to any review.&lt;br&gt;
My base understanding about reviewing someone code comes (Programmer Competency Matrix)&lt;br&gt;
[&lt;a href="https://sijinjoseph.com/programmer-competency-matrix/" rel="noopener noreferrer"&gt;https://sijinjoseph.com/programmer-competency-matrix/&lt;/a&gt;], which serves &lt;/p&gt;

&lt;p&gt;Here is  checklist I noted down from 2 finest engineers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Size of Code Review&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is this Single Objective change?&lt;/li&gt;
&lt;li&gt;Multiple Objective Change

&lt;ul&gt;
&lt;li&gt;is it time consuming and simple enough to review &amp;gt;15 min?

&lt;ul&gt;
&lt;li&gt;Else break it down&lt;/li&gt;
&lt;li&gt;check anti-pattern, clubbing multiple checking?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Requested Functionality Check&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unit tests 

&lt;ul&gt;
&lt;li&gt;are complete and correct ?&lt;/li&gt;
&lt;li&gt; Missing tests?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt; Algorithm

&lt;ul&gt;
&lt;li&gt;Time Complexity&lt;/li&gt;
&lt;li&gt;Space Complexity&lt;/li&gt;
&lt;li&gt;Contextual Appeal of Algorithm&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Data Structure

&lt;ul&gt;
&lt;li&gt;New Data Structure

&lt;ul&gt;
&lt;li&gt;Data Layout  ?&lt;/li&gt;
&lt;li&gt;Interfaces are stable?&lt;/li&gt;
&lt;li&gt;Is there exposed state?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Existing Data structure

&lt;ul&gt;
&lt;li&gt;Is Data Structure Abused in usage?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Domain of Application&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;Find Assumptions hidden in place-sight?&lt;/li&gt;

&lt;li&gt;Are there unhanded cases for assumptions?&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Coding Anti-Patterns&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Code Style Consistency&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Style in Sync with existing style?&lt;/li&gt;
&lt;li&gt;tool clang-format&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Language features&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Are there any Language Features abused?&lt;/li&gt;
&lt;li&gt;Can there be scope for using language features?
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Memory Issue&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is there chance of memory corruption?&lt;/li&gt;
&lt;li&gt;Are there any data races?&lt;/li&gt;
&lt;li&gt;tool val-grind&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Performance Issue&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deterioration due to integration of Algorithmic Functionality
&lt;/li&gt;
&lt;li&gt;Performance Test&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Concurrency Issue&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multi-threaded&lt;/li&gt;
&lt;li&gt;Distributed&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Contextual Appeal for Integration&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Environment variable vs command line options&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Risk of Code Change&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change affects area that is only requested , Guarded by Option with Default&lt;/li&gt;
&lt;li&gt;is there a disaster scenario ?&lt;/li&gt;
&lt;li&gt;are there deployment issues?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Architectural Change&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is there library dependency ? is it explicitly mentioned or implicit?&lt;/li&gt;
&lt;li&gt;Is Architectural change adding value over time/ maintainance?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Review of  "Language" (C++ in this Context)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check Data types used, CV qualifiers, storage specifiers&lt;/li&gt;
&lt;li&gt;Overflow and underflow conditions?&lt;/li&gt;
&lt;li&gt;New Class&lt;/li&gt;
&lt;li&gt;Checklist of Default constructors, operators&lt;/li&gt;
&lt;li&gt;check behaviors are mocked with coverage&lt;/li&gt;
&lt;li&gt; and follow up on guideline based on use cases.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Although this is checklist learnt from experienced programmer and awesome engineer known to me,&lt;br&gt;
It may be possible to think of general framework after deep thought, which any rookie can use.&lt;/p&gt;

&lt;p&gt;HTH!&lt;/p&gt;

</description>
      <category>codereview</category>
    </item>
    <item>
      <title>Code Search and Navigation with livegrep</title>
      <dc:creator>mahesh_attarde</dc:creator>
      <pubDate>Fri, 10 Mar 2023 15:48:45 +0000</pubDate>
      <link>https://dev.to/maheshattarde/code-search-and-navigation-with-livegrep-33dc</link>
      <guid>https://dev.to/maheshattarde/code-search-and-navigation-with-livegrep-33dc</guid>
      <description>&lt;h2&gt;
  
  
  Using Code Search : Livegrep
&lt;/h2&gt;

&lt;p&gt;After trying out bunch of indexing (tag) tools production/command-line, one that index git repo, uses ngram, has web frontend, includes docker setup. It is code search that just works!&lt;/p&gt;

&lt;p&gt;(Original Github) [&lt;a href="https://github.com/livegrep/livegrep" rel="noopener noreferrer"&gt;https://github.com/livegrep/livegrep&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;This are instructions to run source indexer&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to Source Directory&lt;/li&gt;
&lt;li&gt;Clone repo and output index file with idx suffx current directory, mouted as data on container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -v ${PWD}:/data ghcr.io/livegrep/livegrep/indexer /livegrep/bin/livegrep-github-reindex -repo doxygen/doxygen -http -dir /data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create Network
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker network create livegrep
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create backend process load it with idx file and accept RPC call at grpc
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d --rm -v ${PWD}:/data --network livegrep --name livegrep-backend ghcr.io/livegrep/livegrep/base /livegrep/bin/codesearch -load_index /data/livegrep.idx -grpc 0.0.0.0:9999
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Connect web app and backend process and publish web application
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d --rm --network livegrep --publish 8910:8910 ghcr.io/livegrep/livegrep/base /livegrep/bin/livegrep -docroot /livegrep/web -listen 0.0.0.0:8910 --connect livegrep-backend:9999
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Using It&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Connect to "0.0.0.0:8910" of web application&lt;/li&gt;
&lt;li&gt;Search for Code literal&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Best Parts&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;LIVE GREPPING! Grep Works as you type. faster than egrep,grep&lt;/li&gt;
&lt;li&gt;Click on Search, it takes you to github file with anchor! :D&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;HTH!&lt;/p&gt;

</description>
      <category>codesearch</category>
      <category>livegrep</category>
    </item>
    <item>
      <title>[GDB-Quick] Using Eclipse Standalone debugger GUI</title>
      <dc:creator>mahesh_attarde</dc:creator>
      <pubDate>Thu, 22 Sep 2022 14:08:09 +0000</pubDate>
      <link>https://dev.to/maheshattarde/using-eclipse-standalone-debugger-gui-1k08</link>
      <guid>https://dev.to/maheshattarde/using-eclipse-standalone-debugger-gui-1k08</guid>
      <description>&lt;p&gt;GDB is debugger that comes with basic functionality of doing everything command line. GDB-TUI provides basic Text UI for debugging source codes with minimum dependencies. At times, Window-GUI is much help to look at much information at snapshot, edit it, view it without explicitly typing all commands and shortcuts.&lt;br&gt;
Here is how we can use eclipse-cdt as standalone debugger.&lt;br&gt;
No need to create projects!&lt;/p&gt;

&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Download Eclipse-CDT package&lt;br&gt;
Unzip  Eclipse-CDT.zip&lt;br&gt;
switch to unzipped folder eclipse\plugins\org.eclipse.cdt.debug.application_*\s&lt;br&gt;
cripts&lt;br&gt;
/bin/sh  ./cdtdebug.sh&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;

&lt;p&gt;This will install debugger into your $HOME/cdtdebugger/ area  &lt;/p&gt;

&lt;p&gt;Now Connect to local process with GUI prompt. No need to list process on terminal then type process id!&lt;br&gt;
$HOME/cdtdebugger/cdtdebug.sh  -a&lt;/p&gt;

&lt;p&gt;Connect Remote Process&lt;br&gt;
$HOME/cdtdebugger/cdtdebug.sh  -r address:port&lt;/p&gt;

&lt;p&gt;Debug binary&lt;br&gt;
$HOME/cdtdebugger/cdtdebug.sh  -e executable&lt;/p&gt;

&lt;p&gt;Read More at [&lt;a href="https://wiki.eclipse.org/CDT/StandaloneDebugger" rel="noopener noreferrer"&gt;https://wiki.eclipse.org/CDT/StandaloneDebugger&lt;/a&gt;]&lt;br&gt;
[&lt;a href="https://www.infoq.com/presentations/best-practices-cdt-debugger/" rel="noopener noreferrer"&gt;https://www.infoq.com/presentations/best-practices-cdt-debugger/&lt;/a&gt;]&lt;br&gt;
Enjoy!&lt;br&gt;
HTH!&lt;/p&gt;

</description>
      <category>debug</category>
      <category>cpp</category>
    </item>
    <item>
      <title>[GDB-Quick] Prints – No Need to do "std::cout" and compile again</title>
      <dc:creator>mahesh_attarde</dc:creator>
      <pubDate>Fri, 21 Jan 2022 16:59:09 +0000</pubDate>
      <link>https://dev.to/maheshattarde/gdb-quicks-print-info-no-need-to-do-stdcout-and-compile-again-52lc</link>
      <guid>https://dev.to/maheshattarde/gdb-quicks-print-info-no-need-to-do-stdcout-and-compile-again-52lc</guid>
      <description>&lt;p&gt;For debugging, most obvious way to start is adding print. &lt;br&gt;
When we have gdb, we dont need that (implied software built is in debug mode). &lt;br&gt;
Lets see alternate ways to do that in gdb.&lt;/p&gt;
&lt;h3&gt;
  
  
  Printing Variable at some line.
&lt;/h3&gt;

&lt;p&gt;We have breakpoints at hand. when breakpoint hits we can print variable values required or message needed (like which if-else branch taken (-&lt;em&gt;q), ¯_(ツ)&lt;/em&gt;/¯ ).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(gdb) break source.cpp:50  &amp;lt;enter&amp;gt;
(gdb) command &amp;lt;enter&amp;gt;
Type commands for breakpoint(s) 1, one per line.
End with a line saying just "end".
&amp;gt;print "hello World!" 
&amp;gt;end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When breakpoint hits on source file source.cpp at line 50 It prints "hello World!". No need to edit code or recompile it!&lt;/p&gt;

&lt;p&gt;On breakpoint, we specify series of commands that need to process. it can be made simple as printing single variable to printing complete data-structure.&lt;/p&gt;

&lt;p&gt;Same can be done with dprintf breakpoint and print, however first one provides more flexible and easy to use formatting. I hate dprintf!&lt;/p&gt;

&lt;h3&gt;
  
  
  Print but more efficient
&lt;/h3&gt;

&lt;p&gt;Our first command of use is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(gdb) print a
(gdb) p a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is fine, even if we have array. &lt;br&gt;
At times we are interested in buffer contents irrespective of its data structure, treating it like memory area.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(gdb) print &amp;amp;a@10 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above command accepts address and 10 further locations from it. try  a@10 and see how cool that feature becomes!&lt;/p&gt;

&lt;p&gt;same with different formatting, printing 20('n'umber) 'f'ormat 'w'ords  with address a&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(gdb) x/20fw &amp;amp;a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also want to check same variable at different breakpoints&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;display &amp;lt;var_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is easiest way than doing p  everytime.&lt;/p&gt;

&lt;p&gt;HTH, Happy Hacking!&lt;/p&gt;

</description>
      <category>c</category>
      <category>cpp</category>
      <category>gdb</category>
    </item>
    <item>
      <title>[GDB-Quick] BreakPoints - Log/Source</title>
      <dc:creator>mahesh_attarde</dc:creator>
      <pubDate>Mon, 17 Jan 2022 16:00:43 +0000</pubDate>
      <link>https://dev.to/maheshattarde/gdb-quick-breakpoints-logsource-10jk</link>
      <guid>https://dev.to/maheshattarde/gdb-quick-breakpoints-logsource-10jk</guid>
      <description>&lt;p&gt;For debugging, most obvious way to start is adding print. Second obvious is putting breakpoints and printing. While as simple as it sounds, it may not useful with large scale software variety of architecture.&lt;/p&gt;

&lt;p&gt;While each debugging can take several runs, we need efficient setup, hence following post.&lt;/p&gt;

&lt;h3&gt;
  
  
  Save Your Breakpoints for future.
&lt;/h3&gt;

&lt;p&gt;Save breakpoints in file with command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(gdb) save breakpoints bpt_file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rookie stuff!&lt;br&gt;
Here is more,  What is right time to save breakpoint?  :P&lt;br&gt;
For System level debug, create local breakpoints files is efficient, at times multiple ones, depending on debugging flow.&lt;/p&gt;

&lt;p&gt;This save is similar as gdb commands, so edit bpt_file in text editor is required. &lt;/p&gt;
&lt;h3&gt;
  
  
  Load Breakpoints from history
&lt;/h3&gt;

&lt;p&gt;bpt_file is gdb commands, source it with command line or in gdb, it works!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(gdb) source bpt_file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  For VIM users
&lt;/h3&gt;

&lt;p&gt;You can avoid writing breakpoint descriptions like file:line or class::function with few key strokes. Assign KeyMap from VIM to log current file:line_num in bpt_file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;break file:line
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now VimScript to do that is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function! Brkpt(message, file)
  new
  setlocal buftype=nofile bufhidden=hide noswapfile nobuflisted
  put=a:message
  execute 'w ' a:file
  q
endfun

function! BrkLog(fileName,lineNum)
  let msg = 'break' .a:fileName . ':' . a:lineNum
  echo msg
  call Brkpt(msg,'~/gdb.txt')
endfun
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;copy content into say brk.vim file and source in vim. call this function on keystroke.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:nomap &amp;lt;C-G&amp;gt; :call BrkLog(expand('%:t'),line('.'))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last While sourcing breakpoints, libraries that are not loaded will default error then just&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(gdb) set breakpoint pending on
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Happy Hacking!&lt;/p&gt;

</description>
      <category>gdb</category>
      <category>cpp</category>
      <category>c</category>
    </item>
    <item>
      <title>[DopeTales] HackScripts Memory Allocation Testing</title>
      <dc:creator>mahesh_attarde</dc:creator>
      <pubDate>Thu, 23 Sep 2021 13:42:06 +0000</pubDate>
      <link>https://dev.to/maheshattarde/hackscripts-memory-testing-1f3l</link>
      <guid>https://dev.to/maheshattarde/hackscripts-memory-testing-1f3l</guid>
      <description>&lt;p&gt;Very often we want to test maximum memory that can be allocated at user space. It has interesting use cases. Mine relates to 4gb memory shadow creation. Basically I implemented sample DDR of size 4gb and took snapshot of simulated memory at intervals. so i need to know before hand if i have that much memory available before running actual application. Following script allows me to do that in simple loops.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#define MINREQ      0xFFF   // arbitrary minimum
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// adapt to native uint&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;mem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mem&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Required %X&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;mem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;MINREQ&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;free&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Cannot allocate enough memory&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;free&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mem&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Cannot enough allocate memory  %d &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Memory size allocated = %X&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;  &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;free&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It tries to allocate minimum size then goes on continuously till largest possible at current moment.&lt;/p&gt;

&lt;p&gt;Happy Hacking!&lt;/p&gt;

</description>
      <category>malloc</category>
      <category>cpp</category>
      <category>hackscripts</category>
    </item>
    <item>
      <title>[DopeTales] GDB Hit Counter Script for nth time break on function</title>
      <dc:creator>mahesh_attarde</dc:creator>
      <pubDate>Wed, 22 Sep 2021 17:47:45 +0000</pubDate>
      <link>https://dev.to/maheshattarde/dopetales-gdb-hit-counter-script-for-nth-time-break-on-function-1fac</link>
      <guid>https://dev.to/maheshattarde/dopetales-gdb-hit-counter-script-for-nth-time-break-on-function-1fac</guid>
      <description>&lt;p&gt;Some useful hack around gdb. While debugging we often want to wait until certain function hits nth time. Following script does that, in easy python way.&lt;/p&gt;

&lt;h6&gt;
  
  
  Code Explanation
&lt;/h6&gt;

&lt;p&gt;We have HitMe class which inherits from gdb.BreakPoint which is classes defined in gdb python extension. It provides two methods &lt;strong&gt;init&lt;/strong&gt; will define breakpoint on function and handler on hitting breakpoint. Return value of Stop function tells gdb halt or not.&lt;br&gt;
hitCount is just counter to keep track of number of function hits.&lt;br&gt;
In code BREAK_FUNCTION hits 40 times before breakpoint halts gdb.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# script.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;traceback&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;gdb&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="n"&gt;hitCount&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HitMe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gdb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Breakpoint&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;gdb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Breakpoint&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;BREAK_FUNCTION&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;hitCount&lt;/span&gt;
        &lt;span class="n"&gt;hitCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hitCount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hitCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;hitCount&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ran Counter For Function!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;hit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HitMe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running This script&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gdb -xargs binary.o arg1
...
gdb&amp;gt; source script.py 
gdb&amp;gt; run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will be quickest way to get there! Happy Hacking!!!&lt;/p&gt;

</description>
      <category>gdb</category>
    </item>
    <item>
      <title>[DopeTales] Pairing Functions </title>
      <dc:creator>mahesh_attarde</dc:creator>
      <pubDate>Thu, 11 Apr 2019 05:55:41 +0000</pubDate>
      <link>https://dev.to/maheshattarde/dopetales-pairing-functions-3c6f</link>
      <guid>https://dev.to/maheshattarde/dopetales-pairing-functions-3c6f</guid>
      <description>

&lt;p&gt;Pairing functions are surprisingly useful increasing  application performace.&lt;br&gt;
As per Wiki, Pairing functions  uniquely encode two natural numbers into a single natural number. &lt;br&gt;
While designing system software this feature can be exploited to reduce runtime data structures data footprint.&lt;/p&gt;

&lt;p&gt;Here is Dope-tale about one of that use.&lt;br&gt;
While design of compiler diagnostics or run time err0r checkers, we record error location at &lt;a&gt;line:column&lt;/a&gt; &lt;br&gt;
pair from user code, typical implementation for this use-case is&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;typedef std::uint32_t                             Line
typedef std::uint32_t                             Column
typedef std::pair&amp;lt;Line,Column&amp;gt;                    Location;
typedef std::map&amp;lt;Line,Location&amp;gt;               LocationTable; 
// OR
typedef std::multi_map&amp;lt;Line,Location&amp;gt;         LocationTable1;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This implemenation creates wrong semantics. map implemenation does not cover multiple errors on same location.&lt;br&gt;
While both of them fail in semantics as Line does not uniquely identify error and its location.&lt;/p&gt;

&lt;p&gt;So we define problem as&lt;/p&gt;

&lt;p&gt;0 &amp;lt;= Line &amp;lt;= 2&lt;sup&gt;32-1&lt;/sup&gt;&lt;br&gt;
 0 &amp;lt;= Column &amp;lt;= 2&lt;sup&gt;32-1&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Given &amp;lt; Line,Column &amp;gt;  --uniquely--&amp;gt; Error, We would be needing O(1) search&lt;br&gt;
with single key, like...&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;typedef std::unordered_map \&amp;lt; LocationHash,ErrorDescriptor \&amp;gt; LocationTable;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So we wish to pair two integeres into one. hence pairing functions. &lt;br&gt;
A pairing function is a &lt;em&gt;computable bijection&lt;/em&gt;&lt;br&gt;
    π : N × N → N . &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Line and Column are &lt;em&gt;natural number&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt; Practically 32 bits are enough to accommodate Line and Number&lt;/li&gt;
&lt;li&gt; Key made by both can be accommodated in 64 bits&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;here is sample implemenation&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;typedef std::uint32_t  UI32;
typedef std::uint64_t  UI64;
typedef std::uint64_t  Key64;
typedef std::uint32_t  LocationKey;
using LocationHash  = Key64;
typedef std::pair&amp;lt;LocationHash,ErrorDescriptor&amp;gt; LocationListItemPair;


struct PairHashFunction : std::binary_function&amp;lt;UI32,UI32,Key64&amp;gt;
{
    Key64 operator()(UI32 first,UI32 second) const{
        Key64 first64 = (Key64) first;
        Key64 second64 = (Key64) second;
        Key64 sum64 = (first64 + second64);
        Key64 hashValue64 = sum64 * (sum64 + 1) &amp;gt;&amp;gt; 1;
        return hashValue64 + second64;
    }
};

template&amp;lt;typename HashKeyType,typename KeyType&amp;gt;
struct UnPairHashFunctionTemplate
{
    void operator() (HashKeyType hashValue,KeyType *first, KeyType * second){
        HashKeyType temp =  (hashValue &amp;lt;&amp;lt; 3)  + 1;
        temp  =  std::sqrt(temp) - 1;
        temp  =  std::floor(temp&amp;gt;&amp;gt;1);
        HashKeyType consum =  (temp * temp  + temp) &amp;gt;&amp;gt; 1;
        *second =  hashValue -  consum;
        *first  =  temp - *second;
    }
};

typedef UnPairHashFunctionTemplate&amp;lt;Key64,UI32&amp;gt; UnPairHashFunction;

class HitTable{
    LocationTable table;
public:
    void hit(Location locKey){
        PairHashFunction hfunc;
        Key64 key = hfunc(locKey.first,locKey.second);
        auto it =  table.find(key);
        if(it == table.end()){
            table[key] = 1;
        }
        else{
            KeyCountType count  = table[key];
            table[key] = count+1;
        }
    }

    bool isHit(Location locKey){
        PairHashFunction hfunc;
        auto it =  table.find(hfunc(locKey.first,locKey.second));
        return (it ==  table.end()) ? false :true;
    }
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;HTH!&lt;/p&gt;


</description>
      <category>hashingcppmath</category>
    </item>
    <item>
      <title>[DopeTales] String operation slowdown in Source Generator Streams</title>
      <dc:creator>mahesh_attarde</dc:creator>
      <pubDate>Mon, 17 Dec 2018 17:36:37 +0000</pubDate>
      <link>https://dev.to/maheshattarde/dopetales-string-operation-slowdown-in-source-generator-streams--4p1f</link>
      <guid>https://dev.to/maheshattarde/dopetales-string-operation-slowdown-in-source-generator-streams--4p1f</guid>
      <description>&lt;p&gt;Compilers tends to manipulate readable strings way to much than any other software. Source generators written  decade ago and six months back dont really have than much difference. Here is attempt to improve that.&lt;/p&gt;

&lt;p&gt;Source Generators tend to build strings by concatenating strings. Usual go to solution for this problem is Attempt 1 &lt;/p&gt;

&lt;h4&gt;
  
  
  Attempt 1
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;oBuffer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;oBuffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ContextPrefix"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;oBuffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"_"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;oBuffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pClassName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c_str&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="n"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;oBuffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c_str&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution creates different string in memory, results in memory allocations,resizing etc. moreover tend to slow down whole software stack.&lt;br&gt;
Concatenating string with "+" operator does no good either.&lt;/p&gt;

&lt;h4&gt;
  
  
  Attempt 2
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;StringBuffer&lt;/span&gt;  &lt;span class="n"&gt;sBuffer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;sBuffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ContextPrefix"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;sBuffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"_"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;sBuffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pClassName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c_str&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="n"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;sBuffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c_str&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution was inspired by Java/CSharp followup and sadly help from Stackoverflow. While Cpp does not support this, writing own implementation of StringBuffer give much more flexibility of optimizing whole operation. Still we will end up separate buffer. &lt;/p&gt;

&lt;h4&gt;
  
  
  Attempt 3
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;PrefixString&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ostream&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ostream&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;pOut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;PrefixString&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;pPair&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="n"&gt;pOut&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;pPair&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"_"&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;pPair&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pOut&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/*Using PrefixString in Source Generator code */&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;SourceWriter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ClassBegin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;pQualifiedName&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="n"&gt;PrefixString&lt;/span&gt; &lt;span class="n"&gt;oStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_pair&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Constants&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ContextPrefix&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;pQualifiedName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;mOut&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;oStr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Third attempt is made considering fact that strings are always present in objects.&lt;br&gt;
We need not have separate buffer to create output string. In example shown above, &lt;br&gt;
PrefixString is pair which is just placeholder for char string pointers. It can directly write Prefix String without any intermediate buffer. moreover operator &amp;lt;&amp;lt; is inlined. Most of runtime decisions are moved to compile time. and Hence faster alternative.&lt;/p&gt;

&lt;p&gt;Hope this helps.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>compiler</category>
    </item>
  </channel>
</rss>
