Confing launch.json in Vscode to fire up internal gdb - c

I'm trying to configure launch.json in vscode in order to be able to debug my program inside the editor. I' m facing 126. The vscode documentation explains here, that I need to create the 10-ptrace.confe inside /etc/sysctl.d/ and append this into it :
kernel.yama.ptrace_scope = 0
I'm getting into all of these because I don't want to run vscode as root for a simple debug session.
The problem is that the file already exists and I don't have permission to write on it, even after using:
sudo chmod +w 10-ptrace.conf
Moreover, these value that I was supposed to write in the file is set to 1 instead of 0.
How can I overcome those difficulties and debug my C program?
Is there any better way to start the debug session that I should be trying instead?
Alternative question:
If I run gdb via terminal, how can I plot the addresses to find the one that my program is segfaulting ? Should I be using a disassembler or something else? I'm currently using radare, but my prior experience is with QtSpim. Do you have any suggestions ?
I appreciate any kind of help.

gdb generates assembly code that corresponds directly to your source code "optimized code"
i can suggest to try "hex editor" and edit your binary code file it's available on windows and linux.
hope that could help you

Related

VSCode issues when running a C file and displaying output

I'm trying to run C programs/files from VSCode. I've followed a guide to setting up the gcc for VSCode to use, and it "mostly" works. However, when I run a file, it only outputs to the debug console, which is rather messy. I would prefer if it only output to the "output" window.
If I click on the output window and then try to run the file, I get this error.
This is what my tasks.json file looks like, where I've tried to change build from cppbuild to shell, as well as removing the "file" lines that I saw recommended on other threads, which didn't solve my issue.
Also notice how in the drop down menu for my panel, I don't have a "Code" option that I see in other people's VSCode
TLDR: I want VSCode to display the output of a C file into the Output window, not the debug console. I also want to understand why I am given the "Cannot build and debug because the active file is not a C or C++ source file" error. MinGW has been downloaded and GCC has been set up properly, AFAIK.
Have you installed "Code Runner" extension?

Can't launch debug - C in VS code on Linux

Let me preface this, I am very new to linux and to working on a non-IDE based setup.
I am trying to debug a very simple C program using vs code version 1.55
I unloaded all modules beforehand, so vs code can load appropriate default gcc & gdb versions (which it did, GCC 8.2)
I am following the VS code getting started documentation for setting up and everything seems very straight forward until I try to debug.
I use the default settings as instructed, the file builds successfully but then I get the below
/usr/bin/gdb: symbol lookup error: /usr/bin/gdb: undefined symbol: PyUnicodeUCS4_FromEncodedObject
please note that I cannot rebuild python with ucs4 enabled as suggested in another thread as I have no root access. however I can change VS code version to an earlier one if this will help.
Thanks.
I think this issue is specific to my environment but I will post the answer anyway as it may face someone else.
So this for me was 2 separate issues:
First gdb doesn't start and second vs code can't start gdb.
To check if this is the case try to launch gdb from terminal (not vs code) by typing gdb in the terminal (after loading gdb if needed), I was receiving the error above
Solution to this part is this as T0maas thankfully suggested above
Steps for linux newbies:
ldd gdb (or /usr/bin/gdb) (with vs_code loaded)
from step one get the python library path
unload all modules
load gdb
LD_PRELOAD=<python path from 1>
bash -c "export LD_PRELOAD"
load vs_code
load gdb
After the above steps writing gdb in the terminal should start gdb
Part 2:
The rest of the problem was when I tried to launch debugging session through the GUI of vs_code (still produced the same error)
In the terminal (after loading gdb) type whereis gdb
For me this produced multiple directories the first of which was /usr/bin/gdb (this is the default used in vs_code launch.json)
Changing that directory in the launch.json file to a different one of the other directories solved the second part of the problem for me.

How to remove the file path from terminal while running a code in visual studio code (mac os )?

How to remove the file path from terminal while running a code in visual studio code ?
Im using visual studio code with CodeRunner extension in Mac air M1.
I’ve tried to play with the terminal settings and I couldn't find the one that remove this.
I want that remove the part I marked in red, when i running my program.
Welcome to StackOverflow! 👋
This line is terminal command to compile source code file and run executable.
Soooo, I am not sure if you can remove this. What you can do is write bash script and give your file as an input, but anyway you will pass your file as an argument. What you can do is configure build task and run your executable file from terminal, but I'm not sure would you like this way or not.
As others mentioned this is the gcc (your c compiler) build command. Reason: You build and run your program. This should vanish when you explicit build. And then just run your program without build. Or open Terminal and run your binary with:
cd /Users/metanpolik/Desktop/VisualProjects
./heyc2
But like #UnholySheep wrote, what is the problem? It's just during developing with vs code and should not be a problem.

C library problems installing

I am a beginner C programmer, and recently I have run into a problem that I was unable to solve.
I am struggling to install and use a C library. The message error is:
My OS is Mac OS X and im try install the file with ".command" format.
Can someone help me install this library?
Thanks in advance.
More than help installing the library, you need help in understanding what's going on.
Someone gave you a command to run and you ran it, but they were assuming that you were using a different OS, like Ubuntu.
The first message "apt-get not found" is because you're running MacOS, which doesn't have an "apt-get" command.
The second error is because you also don't have wget.
The rest of the messages indicate a poorly written script file, since it blew up twice and still went on to truncate files and do other things.
While you're not going to be able to install apt-get, you might be able to find a compatible version of wget. If not, you can download whatever it was trying to get using your browser, since wget is typically used to automate web HTTP/HTTPS requests.
Without seeing the file, I don't know what the rest if the script was trying to do, but if you read it you should be able to figure out what it was doing, and do it manually.
An easier way to do all this is to install Virtualbox on your mac and run a copy of Ubuntu inside a virtual machine. Then you can run your class assignments without needing to rewrite them all.

i want to run a program without using terminal -- is it possible ? how?

I'm having a program that will do a file copy, but does not need terminal to print.
Now i want to run the file by double clicking ./filemove but it does not do anything. Through the terminal it works fine.
I think it's because I need to include the headers and I do not know how to include the headers to that file.
Can anybody help me? I want to know how to include header files to the executable file or if that is not the reason than what else is the reason?
Thank you.
I use Ubuntu Linux OS. I didn't take any environmental variables and still i cant find it!
I'm going to take a stab at this and guess your problem has to do with file paths. On Linux you can absolutely run programs without the terminal. The terminal is just an interface to the application. The same is true of whatever Desktop Environment you are using.
I'm assuming that your filemove program is using relative paths. What I'm guessing is happening is that when you double click your application, the DE is running it from a different directory than the one it is in. This is pretty counter-intuitive but is possible. So if your program is in /home/user/myproject/ and you double click on it, it's possible that the DE is running it from /home/user/ instead.
I would recommend adding output to your program, and have it print out it's working directory. Since you are not running it on the CLI, have it print to a file.
EDIT: you could also try changing any relative file paths to absolute file paths and seeing if it works. That will at least tell you if the working directory is the problem or not.
If you want to further try to debug this, create a shell script that contains something like:
#!/bin/sh
exec strace /path/to/yourprogram "$#" >/tmp/strace.out 2>&1
Make the script executable, and run that instead of your program, look for clues in /tmp/strace.out
Make sure you've installed the strace program (apt-get install strace)
A wild guess: Are you giving arguments to your program when you run it in the terminal? As in the command ./filemove oldname newname?
In that case, you need to give those arguments to the program in some other way. In at least some window systems- you can drag and drop files on the program icon to give it the paths of those files as arguments.

Resources