GDB and LLDB "swallow" status set by child process in OS X - gdb

Given the following code:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int ac, char** av) {
int status;
pid_t cpid = fork();
if(0 == cpid) { /* Child */
return *(volatile int*) 0; /* exits with signal 11 */
} else { /* Parent */
do {
waitpid(cpid, &status, WUNTRACED);
if(WIFSIGNALED(status)) {
printf("\nChild exited with signal %d\n\n", WTERMSIG(status));
return 0;
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
printf("\nChild exited normally\n\n");
}
return 0;
}
I get the expected result running the app from Terminal:
$ ./Fork4GdbTests.exe
Child exited with signal 11
Running the app within LLDB (or GDB), strangely, I get:
$ lldb ./Fork4GdbTests.exe
(lldb) target create "./Fork4GdbTests.exe"
Current executable set to './Fork4GdbTests.exe' (x86_64).
(lldb) r
Process 46815 launched: './Fork4GdbTests.exe' (x86_64)
Child exited normally
Process 46815 exited with status = 0 (0x00000000)
My Makefile looks like this (used for Cygwin, also):
CC = gcc
CFLAGS = -Wextra -Wall -Werror -Wno-unused-parameter -g
.PHONY: all clean
all: Fork4GdbTests.exe
%.exe: %.o
$(CC) $(CFLAGS) $? $(LIBS) -o $#
clean:
rm -f *.o *.exe *.stackdump;
In Cygwin, I get the expected result both running from the command prompt and in the debugger. Similar behavior occurs for all kinds of other signals, such as SIGFPE or any signals sent to the child by means of kill().
What is going on?

That's just a bug. Given it affects both gdb & lldb it is probably in CoreOS not the debuggers (though the same folks did the Mach specific layer of both debuggers so that's not a guarantee...)
Anyway, please file a bug report with http://bugreporter.apple.com.

Related

How to build a module for kernel linux to calculate and show seconds and jiffies?

I have a simple code:
/**
* simple.c
* A simple kernel module.
* To compile, run makefile by entering "make"
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/* This function is called when the module is loaded. */
int simple_init(void)
{
printk(KERN_INFO "Loading Module\n");
return 0;
}
/* This function is called when the module is removed. */
void simple_exit(void) {
printk(KERN_INFO "Removing Module\n");
}
/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");
and this:
obj-m += simple.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
I want to build a module for kernel Linux to calculate and show seconds and jiffies then with command cat/proc, print result.
how can I?

How to wait for t32rem DO script to complete?

It seems that doing t32rem localhost DO script.cmm is non-blocking. How can I block in a shell script until the cmm script is done?
Here is an abbreviated example:
$ time t32rem localhost wait 5s
real 0m5.048s
$ cat wait-5s.cmm
WAIT 5s
ENDDO
$ time t32rem localhost do wait-5s
real 0m0.225s
I can try to do some sort of t32rem localhost wait STATE.RUN() based on whatever the exact script is doing but this is not a very good solution.
Reading through api_remote.pdf it does note that T32_Cmd for DO is non-blocking and recommends polling using T32_GetPractice but it's not clear how to translate this to t32rem.
In my opinion you questions is a rather good one.
First the bummer: t32rem is not suitable to wait for the execution of a script. In fact t32rem cancels any running script before executing a command with T32_Stop(). (You can find the source code of t32rem in your TRACE32 installation at "C:\T32\demo\api\capi\test\t32rem.c")
So your suggestion to use t32rem localhost wait STATE.RUN() will definitely not work because it would cancel the running script. Furthermore STATE.RUN()returns the running state of the debugged CPU and not of the PRACTICE interpreter.
So in fact you have to use T32_GetPractice() to wait for the PRACTICE script to terminate. To use T32_GetPractice() you either have to link statically or dynamically the "API for Remote Control and JTAG Access in C" to an application that launches your script.
For dynamic linking (e.g. from a Python script) load "C:\T32\demo\api\capi\dll\t32api.dll". (Depending on your host operation system you might need t32api64.dll, t32api.so, or t32api64.so instead.)
For static linking (e.g. from a binary application written in C) add the files from "C:\T32\demo\api\capi\src" to your project.
And here is the code to write a command line application t32do, which starts a PRACTICE script and waits until the script terminates:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "t32.h"
int main(int argc, char *argv[])
{
int pstate;
const char *script;
if (argc == 4 && !strncmp(argv[2],"port=", 5)) {
if ( T32_Config( "PORT=", argv[2]+5 ) == -1 ) {
printf("Port number %s not accepted\n", argv[2] );
exit(2);
}
script = argv[3];
} else {
if (argc != 3) {
printf( "Usage: t32do <host> [port=<n>] <script>\n" );
exit(2);
}
script = argv[2];
}
if ( T32_Config( "NODE=", argv[1] ) == -1 ) {
printf( "Hostname %s not accepted\n", argv[1] );
exit(2);
}
if ( T32_Init() != 0 || T32_Attach(1) != 0){
printf( "Failed to connect to TRACE32\n" );
exit(2);
}
if ( T32_Cmd_f("DO \"%s\"", script) != 0 ){ // Launch PRACTICE script
printf( "Failed to start PRACTICE script\n" );
T32_Exit();
exit(1);
}
while (T32_GetPracticeState(&pstate) == 0 && pstate != 0){ // Wait until PRACTICE script terminates
usleep(10000);
}
T32_Exit();
return 0;
}
Put the source in a file named t32do.c in "C:\T32\demo\api\capi\src" and build the application with the following makefile, which works on both Windows (by using the MinGW compiler of Cygwin) and Linux:
BIN := t32do
OBJ := t32do.o hremote.o hlinknet.o
OS := $(shell uname -s)
ifneq ($(findstring CYGWIN,$(OS)),)
CC := x86_64-w64-mingw32-gcc
LOPT := -lws2_32
COPT := -DT32HOST_LE
endif
ifneq ($(findstring Linux,$(OS)),)
CC := gcc
COPT := -DT32HOST_LE
endif
all: $(BIN)
$(BIN): $(OBJ)
$(CC) $^ -s -o $# $(LOPT)
%.o: %.c t32.h
$(CC) -c $(COPT) -o $# $<
clean:
-rm $(OBJ) $(BIN)
If it compiles and links fine, you'll get an application t32do.exe.
Use it in the form: t32do <host> [port=<n>] <practice script>
My example code above is licensed under Creative Commons Zero 1.0. Use it any way you wish, in any code you want.

"Undefined reference" error when passing variable by reference

I'm fairly new to C++ but this thing has me baffled by any logic. My code is as follows:
#include "stdlib.h"
#include "syslog.h"
#include "unistd.h"
#include "sys/stat.h"
#include "X11/Xlib.h"
#include "cstdio"
void process();
void startTracker();
Display *display;
Window rootWindow;
XEvent xevent;
I have the Xlib header included and if I click on member functions in Eclipse it navigates to the definitions.
int main(int argc, char *argv[])
{
// set logging up
openlog("unison", LOG_CONS|LOG_PID|LOG_NDELAY, LOG_LOCAL1);
syslog(LOG_NOTICE, "Starting Unison Handler");
pid_t pid, sid;
pid = fork();
// fork failed
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
if (chdir("/") < 0) {
exit(EXIT_FAILURE);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
startTracker();
while (true) {
process();
}
closelog();
return(EXIT_SUCCESS);
}
Then I assign the variables for input selection
void startTracker() {
display = XOpenDisplay(0);
rootWindow = XRootWindow(display, 0);
XSelectInput(display, rootWindow, PointerMotionMask);
}
void process()
{
...but when i add the &event here...
XNextEvent(display, &xevent);
switch (xevent.type) {
case MotionNotify:
syslog(
LOG_NOTICE,
"Mouse position is %dx%d",
xevent.xmotion.x_root, xevent.xmotion.y_root
);
}
}
...the whole thing falls apart.
For some reason passing the xevent as reference throws off the entire Xlib header and gives me this:
00:16:15 **** Incremental Build of configuration Debug for project unisond ****
make all
Building file: ../unisond.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"unisond.d" -MT"unisond.d" -o "unisond.o" "../unisond.cpp"
Finished building: ../unisond.cpp
Building target: unisond
Invoking: GCC C++ Linker
g++ -o "unisond" ./unisond.o
./unisond.o: In function `startTracker()':
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:97: undefined reference to `XOpenDisplay'
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:98: undefined reference to `XRootWindow'
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:99: undefined reference to `XSelectInput'
./unisond.o: In function `process()':
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:105: undefined reference to `XNextEvent'
collect2: error: ld returned 1 exit status
make: *** [unisond] Error 1
00:16:15 Build Finished (took 159ms)
At the risk of getting downvoted could someone please explain what I've done wrong? I've tried everything I could think of but no luck.
It looks like you are missing the X11 library for linking.
add -lX11 to the g++ invocation.
This provides the steps required.
Right click on Project Folder> Properties> C/C++ Build > Settings > GCC C++ Linker > Libraries > add "X11"

How to link to shared lib in c++

Could someone help me with linking to a shared lib, specifically libzmq, in C++?
all: clean compile
clean:
rm bin *.o -f
compile:
g++ -g -Wall -I/usr/local/include -L/usr/local/lib main.cpp -lzmq -o bin
I've installed libzmq using the following steps:
git clone https://github.com/zeromq/libzmq.git
cd libzmq
./autogen.sh
./configure
make && sudo make install
Here's my main.cpp
#include <iostream>
#include <string>
#include <zmq/zmq.h>
// Required by fork routine
#include <sys/types.h>
#include <unistd.h>
// Required by wait routine
#include <sys/wait.h>
#include <stdlib.h> // Declaration for exit()
#include <cstdio> // printf
using namespace std;
int global_variable = 2;
int main(int argc, char** argv){
const short int FORK_FAILED = -1;
const short int FORK_SUCCESS = 0;
int stack_variable = 20;
pid_t pid;
string status_identifier;
switch (pid = fork()){
case FORK_SUCCESS:
printf("Child changing global and stack variables\n");
global_variable++;
stack_variable++;
break;
case FORK_FAILED:
cerr << "Failed! -- Failed to fork: " << pid << endl;
exit(1);
default:
printf("Child process (pid=%d) created successfully.\n", pid);
wait(0);
break;
}
printf("[pid=%d] Global: %d\n", pid, global_variable);
printf("[pid=%d] Stack: %d\n", pid, stack_variable);
return 0;
}
And, here's the error msg:
bitcycle # ubuntu64vm ~/git/test $ make
rm bin *.o -f
g++ -g -Wall -I/usr/local/include -L/usr/local/lib main.cpp -lzmq -o bin
main.cpp:4:23: fatal error: zmq/zmq.hpp: No such file or directory
compilation terminated.
make: *** [compile] Error 1
The error is pretty straight forward, but I've yet to find a solution. Any ideas?
My goal is to do something like this with multiple child processes.
Update I'm just going to install it system-wide in ubuntu: sudo apt-get install libzmq-dev, and that resolved the issue. It doesn't teach me anything about how to identify a shared lib and header file on disk and link to it... but I guess I can move that to another day.
C++ wrapper for ZeroMQ (zmq.hpp) is no longer part of ZeroMQ. There is no zmq.hpp in current libzmq master or in latest stable 3.2.x.

Need Help Tracking Down EXC_BAD_ACCESS on Function Entry on MacOS

I have a program that gets a KERN_PROTECTION_FAILURE with EXC_BAD_ACCESS in a very strange place when running multithreaded and I haven't the faintest idea how to troubleshoot it further. This is on MacOS 10.6 using GCC.
The very strange place that it gets this is when entering a function. Not on the first line of the function, but the actual jump to the function GetMachineFactors():
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0xb00009ec
[Switching to process 28242]
0x00012592 in GetMachineFactors () at ../sysinfo/OSX.cpp:168
168 MachineFactors* GetMachineFactors()
(gdb) bt
#0 0x00012592 in GetMachineFactors () at ../sysinfo/OSX.cpp:168
#1 0x000156d0 in CollectMachineFactorsThreadProc (parameter=0x200280) at Threads.cpp:341
#2 0x952f681d in _pthread_start ()
#3 0x952f66a2 in thread_start ()
(gdb)
If I run this non-threaded, it runs great, no issues:
#include "MachineFactors.h"
int main( int argc, char** argv )
{
MachineFactors* factors = GetMachineFactors();
std::string str = CreateJSONObject(factors);
cout << str;
delete factors;
return 0;
}
If I run this in a pthread, I get the EXC_BAD_ACCESS above.
THREAD_FUNCTION CollectMachineFactorsThreadProc( LPVOID parameter )
{
Main* client = (Main*) parameter;
if ( parameter == NULL )
{
ERRORLOG( "No data passed to machine identification thread. Aborting." );
return 0;
}
MachineFactors* mfactors = GetMachineFactors(); // This is where it dies.
// If I don't call GetMachineFactors and do something like mfactors =
// new MachineFactors(); everything is good and the threads communicate and exit
// normally.
if (mfactors == NULL)
{
ERRORLOG("Failed to collect machine identification: GetMachineFactors returned NULL." << endl)
return 0;
}
client->machineFactors = CreateJSONObject(mfactors);
delete mfactors;
EVENT_RAISE(client->machineFactorsEvent);
return 0;
}
Here is an excerpt from the GetMachineFactors() code:
MachineFactors* GetMachineFactors() // Dies on this line in multi-threaded.
{
// printf( "Getting machine factors.\n"); // Tried with and without this, never prints.
factors = new MachineFactors();
factors->OSName = "MacOS";
factors->Manufacturer = "Apple";
///…
// gather various machine metrics here.
//…
return factors;
}
For reference, I am using a socketpair to wait on the thread to complete:
// From the header file I use for cross-platform defines (this runs on OSX, Windows, and Linux.
struct _waitt
{
int fds[2];
};
#define THREAD_FUNCTION void*
#define THREAD_REFERENCE pthread_t
#define MUTEX_REFERENCE pthread_mutex_t*
#define MUTEX_LOCK(m) pthread_mutex_lock(m)
#define MUTEX_UNLOCK pthread_mutex_unlock
#define EVENT_REFERENCE struct _waitt
#define EVENT_WAIT(m) do { char lc; if (read(m.fds[0], &lc, 1)) {} } while (0)
#define EVENT_RAISE(m) do { char lc = 'j'; if (write(m.fds[1], &lc, 1)) {} } while (0)
#define EVENT_NULL(m) do { m.fds[0] = -1; m.fds[1] = -1; } while (0)
Here is the code where I launch the thread.
void Main::CollectMachineFactors()
{
#ifdef WIN32
machineFactorsThread = CreateThread(NULL, 0, CollectMachineFactorsThreadProc, this, 0, 0);
if ( machineFactorsThread == NULL )
{
ERRORLOG( "Could not create thread for machine id: " << ERROR_NO << endl )
}
#else
int retval = pthread_create(&machineFactorsThread, NULL, CollectMachineFactorsThreadProc, this);
if (retval)
{
ERRORLOG( "Return code from machine id pthread_create() is " << retval << endl )
}
#endif
}
Here's the simple failure case of running this multithreaded. It always fails for this code with the stack trace above:
CollectMachineFactors();
EVENT_WAIT(machineFactorsEvent);
cout << machineFactors;
return 0;
At first I suspected a library problem. Here's my makefile:
# Main executable file
PROGRAM = sysinfo
# Object files
OBJECTS = Version.h Main.o Protocol.o Socket.o SSLConnection.o Stats.o TimeElapsed.o Formatter.o OSX.o Threads.o
# Include directories
INCLUDE = -Itaocrypt/include -IyaSSL/taocrypt/mySTL -IyaSSL/include -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5
# Library settings
STATICLIBS = libtaocrypt.a libyassl.a -Wl,-rpath,. -ldl -lpthread -lz -lexpat
# Compile settings
RELCXX = g++ -g -ggdb -DDEBUG -Wall $(INCLUDE)
.SUFFIXES: .o .cpp
.cpp.o :
$(RELCXX) -c -Wall $(INCLUDE) -o $# $<
all: $(PROGRAM)
$(PROGRAM): $(OBJECTS)
$(RELCXX) -o $(PROGRAM) $(OBJECTS) $(STATICLIBS)
clean:
rm -f *.o $(PROGRAM)
I can't for the life of me see anything particularly odd or dangerous and I'm not sure where to look. The same threaded process works fine on any Linux machine I have tried. Any suggestions? Any tools I should try?
I can add more info if it would be helpful.
I can see a problem with your Windows code, but not the OSX code that's crashing on you.
It seems that you're not posting the actual code for GetMachineFactors, since the variable factors is not declared. But regarding debugging, you should not take the non-appearance of printf output as conclusive that that statement hasn't been executed. Use debugger facilities such as setting a breakpoint, using special debugger trace output, so on (not sure what gdb handles, it's a very primitive debugger, but perhaps Apple has better tools?).
For Windows, you should use the run time library's thread creation instead of Windows API CreateThread. That's because with CreateThread the runtime lib isn't informed. E.g, a new expression or other call that uses the runtime lib might fail.
Sorry I can't help more.
I think it could perhaps have something to do with the GetMachineFactors code that you haven't shown?
It turns out, and I can't explain why, that a fork() call combined with a socketpair() as the IPC mechanism was the workaround to get things going as intended.
I wish I knew why it was failing in the first place (headscratch) but that approach seems to have been a good workaround.
It almost seemed like the kind of "build out of whack" problem that could be caused by failing to run a 'make clean' after changing header files, but that wasn't the case here.