2017年3月28日 星期二

Using systemd to restart core dump C++ program for debugging in CentOS 7


Core Dump Generation

How to write a crashing C++ program?
vi a.cpp
#include <string.h>
#include <chrono>
#include <thread>
int main() {
    std::this_thread::sleep_for(std::chrono::seconds(10)); // 10 or 120
    char* p = NULL;
    strcpy(p, "copy string to null pointer and cause crash");
    return 0;
} // g++ -g a.cpp -o a.out -std=c++11
Segmentation fault (core dumped)

Why there is no core dump file generated?
ulimit -c
0
ulimit -a | grep core
core file size          (blocks, -c) 0

How to enable core file size?
ulimit -c unlimited
ulimit -a | grep core
core file size          (blocks, -c) unlimited

What is the file size of the core dump?
417KB
./a.out
ll core.*
rw-------. 1 ricky ricky 417792 Mar 28 11:06 core.1258





Core Dump Analysis



How to setup dbg for debugging?

sudo debuginfo-install glibc-2.17-106.el7_2.8.x86_64 libgcc-4.8.5-11.el7.x86_64 libstdc++-4.8.5-11.el7.x86_64




How to analyse the core dump file?
gdb a.out core.1258
bt

GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-80.el7

Copyright (C) 2013 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.  Type "show copying"

and "show warranty" for details.

This GDB was configured as "x86_64-redhat-linux-gnu".

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/ricky/crash/a.out...done.

[New LWP 1258]
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x00000000004005cb in main () at crashing.cpp:6
6           strcpy(p, "copy string to null pointer and cause crash");


(gdb) print p

$1 = 0x0

Background of systemd

