Skip to main content

How to use gdb in Ubuntu Terminal

Here's a practical, minimal guide for using GDB in Ubuntu terminal — from launching to setting breakpoints and navigating your code. 🧰 Step 1: Compile with Debug Symbols Use -g and disable optimizations ( -O0 preferred for debugging): bash Copy Edit g++ -g -O0 -o myprog myprog.cpp 🚀 Step 2: Launch GDB bash Copy Edit gdb ./myprog You'll see a (gdb) prompt. 🎯 Step 3: Set Breakpoints You can set breakpoints in three main ways : ✅ a. At a function gdb Copy Edit (gdb) break main ✅ b. At a specific file and line gdb Copy Edit (gdb) break myprog.cpp:42 ✅ c. At a method in a class gdb Copy Edit (gdb) break MyClass::doSomething If the symbol is mangled (C++), GDB usually demangles automatically, especially if compiled with -g . ▶️ Step 4: Run the Program gdb Copy Edit (gdb) run 🧭 Step 5: Navigate Command Meaning next or n Step over current line step or s Step into function call finish Run until current function returns continue or c Continue until...

Draft of Java code to save NSE Historical Data in file System instead of Database, Just For TRY! Not yet tested at all

package entitity;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.stream.Stream;

public class HD implements Serializable {
public static void main(String args[]) {
String csvFilePath = "D:\\nseEquityHistoricalDataCsv\\20200116\\"
+ "Quote-Equity-INFRATEL-EQ-14-03-2020-to-14-06-2020.csv";
String dbPath = "D:\\javaWebServers\\ApacheTomEE\\nseeq\\";
String hdTablePath = dbPath + "hd.tb", hdLogPath = dbPath + "hd.lg";

try (Stream<String> stream1 = Files.lines(Paths.get(csvFilePath));
Stream<String> stream2 = Files.lines(Paths.get(csvFilePath))) {
String[] headerLineArr = stream1.findFirst().get().toString().replaceAll("\"", "").split(",");
for (Integer i = 0; i < headerLineArr.length; i++) {
headerLineArr[i] = headerLineArr[i].trim();
}
stream2.skip(1).forEach(e -> {
HD hd = new HD("INFRATEL", headerLineArr, e.replaceAll("\"", ""));
System.out.println(hd.toString());
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

LocalDate d;
String s;
String series;
Double pc, o, h, l, ltp, c;
Long v;
Double turnover;
Double _52WH, _52WL;
Long noOfTrades;
Boolean delFlag = false;

private static final DateTimeFormatter dft = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
private static final Integer logFileHeapSize = 512;

public HD(String s, String[] headerLineArr, String line) {
String[] lineArr = line.replaceAll("\"\"", "").split(",");
HashMap<String, String> row = new HashMap<String, String>();
for (Integer i = 0; i < headerLineArr.length; i++) {
row.put(headerLineArr[i], lineArr[i].trim());
}
// this.d=LocalDate.parse(row.get("Date"),dft);
this.d = LocalDate.parse(lineArr[0], dft);
this.s = s;
this.series = row.get("series");
this.pc = Double.valueOf(row.get("PREV. CLOSE"));
this.o = Double.valueOf(row.get("OPEN"));
this.h = Double.valueOf(row.get("HIGH"));
this.l = Double.valueOf(row.get("LOW"));
this.ltp = Double.valueOf(row.get("ltp"));
this.c = Double.valueOf(row.get("close"));
this.v = Long.valueOf(row.get("VOLUME"));
this.turnover = Double.valueOf(row.get("VALUE"));
this._52WH = Double.valueOf(row.get("52W H"));
this._52WL = Double.valueOf(row.get("52W L"));
this.noOfTrades = Long.valueOf(row.get("No of trades"));
}
    
        //default constructor and other constructers omitted for brevity
// getters and setters omitted for brevity

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((d == null) ? 0 : d.hashCode());
result = prime * result + ((s == null) ? 0 : s.hashCode());
result = prime * result + ((series == null) ? 0 : series.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
HD other = (HD) obj;
if (d == null) {
if (other.d != null)
return false;
} else if (!d.equals(other.d))
return false;
if (s == null) {
if (other.s != null)
return false;
} else if (!s.equals(other.s))
return false;
if (series == null) {
if (other.series != null)
return false;
} else if (!series.equals(other.series))
return false;
return true;
}

@Override
public String toString() {
// omitted for brevity
}

public static void deFragmentTable(String filePath) throws IOException, ClassNotFoundException {
// set names of logFile and tmp file
String logFilePath = filePath.substring(0, filePath.length() - 2) + "lg";
if (Files.size(Paths.get(logFilePath)) < logFileHeapSize * 1000000) {
return;
}
String tmpPath = filePath.subSequence(0, filePath.length() - 2) + "tmp";
// read form tb file in a set
FileInputStream fis = new FileInputStream(filePath);
ObjectInputStream ois = new ObjectInputStream(fis);
HashSet<HD> set = (HashSet<HD>) ois.readObject();
ois.close();
// update set from log file to latest values
ois = new ObjectInputStream(new FileInputStream(logFilePath));
while (true) {
try {
HD obj = (HD) ois.readObject();
set.remove(obj);
set.add(obj);
} catch (EOFException e) {
break;
}
}
// write set to tmp file
File fileTmp = new File(tmpPath, "w");
fileTmp.createNewFile();
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileTmp))) {
oos.writeObject(set);
}
// deletes old table file and log file, them rename tmp file to table file
Files.delete(Paths.get(filePath));
Files.delete(Paths.get(logFilePath));
new File(tmpPath).renameTo(new File(filePath));
}
public static void insert(String filePath, HD obj) throws Exception {
HD prevObj = getLatestPrevObject(filePath, obj);
// if object don't exists in filePath, write object in file path
if (prevObj==null) {
writeObjectInFilePath(filePath, prevObj);
} else {
// if object in deleated status in logPath, write object in logPath
if (prevObj.getDelFlag()) {
writeObjectInLogPath(filePath, prevObj);
} else {
// if object not in deleated status in filePath, throw onject already exists
// exception
throw new Exception("Object already exists");
}
}
}

public static void update(String filePath, HD obj) throws Exception {
HD prevObj = getLatestPrevObject(filePath, obj);
// if object exists and not in deleated state
if(prevObj!=null && !prevObj.getDelFlag()) {
writeObjectInLogPath(filePath, prevObj);
}else {
// throw object not exists exception
throw new Exception("object not available for update as it don't exists");
}
}
public static void delete(String filePath,HD obj) throws Exception {
HD prevObj = getLatestPrevObject(filePath, obj);
// if object exists and not in deleated state
if(prevObj!=null && !prevObj.getDelFlag()) {
prevObj.setDelFlag(true);
writeObjectInLogPath(filePath, prevObj);
}else {
// throw object not exists exception
throw new Exception("object not available for deletion as it don't exists");
}
}

public static HD findObjectInFilePath(String filePath, HD obj) throws IOException, ClassNotFoundException {
HD prevObj = null;
// check if object already exists in filePath
Boolean objectFoundInFilePath = false;
File file = new File(filePath);
file.createNewFile();
ObjectInputStream isFilePath = new ObjectInputStream(new FileInputStream(file));
while (!objectFoundInFilePath) {
try {
HD obj2 = (HD) isFilePath.readObject();
if (obj.equals(obj2)) {
prevObj = obj2;
objectFoundInFilePath = true;
}
} catch (EOFException e) {
break;
}
}
isFilePath.close();
return prevObj;
}

public static void updateLatestObjectInLogPath(String filePath, HD prevObj)
throws IOException, ClassNotFoundException {
// if object exists in filePath, get this final object in logPath
String logFilePath = filePath.substring(0, filePath.length() - 2) + "lg";
File logFile = new File(logFilePath);
logFile.createNewFile();
ObjectInputStream isLogPath = new ObjectInputStream(new FileInputStream(logFilePath));
while (true) {
try {
HD obj2 = (HD) isLogPath.readObject();
if (prevObj.equals(obj2)) {
prevObj = obj2;
}
} catch (EOFException e) {
break;
}
}
isLogPath.close();
}

public static HD getLatestPrevObject(String filePath, HD obj) throws ClassNotFoundException, IOException {
HD prevObj = findObjectInFilePath(filePath, obj);
if (prevObj == null) {
return null;
} else {
updateLatestObjectInLogPath(filePath, prevObj);
return prevObj;
}
}
public static void writeObjectInFilePath(String filePath,HD obj) throws FileNotFoundException, IOException {
File file=new File(filePath);
ObjectOutputStream osFilePath = new ObjectOutputStream(new FileOutputStream(file));
osFilePath.writeObject(obj);
osFilePath.close();
}
public static void writeObjectInLogPath(String filePath,HD obj) throws IOException, ClassNotFoundException {
String logFilePath = filePath.substring(0, filePath.length() - 2) + "lg";
ObjectOutputStream osLogPath = new ObjectOutputStream(new FileOutputStream(logFilePath));
osLogPath.writeObject(obj);
osLogPath.close();
deFragmentTable(filePath);
}
}

Comments

Popular posts from this blog

Ubuntu Notes: Set up autossh as systemd useful-reminder commands

