函数

列出函数名

1
2
3
4
info functions

# 支持正则
info functions <regex>

进入函数

使用gdb调试遇到函数时,使用step命令(缩写为s)可以进入函数(函数必须有调试信息,没有调试信息可以执行set step-mode on
可以使用next命令(缩写为n)不进入函数,gdb会等函数执行完
当单步调试一个函数时,如果不想继续跟踪下去了,可以有两种方式退出:(1)finish,函数会执行完并打印返回值,然后等待输入(2)return,函数不会继续执行下面的语句,而是直接返回,也可以用return <expression>指定返回值

参考:gdb手册

直接执行函数

使用gdb调试程序时,可以使用“call”或“print”命令直接调用函数执行

参考:gdb手册

打印函数堆栈帧信息

1
2
3
4
5
6
7
8
9
10
i frame

i registers

# 查看func函数汇编代码
disassemble <func>

# 输出尾调用的相关信息(设置`debug entry-values`选项为非0值)
# 尾调用gdb(https://sourceware.org/gdb/onlinedocs/gdb/Tail-Call-Frames.html)
set debug entry-values 1

参考:gdb手册

Read more »

环境

ubuntu 16.04 64
g++ 6.3

实例文件

tes.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//#include <iostream>

using namespace std;

int global_a; //.bss
int global_b = 0; //.bss
int global_c = 3; //.data
static int static_global_a; //.bss
static int static_global_b = 3; //.data
const int const_global_a = 0; //.rodata
const int const_global_b = 1; //.rodata
const static int const_static_global_a = 0; //.rodata
const static int const_static_global_b = 1; //.rodata

int main() {
static int local_static_a; //.bss
static int local_static_b = 3; //.data
const static int local_const_static_a = 0; //.rodata
const static int local_const_static_b = 1; //.rodata
const int local_const_a = 0;
const int local_const_b = 1;
int local_a;
return 0;
}
Read more »

今天在写一个进程守护shell文件时,发现判断字符串为空时的一个坑,先记录下来。
在watch函数中,用 if [ ! -z ${pid} ];判断是正常的。
当把watch函数写成以下时(第一个程序正在运行,第二个未运行),

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function watch() {
pid="$(ps aux|grep -v grep|grep "${1}" |awk '{print $2}'|head -n 1)"
date="[$(date '+%Y%m%d %H:%M:%S')]"
echo "1:${1}"
echo "abc:$(ps aux|grep -v grep|grep "${1}")"
echo "pid:${pid}"

if [ ${2} == "daemon" ]; then
if [ -n ${pid} ]; then
echo "nnnnnnnnnnnnnnnnnnnnnnnn-n"
fi
if [ -z ${pid} ]; then
echo "zzzzzzzzzzzzzzzzzzzzzzzz-z"
fi
if [ ! -z ${pid} ]; then
echo "${date}${1}(pid:${pid}) is running"
else
nohup ${1} 1>${3} 2>&1 &
echo "${date}启动进程${1}(pid:"$!")成功"
fi
elif [ ${2} == "restart" ]; then
if [ ! -z ${pid} ]; then
kill -9 ${pid}
fi
nohup ${1} 1>${3} 2>&1 &
echo "${date}重启进程${1}(pid:"$!")成功"
elif [ ${2} == "kill" ]; then
if [ ! -z ${pid} ]; then
kill -9 ${pid}
fi
echo "${date}结束进程${1}(pid:${pid})成功"
fi
}
Read more »

nan和inf的产生

nan 代表不是一个数字(not a number), inf代表无穷大(infinity)

IEEE 754 的一些规定:

‘Invalid Operation’

This exception is raised if the given operands are invalid for the operation to be performed. Examples are (see IEEE 754, section 7):

Addition or subtraction: ∞ - ∞. (But ∞ + ∞ = ∞).
Multiplication: 0 · ∞.
Division: 0/0 or ∞/∞.
Remainder: x REM y, where y is zero or x is infinite.
Square root if the operand is less than zero. More generally, any mathematical function evaluated outside its domain produces this exception.
Conversion of a floating-point number to an integer or decimal string, when the number cannot be represented in the target format (due to overflow, infinity, or NaN).
Conversion of an unrecognizable input string.
Comparison via predicates involving < or >, when one or other of the operands is NaN. You can prevent this exception by using the unordered comparison functions instead; see FP Comparison Functions.
If the exception does not trap, the result of the operation is NaN.

‘Division by Zero’

This exception is raised when a finite nonzero number is divided by zero. If no trap occurs the result is either +∞ or -∞, depending on the signs of the operands.

‘Overflow’

This exception is raised whenever the result cannot be represented as a finite value in the precision format of the destination. If no trap occurs the result depends on the sign of the intermediate result and the current rounding mode (IEEE 754, section 7.3):

Round to nearest carries all overflows to ∞ with the sign of the intermediate result.
Round toward 0 carries all overflows to the largest representable finite number with the sign of the intermediate result.
Round toward -∞ carries positive overflows to the largest representable finite number and negative overflows to -∞.
Round toward ∞ carries negative overflows to the most negative representable finite number and positive overflows to ∞.
Whenever the overflow exception is raised, the inexact exception is also raised.

‘Underflow’

The underflow exception is raised when an intermediate result is too small to be calculated accurately, or if the operation’s result rounded to the destination precision is too small to be normalized.

When no trap is installed for the underflow exception, underflow is signaled (via the underflow flag) only when both tininess and loss of accuracy have been detected. If no trap handler is installed the operation continues with an imprecise small value, or zero if the destination precision cannot hold the small exact result.

‘Inexact’

This exception is signalled if a rounded result is not exact (such as when calculating the square root of two) or a result overflows without an overflow trap.

nan和inf的运算

n + inf = inf; // 任意数 + 无穷大 = 无穷大
n / inf = 0; // 任意数 / 无穷大 = 0
atan(inf) = π // 对无穷大取反正切,结果为π
任意数和nan运算结果都为nan
nan不大于,不小于,也不等于任意数(包括自身)
inf除了自身及nan,正无穷大大于一切,负无穷大小于一切

nan和inf的判断

c++可以使用中的std::isinf(x)和std::isnan(x)来分别判断inf和nan。

参考

http://www.gnu.org/software/libc/manual/html_node/FP-Exceptions.html
http://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html
https://stackoverflow.com/questions/570669/checking-if-a-double-or-float-is-nan-in-c
https://stackoverflow.com/questions/4095337/how-to-check-for-inf-and-or-nan-in-a-double-variable
https://stackoverflow.com/questions/16691207/c-c-nan-constant-literal

IO类

介绍

c++中主要的IO类有三个:iostream,fstream,sstream.iostream定义了用于读写流的基本类型,fstream定义了读写命名文件的类型,sstream定义了读写内存string对象的类型.详细的继承对应关系如下图:
img

条件状态(condition state)

strm::iostate strm指某一种IO类型,iostate是一种机器相关的类型,提供了表达条件状态的完整功能
strm::badbit 流已崩溃
strm::failbit 指出一个IO操作失败了
strm::eofbit 指出流到达了文件结束
strm::goodbit 用来指出流未处于错误状态.此值保证为零
s.eof() 若流s的eofbit置位,则返回true
s.fail() 若流s的failbit或badbit置位,则返回true
s.bad() 若流s的badbit置位,则返回true
s.good() 若流s处于有效状态,则返回true
s.clear() 将流s中所有条件状态位复位,将流的状态设置为有效.返回void
s.clear(flags) 根据给定的flags标志位,将流s中对应条件状态位复位.flags的类型为strm::iostate.返回void
s.setstate(flags) 根据给定的flags标志位,将流s中对应条件状态位置位.flags的类型为strm::iostate.返回void
s.rdstate() 返回流s的当前条件状态,返回值类型为strm::iostate

Read more »

asciinema

Asciinema 是一个轻量并且非常高效的终端会话录制器。使用它可以录制、回放和分享 JSON 格式的终端会话记录。与一些桌面录制器,比如 Recordmydesktop、Simplescreenrecorder、Vokoscreen 或 Kazam 相比,Asciinema 最主要的优点是,它能够以通过 ASCII 文本以及 ANSI 转义码编码来录制所有的标准终端输入、输出和错误信息。

  • 通过命令行一键进行录制,不再需要打开各种 GUI 程序进行复杂操作。
  • 非常适合在 Github 上展示自己的程序,或者是录制教程等。
  • 还可以一键上传到 asciinema 官网,命名保存自己的操作,会生成短 URL 便于随时访问、分享。
  • 会自动识别系统和使用的 terminal 类型,比如bash。
  • 录制产生的文件是一个 asciinema 自己格式的 json 文件,可以本地播放,也可以用官方的组件嵌入到自己的 web 博客中。

参考:
如何在 Linux 中使用 Asciinema 进行录制和回放终端会话

公司登陆测试服务器使用的是先登陆跳板机,然后再登陆远程机器,使用secureCRT的rz,sz可以上传下载文件,但不是很方便.

方法

在config文件中
vim ~/.ssh/config

1
2
3
4
5
6
7
Host *
ControlMaster auto
ControlPath ~/.ssh/%h-%p-%r
ControlPersist yes

Host d111
ProxyCommand ssh -p 10099 userdev@000.00.00.10 nc 10.10.000.000 22

以后登陆远程机器时就可以使用

1
ssh usename@d111

以后就可以使用scp直接上传文件或下载文件

1
2
scp -r ~/dir username@d111:/data/username/tmp
scp username@d111:/data/username/tmp/dir/test.txt ./

参考

https://serverfault.com/questions/37629/how-do-i-do-multihop-scp-transfers
scp 跨机远程拷贝
ssh如何通过跳板机直接访问到后端服务器
SSH穿越跳板机:一条命令跨越跳板机直接登陆远程计算机
Mac OS X 平台有哪些好用的 SSH 客户端?
scp如何跨过中转主机直接传输文件?

C/C++基础知识

排序

排序方式 平均时间复杂度 最坏时间复杂度 最好时间复杂度 空间复杂度 稳定性
插入排序 O(n2) O(n2) O(n) O(1) 稳定
希尔排序 O(n1.3) O(1) 不稳定
冒泡排序 O(n2) O(n2) O(n) O(1) 稳定
快速排序 O(nlog2n) O(n2) O(nlog2n) O(log2n) 不稳定
选择排序 O(n2) O(n2) O(n2) O(1) 不稳定
堆排序 O(nlog2n) O(nlog2n) O(nlog2n) O(1) 不稳定
归并排序 O(nlog2n) O(nlog2n) O(nlog2n) O(n) 稳定
基数排序 O(d(n+r)) O(d(n+r)) O(d(n+r)) O(r) 稳定

希尔排序,快排,推排序问的比较多

多态

浅谈C++多态性

网络

怎么理解socket

简单理解Socket

select和epoll的区别

select、poll、epoll之间的区别总结[整理]
Linux IO模式及 select、poll、epoll详解
聊聊IO多路复用之select、poll、epoll详解

tcp三次握手四次挥手

TCP三次握手四次挥手

滑动窗口

TCP 滑动窗口(发送窗口和接收窗口)

进程之间通信

Linux进程间通信的几种方式总结–linux内核剖析(七)

线程之间通信

linux基础——linux线程间通信及同步机制总结

操作系统

虚拟地址

linux内存管理—虚拟地址、逻辑地址、线性地址、物理地址的区别(一)

中断分类

中断的分类

数据库

sql基本命令

w3school

0%