What is init?
init is the default PID 1 in CentOS 6
cat /etc/*release
CentOS release 6.6
ps -ef | head -2
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0  2016 ?        00:01:03 /sbin/init

What is systemd?
systemd is the default PID 1 in CentOS 7
cat /etc/*release
CentOS Linux release 7.2.1511
ps -ef | head -2
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0  2016 ?        00:09:54 /usr/lib/systemd/systemd --system --deserialize 26

Restarting the crashing program with systemd

How to create a daemon?
sudo vim /etc/systemd/system/a.service
[Unit]
Description=service a.service here
[Service]
Type=simple
Restart=always
WorkingDirectory=/home/ricky/a/
ExecStart=/home/ricky/a/a.out
[Install]
WantedBy=multi-user.target

How to start a daemon?
sudo systemctl daemon-reload
sudo systemctl status -l a
● a.service
   Loaded: loaded (/etc/systemd/system/a.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
sudo systemctl start a
sudo systemctl status -l a
● a.service
   Loaded: loaded (/etc/systemd/system/a.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2017-03-28 11:46:36 HKT; 108ms ago
 Main PID: 2056 (a.out)
   CGroup: /system.slice/a.service
           └─2056 /home/ricky/a/a.out
ps -ef | grep a.out

How to enable core dump for systemd daemon?
sudo vi /etc/systemd/system.conf
DefaultLimitCORE=infinity
systemctl daemon-reexec



How to make a b.service that requires a.service?
sudo vim /etc/systemd/system/b.service
[Unit]
Description=service b.service here
Requires=a.service
[Service]
Type=simple
Restart=always
WorkingDirectory=/home/ricky/b/
ExecStart=/home/ricky/b/b.out
ExecStartPre=/usr/bin/date
ExecStartPost=/usr/bin/date
ExecReload=/usr/bin/date
ExecStop=/usr/bin/date
ExecStopPost=/usr/bin/date
[Install]
WantedBy=multi-user.target

sudo systemctl daemon-reload
sudo systemctl status -l b
sudo systemctl start b

sudo systemctl status -l a
sudo systemctl status -l b
ls && ps -ef | grep b.out


sudo systemctl stop a
sudo systemctl stop b

sudo systemctl start a
sudo systemctl start b

sudo systemctl status -l a
sudo systemctl status -l b


ExecStartPre=/bin/bash -c 'pwd; ulimit -c unlimited; rm ./*.log; python -c "print(123)" '?
Process: 23069 ExecStartPre=/bin/bash -c pwd; ulimit -c unlimited; rm ./*.log; python -c "print(123)"  (code=exited, status=0/SUCCESS)
Apr 07 12:41:05 pc009 bash[23069]: /home/ricky/b
Apr 07 12:41:05 pc009 bash[23069]: rm: cannot remove ‘./*.log’: No such file or directory
Apr 07 12:41:05 pc009 bash[23069]: 123

ExecStartPre=/usr/bin/sleep 5?
Yes. It will be exited with status 0/SUCCESS.
Process: 20924 ExecStartPre=/usr/bin/sleep 5 (code=exited, status=0/SUCCESS)

ExecStartPre=/usr/bin/sleep 1; /usr/bin/sleep 1; /usr/bin/sleep 3?
No. Multiple commands not allowed.
Process: 21025 ExecStartPre=/usr/bin/sleep 1; /usr/bin/sleep 1; /usr/bin/sleep 3 (code=exited, status=1/FAILURE)

ExecStartPre=rm /home/ricky/b/md/*.con ?
No. It is because rm is not absolute. You will see below error.
[/etc/systemd/system/b.service:8] Executable path is not absolute, ignoring: rm /home/ricky/b/md/*.con

ExecStartPre=/usr/bin/rm /home/ricky/b/md/*.con ?
No. It will be exited with status 1/FAILURE
Process: 20827 ExecStartPre=/usr/bin/rm /home/ricky/b/md/*.con (code=exited, status=1/FAILURE)


How to make a sleeping service?
sudo vim /etc/systemd/system/sleeping.service
[Unit]
Description=sleeping service
[Service]
Type=simple
User=ricky
Restart=always
WorkingDirectory=/home/ricky/
ExecStart=/usr/bin/sleep 10
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload && sudo systemctl restart sleeping && sudo systemctl status sleeping
● sleeping.service - sleeping service
   Loaded: loaded (/etc/systemd/system/sleeping.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2017-04-21 16:43:19 HKT; 31ms ago
 Main PID: 28266 (sleep)
   CGroup: /system.slice/sleeping.service
           └─28266 /usr/bin/sleep 10

Apr 21 16:43:19 pc009 systemd[1]: Started sleeping service.
Apr 21 16:43:19 pc009 systemd[1]: Starting sleeping service...


abcdefghijklmnopqrstuwxyz
abcdefghijklmnopqrstuwxyz
abcdefghijklmnopqrstuwxyz
abcdefghijklmnopqrstuwxyz
abcdefghijklmnopqrstuwxyz
abcdefghijklmnopqrstuwxyz



2017年3月27日 星期一

MT4 Adeline_Complete.ex4

MT4 Adeline_Complete.ex4




//+------------------------------------------------------------------+
//| This MQL is generated by Expert Advisor Builder                  |
//|                http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/ |
//|                                                                  |
//|  In no event will author be liable for any damages whatsoever.   |
//|                      Use at your own risk.                       |
//|                                                                  |
//|   Modified by Lucas Liew                                         |                                                                 
//|                                                                  |
//+------------------- DO NOT REMOVE THIS HEADER --------------------+

/*
-----

Hey guys,

I strongly encourage you stay in touch with our course updates
& new course launches (on Machine Learning, Data Science etc) by joining our mailing list.
This allows me to have a channel to keep in touch with you.
No spam, and you can unsubscribe anytime. =)

Link: http://eepurl.com/bVQiXr

After you sign up, we will send you our Ebook - "Black Algo Strategy Development Guide".
We are looking to create more ebooks/guides for you guys. Will send them to you via email once they
are out!

Cheers,
Lucas

-----
*/