 sudo vim /etc/systemd/system/permanentAutossh.service ======================================== [Unit] Description=Keeps a tunnel to myOciFreeNat open After=network-online.target [Service] User=<username> Environment="AUTOSSH_GATETIME=0" ExecStart=/usr/bin/autossh -M 0 -q -N -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -L 4322:127.0.0.1:5432 -i /<ssh_private_key_file_absolute_path> <username>@<ip_address> -p 22 [Install] WantedBy=multi-user.target ======================================== sudo systemctl daemon-reload sudo systemctl enable permanentAutossh sudo systemctl start permanentAutossh ================== sudo systemctl daemon-reload sudo systemctl restart permanentAutossh sudo systemctl status permanentAutossh ====================== sudo systemctl stop permanentAutossh sudo systemctl disable permanentAutossh sudo rm /etc/systemd/system/permanentAutossh.service

syslog cpp programming: learning using chatGcp

  Introduction to Syslog C++ August 27, 2024 This conversation may reflect the link creator’s personalized data, which isn’t shared and can meaningfully change how the model responds. You said: I am new to syslog in cpp programming n just starting. Can you teach me ChatGPT said: ChatGPT Sure! syslog is a standard for logging system messages in Unix-like operating systems. It provides a way to send log messages to the system log, which can then be collected and analyzed by various tools. Here's a basic guide to get you started with syslog in C++: 1. Include the Necessary Header To use syslog , you need to include the syslog.h header: cpp Copy code # include <syslog.h> 2. Open the Connection to syslog Before sending log messages, you need to open a connection to the syslog system. This is typically done at the start of your program: cpp Copy code openlog ( "your_program_name" , LOG_PID | LOG_CONS, LOG_USER); "your_program_name" : A string representing ...

Ubuntu - C++ Notes: Install & update boost c++ library from git (may contain some error in process mentioned below)

 Boost C++ Library Installation from git in ubuntu: 1. create a directory named boost in your desired location and go to that directory mkdir boost;cd boost; 2. clone boost recursively from git using command git clone --recursive https://github.com/boostorg/boost.git 3. cd boost; sudo ./bootstrap.sh; 4. sudo ./b2; 5. sudo ./b2 install; DONE! --------------------------- Boost C++ Library Update Verson from git in Ubuntu: 1. go to gitDownloads/boost directory cd gitDownloads/boost; 2. git pull; 3. git submodule update --recursive; 4. cd boost; sudo ./bootstrap.sh; 5. sudo ./b2; 6. sudo ./b2 install; DONE!