How to log pid(process id) by using spdlog - pid

spdlog can log threadid by doing:
spdlog::set_pattern("*** [%H:%M:%S %z] [thread %t] %v ***");
But how can I log the pid(process id)?

This is already supported in latest versions, using the %P flag.

Related

GDB/bin/bash can't execute the app in Eclipse CDT?

Having difficulties running GDB with Eclipse CDT on Mac OS X Mojave.
When I try to debug the project, GDB behaves chaotic: I am able to debug a few times, but then it starts stalling at the begining of the next debug session with the message "Configuring GDB".
Executable is /Users/jd/Work/myapp.
When I list the processes ps -al I see there are hanging processes:
/Users/jd/Library/Caches/gdb/bin/bash -c exec /Users/jd/Work/myapp
/Users/jd/Library/Caches/gdb/bin/bash -c exec /Users/jd/Work/myapp
/Users/jd/Library/Caches/gdb/bin/bash -c exec /Users/jd/Work/myapp
/Users/jd/Work/myapp
/Users/jd/Library/Caches/gdb/bin/bash -c exec /Users/jd/Work/myapp
/Users/jd/Library/Caches/gdb/bin/bash -c exec /Users/jd/Work/myapp
/Users/jd/Work/myapp
/usr/local/bin/gdb --interpreter mi2 --nx
/usr/local/bin/gdb --interpreter mi2 --nx
/usr/local/bin/gdb --interpreter mi2 --nx
/usr/local/bin/gdb --interpreter mi2 --nx
What could be the problem? Is it GDB or Bash?
Edit
The hanging myapp processes have a TXs+ state, which, according to man pspage means:
T, marks a stopped process
X, the process is being traced or debugged
s, the process is a session leader
"+", the process is in the foreground process group of its control terminal
The hanging ggdbprocesses have a state:
S, process that is sleeping for less than about 20 seconds (never turns to I, which means it sleeps more than 20 seconds, idle.)
From the details in the question and from my past experience of a similar issue this is all I have for you,
After starting GDB, I saw a message, "launching gdb aborting configuring gdb." My environment was Mac with Eclipse + gdb (ggdb downloaded from macports).
Temporary solution that worked for me was to start eclipse as root.
To fix the issue permanenty,
Use the below link to see how to create a certificate or cross check if already configured,
Mac C++/Mars eclipse gdb debug hangs at Launching Test 96%
If my response above and link were not helpful then try the below eclipse forum to check and compare your problems to others posted and see if you can identify and curate the question further with more relevant info.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=509737

Mac kernel programming generic kernel extension prinf() not working