/*
ADELINE ENTRY RULES:
Enter a long trade when SMA(10) crosses SMA(40) from bottom
Enter a short trade when SMA(10) crosses SMA(40) from top

ADELINE EXIT RULES:
Exit the long trade when SMA(10) crosses SMA(40) from top
Exit the short trade when SMA(10) crosses SMA(40) from bottom
30 pips hard stop (30pips from initial entry price)
Trailing stop of 30 pips

ADELINE POSITION SIZING RULE:
1 Lot
*/

#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4

#property copyright "Expert Advisor Builder"
#property link      "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"

// TDL 5: Stops and Position Sizing

extern int MagicNumber = 12345;
extern bool SignalMail = False;
extern double Lots = 1.0;
extern int Slippage = 3;
extern bool UseStopLoss = True;
extern int StopLoss = 30;
extern bool UseTakeProfit = False;
extern int TakeProfit = 0;
extern bool UseTrailingStop = True;
extern int TrailingStop = 30;

int P = 1;
int Order = SIGNAL_NONE;
int Total, Ticket, Ticket2;
double StopLossLevel, TakeProfitLevel, StopLevel;

// To-Do-List (TDL) 1: Declare Variables
double sma10_1, sma10_2, sma40_1, sma40_2;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {

       if (Digits == 5 || Digits == 3 || Digits == 1)P = 10; else P = 1; // To account for 5 digit brokers

       return(0);
}
//+------------------------------------------------------------------+
//| Expert initialization function - END                             |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
       return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function - END                           |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert start function                                            |