I've followed the Creating a Generic Kernel Extension with Xcode tutorial.
MyKext.c:
#include <sys/systm.h>
#include <mach/mach_types.h>
kern_return_t MyKext_start (kmod_info_t * ki, void * d)
{
printf("MyKext has started.\n");
return KERN_SUCCESS;
}
kern_return_t MyKext_stop (kmod_info_t * ki, void * d)
{
printf("MyKext has stopped.\n");
return KERN_SUCCESS;
}
I've also disabled the csrutil, which allow me to load my own kext.
# csrutil disable
When I load my own kext into kernel
$ sudo kextload -v /tmp/MyKext.kext
The result of printf() not write into /var/log/system.log.
I've also set boot-args
$ sudo nvram boot-args="original_contents debug=0x4"
Can anyone help me out?
Apparently, since Sierra (10.12) at least, they reorganized the way the logs are written (iOS support?), so you cannot see it in system.log anymore. Still, in your Console application, you have in the sidebar a Devices section, where you can select your device (usually your Mac system) and see real-time log limited to "kernel" in the search box. So I can see these when using kext load/kextunload:
default 11:58:27.608228 +0200 kernel MyKext has started.
default 11:58:34.446824 +0200 kernel MyKext has stopped.
default 11:58:44.803350 +0200 kernel MyKext has started.
There is no need for the csrutil and nvram changes.
Important For some freaky reason, I needed to restart the Console to reflect my messages changes, otherwise it has showing the ones (start & stop) from the previous build. Very strange indeed!
Later To recover old logs, try sudo log collect --last 1d and open the result with Console(more here).
Sorry to necro-post, but I found it useful to use log(1) with one of its many commands (as suggested by #pmdj in the comments above) rather than use Console. From the manual:
log -- Access system wide log messages created by os_log, os_trace and other log-
ging systems.
For example, one can run:
log stream
to see real-time output of the system, including printf() from the MacOS kernel extension.

How to setup LLDB with "openocd and JTAG board"

I'm trying to use lldb with openocd/jtag board but I'm in trouble.
I already use openocd with gdb to develop on L0 STMicroelectronics board and it works perfectly.
Now I want the same with lldb.
I do that on LLDB host side
$ lldb bin/token.elf
(lldb) target create "bin/token.elf"
Current executable set to 'bin/token.elf' (arm).
(lldb) platform select remote-gdb-server
Platform: remote-gdb-server
Connected: no
(lldb) platform connect connect://localhost:5557
Platform: remote-gdb-server
Hostname: (null)
Connected: yes
(lldb) target list
Current targets:
* target #0: /home/cme/Projects/Tacos/ledger/trunk/se/build/st31_bolos/bin/token.elf ( arch=arm-unknown-unknown, platform=host )
On the openocd/GDB server side I correctly see the "Info : accepting 'gdb' connection on tcp/5557"
But now I don't found how to continue:
(lldb) process launch
error: process launch failed: Child exec failed.
I also tried "process continue", but lldb complains there is no process
With gdb, the process is considered as already running and I use reset/continue commands, never the 'run' command.
Does anybody know how to use lldb with openocd/jtag gdb-server?
Thanks for your help
C/M.
from what we were researching, it is not possible to debug remote (bare-metal!) targets with lldb without writing extra code.
for basic functionality, lldb needs to recognize at least one thread context.
the same is true for gdb. But there in gdb there is some sort of stub implemented faking an existing thread on the remote system. [1]
from an conversation on the lldb mailing list [2] the answer compiles to:
we have to write some (python) code to get a remote bare metal working with lldb.
[1] https://github.com/bminor/binutils-gdb/blob/28170b88cc8b40fdea2b065dafe6e1872a47ee4e/gdb/remote.c#L1808
[2] http://comments.gmane.org/gmane.comp.debugging.lldb.devel/3405
So far this works for me on my stm32g0b1 nucleo board and pyOCD but I also tested with OpenOCD
$ lldb --local-lldbinit Build/temp.elf
(lldb) target create "Build/temp.elf"
Current executable set to '/home/diego/Workspace/genesis/Buil /temp.elf' (arm).
(lldb) gdb-remote 127.0.0.1:3333
Process 1 stopped
(lldb) target modules load --load .text 0x08000000
section '.text' loaded at 0x8000000
(lldb) process plugin packet monitor reset halt
packet: qRcmd,72657365742068616c74
response: 526573657474696e672074617267657420776974682068616c740a5375636365737366756c6c792068616c74656420646576696365206f6e2072657365740a

PTRACE_ATTACH not permitted for

For some reason i am not able to attach to my very own processes?! Works fine if i try strace as root.
$ ./list8 &
[1] 3141
$ child4 starts...
$ strace -p 3141
attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted
Could not attach to process. If your uid matches the uid of the target
process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try
again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf
$ cat /proc/sys/kernel/yama/ptrace_scope
1
Running on lubuntu 13.10
Linux goal 3.8.0-19-generic #29-Ubuntu SMP Wed Apr 17 18:19:42 UTC 2013 i686 i686 i686 GNU/Linux
So then how does gdb attach to user's own processes without having to muck around with kernel settings (ptrace_scope)??
Looks like you answered your own question -- you have ptrace_scope set to 1, so you can only trace direct children. To allow tracing any process belonging to the same user, set it to 0. This is also required to use the gdb attach command.
READ the /etc/sysctl.d/10-ptrace.conf file as your error message suggested...
If strace fails as root, try checking whether... gdb or strace is not running in the background (that was my case).
Command: ps aux | grep "gdb\|strace"
If this fails as root, I had a problem stracing enlightenment (e17) and the reason was that you can't strace a process already being straced or run under gdb, which some programs will do so that they can get their own debugging info.

GDB blows up when debugging multithreaded program that fast-forks and executes another program

I'm debugging my C program(s) with GDB and it's kinda wonky, probably because I'm not using glibc so it doesn't detect new threads until it breaks in them. I fixed this by adding a breakpoint which immediately resumes (break if 0).
Today however I ran into a wall.
I need to execve() really fast so normal fork() is out of the question (will use a ton of memory) and I can't vfork() (I need a stack to set up pipes etc) so I used the same method as in this library (https://code.google.com/p/popen-noshell/) which is basically only cloning with CLONE_VM. This (or calling execve() in general - I don't really know) makes GDB really confused for some reason - basically it's outputting something like this:
[New LWP 516]
[New LWP 520]
[New LWP 519]
[New LWP 521]
LWP 521 is executing new program: /bin/bash
Error in re-setting breakpoint 1: No source file named xxx.c.
Error in re-setting breakpoint 2: No source file named yyy.c.
Error in re-setting breakpoint 4: Function "zzz_main" not defined.
[LWP 521 exited]
Program received signal SIGTRAP, Trace/breakpoint trap.
[Switching to LWP 519]
0x00000000004307f8 in shell_execve ()
(gdb) info threads
Id Target Id Frame
* 5 LWP 519 "self-tes" 0x00000000004307f8 in shell_execve ()
4 LWP 520 "self-tes" 0x000000000040825f in ?? ()
3 LWP 516 "self-tes" 0x000000000040825f in ?? ()
2 LWP 515 "self-tes" 0x000000000040825f in ?? ()
1 LWP 512 "self-tes" 0x000000000040848a in ?? ()
GDB basically get's confused about everything and think it's currently executing /bin/bash - it's not - running without GDB works fine however I need it to debug my program. I've tried to disable every fork and exec setting I could find but nothing like "follow exec" is enabled. As you can see it tries to re-set breakpoints after the execve()... why?? It no longer recognizes any frames or symbols as it completely wiped the previous space and loaded /bin/bash instead.
How can I make GDB ignore the thread that calls the execve() and NOT try to load symbols for the subprocess?
EDIT: Oh and I'm currently using x86_64 Linux 3.2.0-27 (Ubuntu).
EDIT2: Also GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2) 7.4-2012.04
EDIT3: Also just checked with gdb 7.5. Same problem.
Found the problem! Apparently I forgot to mask flags with SIGCHLD when cloning which apparently gdb (or libthread) uses as a hint of spawning a subprocess (fork style) and not just another thread. Thanks #R.. for the hint which lead me in the right direction.

Resources