//+------------------------------------------------------------------+
int start() {

       Total = OrdersTotal();
       Order = SIGNAL_NONE;

       //+------------------------------------------------------------------+
       //| Variable Setup                                                   |
       //+------------------------------------------------------------------+

       // TDL 2: Assign Values to variables

       sma10_1 = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 1); // c
       sma10_2 = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 2); // b
       sma40_1 = iMA(NULL, 0, 40, 0, MODE_SMA, PRICE_CLOSE, 1); // d
       sma40_2 = iMA(NULL, 0, 40, 0, MODE_SMA, PRICE_CLOSE, 2); // a


       StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD); // Defining minimum StopLevel

       if (StopLoss < StopLevel) StopLoss = StopLevel;
       if (TakeProfit < StopLevel) TakeProfit = StopLevel;

       //+------------------------------------------------------------------+
       //| Variable Setup - END                                             |
       //+------------------------------------------------------------------+

       //Check position
       bool IsTrade = False;

       for (int i = 0; i < Total; i++) {
              Ticket2 = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
              if (OrderType() <= OP_SELL &&  OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
                     IsTrade = True;
                     if (OrderType() == OP_BUY) {
                           //Close

                           //+------------------------------------------------------------------+
                           //| Signal Begin(Exit Buy)                                           |
                           //+------------------------------------------------------------------+

                           /* ADELINE EXIT RULES:
                           Exit the long trade when SMA(10) crosses SMA(40) from top
                           Exit the short trade when SMA(10) crosses SMA(40) from bottom
                           30 pips hard stop (30pips from initial entry price)
                           Trailing stop of 30 pips
                           */

                           // TDL 4: Code Exit Rules

                           if (sma10_2 > sma40_2 && sma40_1 >= sma10_1) Order = SIGNAL_CLOSEBUY// Rule to EXIT a Long trade

                                                                                                                                                  //+------------------------------------------------------------------+
                                                                                                                                                  //| Signal End(Exit Buy)                                             |
                                                                                                                                                  //+------------------------------------------------------------------+

                           if (Order == SIGNAL_CLOSEBUY) {
                                  Ticket2 = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
                                  if (SignalMail) SendMail("[Signal Alert]""[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
                                  IsTrade = False;
                                  continue;
                           }
                           //Trailing stop
                           if (UseTrailingStop && TrailingStop > 0) {
                                  if (Bid - OrderOpenPrice() > P * Point * TrailingStop) {
                                         if (OrderStopLoss() < Bid - P * Point * TrailingStop) {
                                                Ticket2 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - P * Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                                                continue;
                                         }
                                  }
                           }
                     }
                     else {
                           //Close

                           //+------------------------------------------------------------------+
                           //| Signal Begin(Exit Sell)                                          |
                           //+------------------------------------------------------------------+

                           if (sma40_2 > sma10_2 && sma10_1 >= sma40_1) Order = SIGNAL_CLOSESELL// Rule to EXIT a Short trade

                                                                                                                                                   //+------------------------------------------------------------------+
                                                                                                                                                   //| Signal End(Exit Sell)                                            |
                                                                                                                                                   //+------------------------------------------------------------------+

                           if (Order == SIGNAL_CLOSESELL) {
                                  Ticket2 = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
                                  if (SignalMail) SendMail("[Signal Alert]""[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
                                  IsTrade = False;
                                  continue;
                           }
                           //Trailing stop
                           if (UseTrailingStop && TrailingStop > 0) {
                                  if ((OrderOpenPrice() - Ask) > (P * Point * TrailingStop)) {
                                         if ((OrderStopLoss() > (Ask + P * Point * TrailingStop)) || (OrderStopLoss() == 0)) {
                                                Ticket2 = OrderModify(OrderTicket(), OrderOpenPrice(), Ask + P * Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                                                continue;
                                         }
                                  }
                           }
                     }
              }
       }

       //+------------------------------------------------------------------+
       //| Signal Begin(Entries)                                            |
       //+------------------------------------------------------------------+

       /* ADELINE ENTRY RULES:
       Enter a long trade when SMA(10) crosses SMA(40) from bottom
       Enter a short trade when SMA(10) crosses SMA(40) from top
       */

       // TDL 3: Code Entry Rules

       if (sma40_2 > sma10_2 && sma10_1 >= sma40_1) Order = SIGNAL_BUY// Rule to ENTER a Long trade

       if (sma10_2 > sma40_2 && sma40_1 >= sma10_1) Order = SIGNAL_SELL// Rule to ENTER a Short trade


                                                                                                                       //+------------------------------------------------------------------+
                                                                                                                       //| Signal End                                                       |
                                                                                                                       //+------------------------------------------------------------------+

                                                                                                                       //Buy
       if (Order == SIGNAL_BUY) {
              if (!IsTrade) {
                     //Check free margin
                     if (AccountFreeMargin() < (1000 * Lots)) {
                           Print("We have no money. Free Margin = ", AccountFreeMargin());
                           return(0);
                     }

                     if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point * P; else StopLossLevel = 0.0;
                     if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point * P; else TakeProfitLevel = 0.0;

                     Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
                     if (Ticket > 0) {
                           if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
                                  Print("BUY order opened : ", OrderOpenPrice());
                                  if (SignalMail) SendMail("[Signal Alert]""[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
                           }
                           else {
                                  Print("Error opening BUY order : ", GetLastError());
                           }
                     }
                     return(0);
              }
       }

       //Sell
       if (Order == SIGNAL_SELL) {
              if (!IsTrade) {
                     //Check free margin
                     if (AccountFreeMargin() < (1000 * Lots)) {
                           Print("We have no money. Free Margin = ", AccountFreeMargin());
                           return(0);
                     }

                     if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point * P; else StopLossLevel = 0.0;
                     if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point * P; else TakeProfitLevel = 0.0;

                     Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
                     if (Ticket > 0) {
                           if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
                                  Print("SELL order opened : ", OrderOpenPrice());
                                  if (SignalMail) SendMail("[Signal Alert]""[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
                           }
                           else {
                                  Print("Error opening SELL order : ", GetLastError());
                           }
                     }
                     return(0);
              }
       }

       return(0);
}
//+------------------------------------------------------------------+



2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...