Gowhich

Durban's Blog

系统默认的颜色设置

1
2
3
4
5
6
//无色  
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//蓝色
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//灰色
cell.selectionStyle = UITableViewCellSelectionStyleGray;

自定义颜色和背景设置

改变UITableViewCell选中时背景色:

1
2
3
UIColor *color = [[UIColoralloc]initWithRed:0.0 green:0.0 blue:0.0 alpha:1];//通过RGB来定义自己的颜色
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
cell.selectedBackgroundView.backgroundColor = [UIColor xxxxxx];

自定义UITableViewCell选中时背景

1
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellart.png"]] autorelease];

还有字体颜色

1
cell.textLabel.highlightedTextColor = [UIColor xxxcolor];  [cell.textLabel setTextColor:color];//设置cell的字体的颜色

设置tableViewCell间的分割线的颜色

1
2
3
4
5
6
7
[theTableView setSeparatorColor:[UIColor xxxx ]];
UITableViewCellSeparatorStyle有如下几种
typedef enum {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine,
UITableViewCellSeparatorStyleSingleLineEtched
} UITableViewCellSeparatorStyle;

设置cell中字体的颜色

1
2
3
4
5
6
7
8
9
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(0 == indexPath.row)
{
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.highlightedTextColor = [UIColor redColor];
}
}

定义UITableViewCell的样式

1
2
3
4
5
6
7
8
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
accessoryType有如下几种
typedef enum {
UITableViewCellAccessoryNone,
UITableViewCellAccessoryDisclosureIndicator,
UITableViewCellAccessoryDetailDisclosureButton,
UITableViewCellAccessoryCheckmark
} UITableViewCellAccessoryType;

设置UITableViewCell之间分隔线的颜色

1
[chatTableViewsetSeparatorColor:[UIColor blueColor]];

Cell 样式

1
2
3
4
5
6
7
An enumeration for the various styles of cells.
typedef enum {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
} UITableViewCellStyle;

UITableView的强大更多程度上来自于可以任意自定义 UITableViewCell单元格。通常,UITableView中的Cell是动态的,在使用过程中,会创建一个Cell池,根据每个cell的高 度(即tableView:heightForRowAtIndexPath:返回值),以及屏幕高度计算屏幕中可显示几个cell。而进行自定义 TableViewCell无非是采用代码实现或采用IB编辑nib文件来实现两种方式,本文主要收集代码的方式实现各种cell自定义。

如何动态调整Cell高度

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
34
35
36
37
38
39
40
41
42
43
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.tag = 1;
label.lineBreakMode = UILineBreakModeWordWrap;
label.highlightedTextColor = [UIColor whiteColor];
label.numberOfLines = 0;
label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
[label release];
}

UILabel *label = (UILabel *)[cell viewWithTag:1];
NSString *text;
text = [textArray objectAtIndex:indexPath.row];
CGRect cellFrame = [cell frame];
cellFrame.origin = CGPointMake(0, 0);

label.text = text;
CGRect rect = CGRectInset(cellFrame, 2, 2);
label.frame = rect;
[label sizeToFit];
if (label.frame.size.height > 46) {
cellFrame.size.height = 50 + label.frame.size.height - 46;
}
else {
cellFrame.size.height = 50;
}
[cell setFrame:cellFrame];

return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}

如何用图片自定义Table Separeator分割线

一般地,利用类似[tableView setSeparatorColor:[UIColor redColor]];语句即可修改cell中间分割线的颜色。那又如何用一个图片作为分割线背景呢?可以尝试如下:
方法一:
先设置cell separatorColor为clear,然后把图片做的分割线添加到自定义的custom cell上。
方法二:
在cell里添加一个像素的imageView后将图片载入进,之后设置tableView.separatorStyle = UITableViewCellSeparatorStyleNone

自定义首行Cell与其上面导航栏间距

1
tableView.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,5,20)] autorelease];

自定义UITableViewCell的accessory样式

默认的accessoryType属性有四种取值:

  • UITableViewCellAccessoryNone
  • UITableViewCellAccessoryDisclosureIndicator
  • UITableViewCellAccessoryDetailDisclosureButton
  • UITableViewCellAccessoryCheckmark

如果想使用自定义附件按钮的其他样式,则需使用UITableView的accessoryView属性来指定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
UIButton *button;
if(isEditableOrNot) {
UIImage *image = [UIImage imageNamed:@"delete.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0,0.0,image.size.width,image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}else{
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}

以上代码仅仅是定义了附件按钮两种状态下的样式,问题是现在这个自定义附件按钮的事件仍不可用。即事件还无法传递到 UITableViewDelegateaccessoryButtonTappedForRowWithIndexPath方法上。当我们在上述代码 中在加入以下语句:

1
[button addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];

虽然可以捕捉到每个附件按钮的点击事件,但我们还无法进行区别到底是哪一行的附件按钮发生了点击动作!因为addTarget:方法最多允许传递两个参 数:target和event,这两个参数都有各自的用途了(target指向事件委托对象,event指向所发生的事件)。看来只依靠Cocoa框架已 经无法做到了。
但我们还是可以利用event参数,在自定义的btnClicked方法中判断出事件发生在UITableView的哪一个cell上。因为UITableView有一个很关键的方法indexPathForRowAtPoint,可以根据触摸发生的位置,返回触摸发生在哪一个cell的indexPath。而且通过event对象,正好也可以获得每个触摸在视图中的位置。

1
2
3
4
5
6
7
8
9
10
11
12
// 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
- (void)btnClicked:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
if(indexPath != nil)
{
[self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}

这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath参数。通过这个indexPath参数,我们即可区分到底哪一行的附件按钮发生了触摸事件。

1
2
3
4
5
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
int *idx = indexPath.row;
//这里加入自己的逻辑
}

摘自:http://www.cnblogs.com/ownerblood/archive/2012/08/02/2620173.html

这里给出个文档的地址:http://iosfonts.com/ ,里面包括了所有的ios的字体类型库:这里贴一下吧

Academy Engraved LET
AcademyEngravedLetPlain 5.0 4.3
American Typewriter
AmericanTypewriter 3.0 4.3
AmericanTypewriter-Bold 3.0 4.3
AmericanTypewriter-Condensed 5.0 5.0
AmericanTypewriter-CondensedBold 5.0 5.0
AmericanTypewriter-CondensedLight 5.0 5.0
AmericanTypewriter-Light 5.0 5.0
Apple Color Emoji
AppleColorEmoji 3.0 4.3
Apple SD Gothic Neo
AppleSDGothicNeo-Bold 5.0 5.0
AppleSDGothicNeo-Medium 4.3 4.3
Arial
ArialMT 3.0 4.3
Arial-BoldItalicMT 3.0 4.3
Arial-BoldMT 3.0 4.3
Arial-ItalicMT 3.0 4.3
Arial Hebrew
ArialHebrew 3.0 4.3
ArialHebrew-Bold 3.0 4.3
Arial Rounded MT Bold
ArialRoundedMTBold 3.0 4.3
Avenir
Avenir-Black 6.0 6.0
Avenir-BlackOblique 6.0 6.0
Avenir-Book 6.0 6.0
Avenir-BookOblique 6.0 6.0
Avenir-Heavy 6.0 6.0
Avenir-HeavyOblique 6.0 6.0
Avenir-Light 6.0 6.0
Avenir-LightOblique 6.0 6.0
Avenir-Medium 6.0 6.0
Avenir-MediumOblique 6.0 6.0
Avenir-Oblique 6.0 6.0
Avenir-Roman 6.0 6.0
Avenir Next
AvenirNext-Bold 6.0 6.0
AvenirNext-BoldItalic 6.0 6.0
AvenirNext-DemiBold 6.0 6.0
AvenirNext-DemiBoldItalic 6.0 6.0
AvenirNext-Heavy 6.0 6.0
AvenirNext-HeavyItalic 6.0 6.0
AvenirNext-Italic 6.0 6.0
AvenirNext-Medium 6.0 6.0
AvenirNext-MediumItalic 6.0 6.0
AvenirNext-Regular 6.0 6.0
AvenirNext-UltraLight 6.0 6.0
AvenirNext-UltraLightItalic 6.0 6.0
Avenir Next Condensed
AvenirNextCondensed-Bold 6.0 6.0
AvenirNextCondensed-BoldItalic 6.0 6.0
AvenirNextCondensed-DemiBold 6.0 6.0
AvenirNextCondensed-DemiBoldItalic 6.0 6.0
AvenirNextCondensed-Heavy 6.0 6.0
AvenirNextCondensed-HeavyItalic 6.0 6.0
AvenirNextCondensed-Italic 6.0 6.0
AvenirNextCondensed-Medium 6.0 6.0
AvenirNextCondensed-MediumItalic 6.0 6.0
AvenirNextCondensed-Regular 6.0 6.0
AvenirNextCondensed-UltraLight 6.0 6.0
AvenirNextCondensed-UltraLightItalic 6.0 6.0
Bangla Sangam MN
BanglaSangamMN 3.0 4.3
BanglaSangamMN-Bold 3.0 4.3
Baskerville
Baskerville 3.0 4.3
Baskerville-Bold 3.0 4.3
Baskerville-BoldItalic 3.0 4.3
Baskerville-Italic 3.0 4.3
Baskerville-SemiBold 5.0 5.0
Baskerville-SemiBoldItalic 5.0 5.0
Bodoni Ornaments
BodoniOrnamentsITCTT 5.0 4.3
Bodoni 72
BodoniSvtyTwoITCTT-Bold 5.0 4.3
BodoniSvtyTwoITCTT-Book 5.0 4.3
BodoniSvtyTwoITCTT-BookIta 5.0 4.3
Bodoni 72 Oldstyle
BodoniSvtyTwoOSITCTT-Bold 5.0 4.3
BodoniSvtyTwoOSITCTT-Book 5.0 4.3
BodoniSvtyTwoOSITCTT-BookIt 5.0 4.3
BodoniSvtyTwoSCITCTT-Book 5.0 4.3
Bradley Hand
BradleyHandITCTT-Bold 6.0 4.3
Chalkboard SE
ChalkboardSE-Bold 3.0 4.3
ChalkboardSE-Light 5.0 5.0
ChalkboardSE-Regular 3.0 4.3
Chalkduster
Chalkduster 5.0 4.3
Cochin
Cochin 3.0 4.3
Cochin-Bold 3.0 4.3
Cochin-BoldItalic 3.0 4.3
Cochin-Italic 3.0 4.3
Copperplate
Copperplate 5.0 4.3
Copperplate-Bold 5.0 4.3
Copperplate-Light 5.0 5.0
Courier
Courier 3.0 4.3
Courier-Bold 3.0 4.3
Courier-BoldOblique 3.0 4.3
Courier-Oblique 3.0 4.3
Courier New
CourierNewPS-BoldItalicMT 3.0 4.3
CourierNewPS-BoldMT 3.0 4.3
CourierNewPS-ItalicMT 3.0 4.3
CourierNewPSMT 3.0 4.3
DB LCD Temp
DBLCDTempBlack 6.0 3.0 6.0 4.3
Devanagari Sangam MN
DevanagariSangamMN 3.0 4.3
DevanagariSangamMN-Bold 3.0 4.3
Didot
Didot 5.0 4.3
Didot-Bold 5.0 4.3
Didot-Italic 5.0 4.3
Euphemia UCAS
EuphemiaUCAS 5.0 5.0
EuphemiaUCAS-Bold 5.0 5.0
EuphemiaUCAS-Italic 5.0 5.0
Futura
Futura-CondensedExtraBold 3.0 4.3
Futura-CondensedMedium 5.0 5.0
Futura-Medium 3.0 4.3
Futura-MediumItalic 3.0 4.3
Geeza Pro
GeezaPro 3.0 4.3
GeezaPro-Bold 3.0 4.3
Georgia
Georgia 3.0 4.3
Georgia-Bold 3.0 4.3
Georgia-BoldItalic 3.0 4.3
Georgia-Italic 3.0 4.3
Gill Sans
GillSans 5.0 4.3
GillSans-Bold 5.0 4.3
GillSans-BoldItalic 5.0 4.3
GillSans-Italic 5.0 4.3
GillSans-Light 5.0 5.0
GillSans-LightItalic 5.0 5.0
Gujarati Sangam MN
GujaratiSangamMN 3.0 4.3
GujaratiSangamMN-Bold 3.0 4.3
Gurmukhi MN
GurmukhiMN 3.0 4.3
GurmukhiMN-Bold 3.0 4.3
Heiti SC
STHeitiSC-Light 3.0 4.3
STHeitiSC-Medium 3.0 4.3
Heiti TC
STHeitiTC-Light 3.0 4.3
STHeitiTC-Medium 3.0 4.3
Helvetica
Helvetica 3.0 4.3
Helvetica-Bold 3.0 4.3
Helvetica-BoldOblique 3.0 4.3
Helvetica-Light 5.0 5.0
Helvetica-LightOblique 5.0 5.0
Helvetica-Oblique 3.0 4.3
Helvetica Neue
HelveticaNeue 3.0 4.3
HelveticaNeue-Bold 3.0 4.3
HelveticaNeue-BoldItalic 3.0 4.3
HelveticaNeue-CondensedBlack 5.0 5.0
HelveticaNeue-CondensedBold 5.0 5.0
HelveticaNeue-Italic 3.0 4.3
HelveticaNeue-Light 5.0 5.0
HelveticaNeue-LightItalic 5.0 5.0
HelveticaNeue-Medium 5.0 5.0
HelveticaNeue-UltraLight 5.0 5.0
HelveticaNeue-UltraLightItalic 5.0 5.0
Hiragino Kaku Gothic ProN
HiraKakuProN-W3 3.0 4.3
HiraKakuProN-W6 5.0 4.3
Hiragino Mincho ProN
HiraMinProN-W3 3.0 4.3
HiraMinProN-W6 3.0 4.3
Hoefler Text
HoeflerText-Black 5.0 4.3
HoeflerText-BlackItalic 5.0 4.3
HoeflerText-Italic 5.0 4.3
HoeflerText-Regular 5.0 4.3
Kailasa
Kailasa 3.0 4.3
Kailasa-Bold 3.0 4.3
Kannada Sangam MN
KannadaSangamMN 3.0 4.3
KannadaSangamMN-Bold 3.0 4.3
Malayalam Sangam MN
MalayalamSangamMN 3.0 4.3
MalayalamSangamMN-Bold 3.0 4.3
Marion
Marion-Bold 5.0 5.0
Marion-Italic 5.0 5.0
Marion-Regular 5.0 5.0
Marker Felt
MarkerFelt-Thin 3.0 4.3
MarkerFelt-Wide 3.0 4.3
Noteworthy
Noteworthy-Bold 5.0 5.0
Noteworthy-Light 5.0 5.0
Optima
Optima-Bold 5.0 4.3
Optima-BoldItalic 5.0 4.3
Optima-ExtraBlack 5.0 5.0
Optima-Italic 5.0 4.3
Optima-Regular 5.0 4.3
Oriya Sangam MN
OriyaSangamMN 3.0 4.3
OriyaSangamMN-Bold 3.0 4.3
Palatino
Palatino-Bold 3.0 4.3
Palatino-BoldItalic 3.0 4.3
Palatino-Italic 3.0 4.3
Palatino-Roman 3.0 4.3
Papyrus
Papyrus 5.0 4.3
Papyrus-Condensed 3.0 5.0
Party LET
PartyLetPlain 5.0 4.3
Sinhala Sangam MN
SinhalaSangamMN 3.0 4.3
SinhalaSangamMN-Bold 3.0 4.3
Snell Roundhand
SnellRoundhand 3.0 4.3
SnellRoundhand-Black 5.0 5.0
SnellRoundhand-Bold 3.0 4.3
Symbol
Symbol 6.0 6.0
Tamil Sangam MN
TamilSangamMN 3.0 4.3
TamilSangamMN-Bold 3.0 4.3
Telugu Sangam MN
TeluguSangamMN 3.0 4.3
TeluguSangamMN-Bold 3.0 4.3
Thonburi
Thonburi 3.0 4.3
Thonburi-Bold 3.0 4.3
Times New Roman
TimesNewRomanPS-BoldItalicMT 3.0 4.3
TimesNewRomanPS-BoldMT 3.0 4.3
TimesNewRomanPS-ItalicMT 3.0 4.3
TimesNewRomanPSMT 3.0 4.3
Trebuchet MS
Trebuchet-BoldItalic 3.0 4.3
TrebuchetMS 3.0 4.3
TrebuchetMS-Bold 3.0 4.3
TrebuchetMS-Italic 3.0 4.3
Verdana
Verdana 3.0 4.3
Verdana-Bold 3.0 4.3
Verdana-BoldItalic 3.0 4.3
Verdana-Italic 3.0 4.3
Zapf Dingbats
ZapfDingbatsITC 5.0 4.3
Zapfino
Zapfino 3.0 4.3

不管是在C还是C++代码中,typedef这个词都不少见,当然出现频率较高的还是在C代码中。typedef与#define有些相似,但更多的是不同,
特别是在一些复杂的用法上,就完全不同了。

用途一

定义一种类型的别名,而不只是简单的宏替换。可以用作同时声明指针型的多个对象。比如:

1
char* pa, pb; // 这多数不符合我们的意图,它只声明了一个指向字符变量的指针,和一个字符变量;

以下则可行:

1
2
typedef char* PCHAR;
PCHAR pa, pb;

这种用法很有用,特别是char* pa, pb的定义,初学者往往认为是定义了两个字符型指针,其实不是,而用typedef char* PCHAR就不会出现这样的问题,减少了错误的发生。

用途二

用在旧的C代码中,帮助struct。以前的代码中,声明struct新对象时,必须要带上struct,即形式为: struct 结构名对象名,如:

1
2
3
4
5
struct tagPOINT1
{
int x;
int y;
};

struct tagPOINT1 p1;
而在C++中,则可以直接写:结构名对象名,即:tagPOINT1 p1;

1
2
3
4
5
typedef struct tagPOINT
{
int x;
int y;
}POINT;

这样就比原来的方式少写了一个struct,比较省事,尤其在大量使用的时候,

1
POINT p1;

或许,在C++中,typedef的这种用途二不是很大,但是理解了它,对掌握以前的旧代码还是有帮助的,毕竟我们在项目中有可能会遇到较早些年代遗留下来的代码。

用途三

用typedef来定义与平台无关的类型。
比如定义一个叫 REAL 的浮点类型,在目标平台一上,让它表示最高精度的类型为:

1
typedef long double REAL;

在不支持 long double 的平台二上,改为:

1
typedef double REAL;

在连 double 都不支持的平台三上,改为:

1
typedef float REAL;

也就是说,当跨平台时,只要改下 typedef 本身就行,不用对其他源码做任何修改。
标准库就广泛使用了这个技巧,比如size_t。另外,因为typedef是定义了一种类型的新别名,不是简单的字符串替换,所以它比宏来得稳健。
这个优点在我们写代码的过程中可以减少不少代码量哦!

用途四

为复杂的声明定义一个新的简单的别名。方法是:在原来的声明里逐步用别名替换一部
分复杂声明,如此循环,把带变量名的部分留到最后替换,得到的就是原声明的最简化版。
举例:
原声明:void (*b[10]) (void (*)());
变量名为b,先替换右边部分括号里的,pFunParam为别名一:

1
typedef void (*pFunParam)();

再替换左边的变量b,pFunx为别名二:

1
typedef void (*pFunx)(pFunParam);

原声明的最简化版:

1
pFunx b[10];

原声明:doube(*)() (*e)[9];
变量名为e,先替换左边部分,pFuny为别名一:

1
typedef double(*pFuny)();

再替换右边的变量e,pFunParamy为别名二

1
typedef pFuny (*pFunParamy)[9];

原声明的最简化版:

1
pFunParamy e;

理解复杂声明可用的“右左法则”:从变量名看起,先往右,再往左,碰到一个圆括号
就调转阅读的方向;括号内分析完就跳出括号,还是按先右后左的顺序,如此循环,直到整个声明分析完。
举例:

1
int (*func)(int *p);

首先找到变量名func,外面有一对圆括号,而且左边是一个*号,这说明func是一个指针;
然后跳出这个圆括号,先看右边,又遇到圆括号,这说明(*func)是一个函数,所以func是一个指向这类函数的指针,即函数指针,
这类函数具有int*类型的形参,返回值类型是int。

1
int (*func[5])(int *);

func右边是一个[]运算符,说明func是具有5个元素的数组;func的左边有一个*,说明
func的元素是指针(注意这里的*不是修饰func,而是修饰func[5]的,原因是[]运算符
优先级比*高,func先跟[]结合)。跳出这个括号,看右边,又遇到圆括号,说明func数
组的元素是函数类型的指针,它指向的函数具有int*类型的形参,返回值类型为int。

Mac OS X 默认是使用普通睡眠+安全睡眠(在 Windows 中称为休眠)。

安全睡眠会在计算机进入睡眠状态时把内存(RAM)中的数据保存到硬盘上的「/private/var/vm/sleepimage」文件,然后计算机才进入普通睡眠。这样做的好处是如果计算机完全没电了,那原来内存中的内容还可以从硬盘上恢复。

但这样也带来了不利之处,每当进入睡眠的时候都要写入内存容量大小的数据到硬盘上,这无疑延长了进入睡眠所需的时间(通常需要20秒-1分钟);硬盘上还要使用内存容量大小的空间来存储睡眠文件,像笔者的 MacBook 有 4GB 的内存,那就要使用 4GB 的磁盘空间来存储睡眠文件,这对于磁盘空间比较紧张的笔记本用户来说就不太值得了。

可使用如下的命令进行查看:

1
ls -lh /private/var/vm/

可以在「应用程序-实用程序-终端」中使用以下命令禁止安全睡眠:

1
2
$ sudo pmset -a hibernatemode 0
$ sudo nvram “use-nvramrc?”=false

以上设置需要在计算机重启后才能生效。生效后就可以使用下面的命令删除睡眠文件了:

1
$ sudo rm -f /private/var/vm/sleepimage

如果需要恢复安全睡眠,可以使用下面的命令:

1
2
$ sudo pmset -a hibernatemode 3
$ sudo nvram “use-nvramrc?”=true

然后重启计算机即可。

链表实现数据的存储,解决数组存储数据的不便利性

实例代码如下:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* films2.c -- 使用结构链接表 */
#include <stdio.h>
#include <stdlib.h> /* 提供malloc()原型 */
#include <string.h> /* 提供strcpy()原型 */
#define TSIZE 45 /* 存放片名的数组大小 */
struct film
{
char title[TSIZE];
int rating;
struct film *next; /* 指向链表的下一个结构 */
};

int main(void)
{
struct film *head = NULL;
struct film *prev, *current;
char input[TSIZE];

/* 手机并存数信息 */
puts("Enter first movie title: ");
while(gets(input) != NULL && input[0] != '\0')
{
current = (struct film *) malloc(sizeof(struct film));
if(head == NULL) /* 第一个结构 */
{
head = current;
}
else /* 后续结构 */
{
prev->next = current;
}
current->next = NULL;
strcpy(current->title, input);
puts("Enter your rating <0-10>: ");
scanf("%d", &current->rating);
while(getchar() != '\n')
{
continue;
}
puts("Enter next movie title (empty line to stop)");
prev = current;
}

/* 给出电影的列表 */
if(head == NULL)
{
printf("No data entered. ");
}
else
{
printf("Here is the movie list :\n");
}

current = head;
while(current != NULL)
{
printf("Movie: %s Rating: %d\n",current->title,current->rating);
current = current->next;
}
/* 任务完成,因此释放所分配的内存 */
current = head;
while(current != NULL)
{
free(current);
current = current->next;
}
printf("Bye!\n");
}

c代码编译的错误提示:

1
2
3
films1.c:15: error: stray ‘\200’ in program
films1.c:15: error: stray ‘\343’ in program
films1.c:15: error: stray ‘\200’ in program

代码如下:

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
34
35
36
37
38
39
40
41
/* films1.c-- 使用结构数组 */
#include <stdio.h>
#define TSIZE 45 /*存放片名的数组大小*/
#define FMAX 5 /* 对多的影片数 */
struct film{
char title[TSIZE];
int rating;
};
int main(void)
{
struct film movies[FMAX];
int i =0;
int j;
puts("Enter first movie title: ");
while(i < FMAX && gets(movies[i].title) != NULL && movies[i].title != '\0')
{
puts("Enter your rating <0-10>");
scanf("%d",&movies[i++].rating);
while(getchar() != '\n')
{
continue;
}
puts("Enter next movie title (empty line to stop)");
}

if(i == 0)
{
printf("No data entered.");
}
else
{
printf("Here is the movie list: \n");
}
for(j=0;j<i;j++)
{
printf("Movie: %s Rating: %d \n", movies[j].title, movies[i].rating);
}
printf("Bye!\n");

return 0;
}

经过搜索,找出了问题的所在:

这个错误是由于使用了中文引号或其他全角符号,还有一种就是有中文的空格(这个不容易观察),需调到顶格处,再用tab即可。
通过 cat -A可以看到捣乱的字符。
解决方案可以编写脚本过滤字符,看到有人说可以用gedit的替换功能,替换为标准空格。这个方法比较省力一点。尤其是代码较多时。一行一行改的想法还是放弃吧。

安装

一般在CentOS上都自动安装了vsftd,若没有安装则可以使用以下步骤进行安装

1
2
3
yum -y install vsftpd

touch /var/log/vsftpd.log # 创建vsftp的日志文件

在CentOS中,这样就可以完成了一个简单的匿名FTP的搭建。你可以通过访问ftp://yourip 来进行,不过这个FTP没有任何权限。

基于匿名的FTP架设

参考其他关于Vsftpd的CentOS FTP服务配置文章。

3.基于虚拟用户的FTP架设

所谓虚拟用户就是没有使用真实的帐户,只是通过某种手段达到映射帐户和设置权限的目的。

1)我们在/etc/vsftpd/vsftpd.conf中做如下CentOS FTP服务配置:

anonymous_enable=NO 设定不允许匿名访问

chroot_list_enable=YES 使用户不能离开主目录

xferlog_file=/var/log/vsftpd.log 设定vsftpd的服务日志保存路径。注意,该文件默认不存在。必须要手动touch出来

ascii_upload_enable=YES

ascii_download_enable=YES 设定支持ASCII模式的上传和下载功能。

local_enable=YES 设定本地用户可以访问。注:如使用虚拟宿主用户,在该项目设定为NO的情况下所有虚拟用户将无法访问。

pam_service_name=vsftpd PAM认证文件名。PAM将根据/etc/pam.d/vsftpd进行认证

以下这些是关于Vsftpd虚拟用户支持的重要CentOS FTP服务配置项目。默认vsftpd.conf中不包含这些设定项目,需要自己手动添加CentOS FTP服务配置。

guest_enable=YES 设定启用虚拟用户功能。

guest_username=ftp 指定虚拟用户的宿主用户。-CentOS中已经有内置的ftp用户了

user_config_dir=/etc/vsftpd/vuser_conf 设定虚拟用户个人vsftp的CentOS FTP服务文件存放路径。存放虚拟用户个性的CentOS FTP服务文件(配置文件名=虚拟用户名)

2)创建chroot list,将用户ftp加入其中:

1
2
3
touch /etc/vsftpd/chroot_list

echo ftp >> /etc/vsftpd/chroot_list

3)进行认证:

首先,安装Berkeley DB工具,很多人找不到db_load的问题就是没有安装这个包。

1
yum install db4 db4-utils

然后,创建用户密码文本/etc/vsftpd/vuser_passwd.txt ,注意奇行是用户名,偶行是密码

ftpuser1
ftppass1
ftpuser2
ftppass2

接着,生成虚拟用户认证的db文件

1
db_load -T -t hash -f /etc/vsftpd/vuser_passwd.txt /etc/vsftpd/vuser_passwd.db

随后,编辑认证文件/etc/pam.d/vsftpd,全部注释掉原来语句
再增加以下两句

1
2
auth required pam_userdb.so db=/etc/vsftpd/vuser_passwd
account required pam_userdb.so db=/etc/vsftpd/vuser_passwd

最后,创建虚拟用户个性CentOS FTP服务文件

1
2
mkdir /etc/vsftpd/vuser_conf/
vi /etc/vsftpd/vuser_conf/ftpuser1

内容如下:

1
2
3
4
5
6
7
local_root=/opt/var/ftp1 虚拟用户的根目录(根据实际修改)
write_enable=YES 可写
anon_umask=022 掩码
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

启动vsftp服务器

1
2
3
mkdir /opt/var/ftp/ftpuser1
chmod 777 /opt/var/ftp/ftpuser1
service vsftpd start

常见错误:

553 Could not create file

一般都是SELinux的问题,设置SELinux的一个值,重启服务器即可。

1
2
setsebool -P ftpd_disable_trans 1
service vsftpd restart

500 OOPS: bad bool value in config file for: write_enable

注意你的CentOS FTP服务文件中保证每一行最后没有任何空格,一般出错就是在多余的空格上。
更改端口号:listen_port=端口号 (需要自己添加)
欢迎信息:ftpd_banner=欢迎信息
====================================================================
权限问题:
当virtual_use_local_privs=YES时,虚拟用户和本地用户有相同的权限;
当virtual_use_local_privs=NO时,虚拟用户和匿名用户有相同的权限,默认是NO。
当virtual_use_local_privs=YES,write_enable=YES时,虚拟用户具有写权限(上传、下载、删除、重命名)。
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=YES,
anon_upload_enable=YES时,虚拟用户不能浏览目录,只能上传文件,无其他权限。
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_upload_enable=NO时,虚拟用户只能下载文件,无其他权限。
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_upload_enable=YES时,虚拟用户只能上传和下载文件,无其他权限。
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_mkdir_write_enable=YES时,虚拟用户只能下载文件和创建文件夹,无其他权限。
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_other_write_enable=YES时,虚拟用户只能下载、删除和重命名文件,无其他权限。
一些RadHat版本是默认打开SeLinux的。这个东西有加强安全性的同时很讨厌,比如让配置好的vsftpd无法正常登录。

1
#setsebool -P ftpd_disable_trans 1

重启FTP服务~
IP限制的方法

vsftpd中的配置需要 tcp_wrappers=YES

/etc/hosts.allow 中加入允许的IP

1
vsftpd : IP1 IP2 : allow

/etc/hosts.deny 中屏蔽所有IP

1
vsftpd : ALL : deny

重启服务 service xinetd restart (此服务应该开机启动!)

来源:http://blog.sina.com.cn/s/blog\_50e52c230100l9sx.html

本文实现的是一个不用拖控件,而是用代码写出一个按钮,然后点击弹出一个警告信息,有人问那么好的IB工具不用却去苦逼的写代码呢?因为IB高度集成开发工具,拖出的控件帮我省了很大麻烦,这个过程农民工也可以干,但是作为初学者,IB是个比较高层的东西,我们是不是应该了解一下IB底层的东西呢,如果一味追求方便快捷,哪天突然有人问怎么用代码写出来,咱岂不是要被鄙视了;所以吧,初学者不要学懒,多写代码提高我们的编程能力,当我们在开发项目或者在公司工作去用IB,来帮我们节省时间提高效率; 初始化视图代码,绘制了一个距原点(100,100)的140x50像素的按钮,有一点需要注意的是iphone的远点坐标是在左上角,屏幕640x480像素,不过现在用个是Retina分辨率,画质更加细腻;

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
- (void)viewDidLoad
{
[super viewDidLoad];

//设置按钮类型,此处为圆角按钮
UIButton *writeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//设置和大小
CGRect frame = CGRectMake(100.0f, 100.0f, 140.0f, 50.0f);
//将frame的位置大小复制给Button
writeButton.frame = frame;

//给Button添加标题
[writeButton setTitle:@"代码按钮" forState:UIControlStateNormal];
//设置按钮背景颜色
writeButton.backgroundColor = [UIColor clearColor];
//设置按钮标题文字对齐方式,此处为左对齐
writeButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
//使文字距离做边框保持10个像素的距离。
writeButton.contentEdgeInsets = UIEdgeInsetsMake(0,30, 0, 0);

//此处类容目的掩饰代码代码操作按钮一些属性,如果设置按钮背景为图片可以将此处注释取消,注释掉上没横线范围类代码,进行测试
//设置按钮背景图片
UIImage *image= [UIImage imageNamed:@"background.png"];

[writeButton setBackgroundImage:image forState:UIControlStateNormal];
//按钮的相应事件
[writeButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:writeButton];
UIButton *writeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
}

设置按钮类型,按钮类型定义在一个枚举类型中

1
2
3
4
5
6
7
8
typedef enum {
UIButtonTypeCustom = 0, // 没有风格
UIButtonTypeRoundedRect, // 圆角风格按钮
UIButtonTypeDetailDisclosure, // 详情信息按钮
UIButtonTypeInfoLight, // 明亮背景的信息按钮
UIButtonTypeInfoDark, // 黑暗背景的信息按钮
UIButtonTypeContactAdd, // 添加按钮
} UIButtonType;

但是考虑的ios开发中,为了界面美观一般设置背景图片,代替按钮的标题设置,此处推荐一个所搜icon的网址,里面有基本用的icon素材,个人觉得不错,给分享下 http://www.easyicon.cn/ 点击打开链接 (已经不存在了);

在点击按钮是按钮是凹下去,然后弹起才触发起事件,按钮的状态有:

1
2
3
4
5
6
7
8
9
UIControlEventTouchDown // 按下 
UIControlEventTouchDownRepeat // 多次按下
UIControlEventTouchDragInside // 保持按下然后在按钮及其一定的外围拖动
UIControlEventTouchDragOutside // 保持按下,在按钮外面拖动
UIControlEventTouchDragEnter // DragOutside进入DragInside触发
UIControlEventTouchDragExit // in到out触发
UIControlEventTouchUpInside // 在按钮及其一定外围内松开
UIControlEventTouchUpOutside // 按钮外面松开
UIControlEventTouchCancel // 点击取消

弹出一个警告,一般都这样写

1
2
3
4
5
-(void) buttonClicked:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了一个按钮" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}

此处弹出的一个警告,主要用到UIAlertView这个类,initWithTitle初始化标题,message是弹出警告类容,提示你做了什么事,delegate是委托代理,此处不需要其他类做什么事,自个完全能搞定,所以设置为self,类似于C++中的this指针,cancelButtonTitle这个一看就能明白,取消按钮的标题是什么了,otherButtonTitles设置其他按钮,也就是说你需要更多按钮支持的时候,此处不需要,只要一个nil就好了,就如还需要其他的,你可以添加代码假如:otherButtonTitles:@”test1”,@”test2”,@”test3”,@”test4”,nil

在iOS开发过程中,会遇到这样的问题,就是调试。但是在对于一个初学者的方式是,去搜索,搜索来的结果大部分都是gdb的命令(command),但是我的xcode显示的是lldb,这就难道我了。于是我在搜索lldb和gdb就出结果了。其实是有对照表的,下面给出链接

http://lldb.llvm.org/lldb-gdb.html

GDB to LLDB Command Map

Below is a table of GDB commands with the LLDB counterparts. The built in GDB-compatibility aliases in LLDB are also listed. The full lldb command names are often long, but any unique short form can be used. Instead of “breakpoint set“, “br se“ is also acceptable.

Execution Commands

GDB LLDB
Launch a process no arguments.
(gdb) run (gdb) r (lldb) process launch (lldb) run (lldb) r
Launch a process with arguments.
(gdb) run (gdb) r (lldb) process launch – (lldb) r
Launch a process for with arguments a.out 1 2 3 without having to supply the args every time.
% gdb –args a.out 1 2 3 (gdb) run … (gdb) run … % lldb – a.out 1 2 3 (lldb) run … (lldb) run …
Launch a process with arguments in new terminal window (Mac OS X only).
(lldb) process launch –tty – (lldb) pro la -t –
Launch a process with arguments in existing terminal /dev/ttys006 (Mac OS X only).
(lldb) process launch –tty=/dev/ttys006 – (lldb) pro la -t/dev/ttys006 –
Set environment variables for process before launching.
(gdb) set env DEBUG 1 (lldb) settings set target.env-vars DEBUG=1 (lldb) set se target.env-vars DEBUG=1 (lldb) env DEBUG=1
Set environment variables for process and launch process in one command.
(lldb) process launch -v DEBUG=1
Attach to a process with process ID 123.
(gdb) attach 123 (lldb) process attach –pid 123 (lldb) attach -p 123
Attach to a process named “a.out”.
(gdb) attach a.out (lldb) process attach –name a.out (lldb) pro at -n a.out
Wait for a process named “a.out” to launch and attach.
(gdb) attach -waitfor a.out (lldb) process attach –name a.out –waitfor (lldb) pro at -n a.out -w
Attach to a remote gdb protocol server running on system “eorgadd”, port 8000.
(gdb) target remote eorgadd:8000 (lldb) gdb-remote eorgadd:8000
Attach to a remote gdb protocol server running on the local system, port 8000.
(gdb) target remote localhost:8000 (lldb) gdb-remote 8000
Attach to a Darwin kernel in kdp mode on system “eorgadd”.
(gdb) kdp-reattach eorgadd (lldb) kdp-remote eorgadd
Do a source level single step in the currently selected thread.
(gdb) step (gdb) s (lldb) thread step-in (lldb) step (lldb) s
Do a source level single step over in the currently selected thread.
(gdb) next (gdb) n (lldb) thread step-over (lldb) next (lldb) n
Do an instruction level single step in the currently selected thread.
(gdb) stepi (gdb) si (lldb) thread step-inst (lldb) si
Do an instruction level single step over in the currently selected thread.
(gdb) nexti (gdb) ni (lldb) thread step-inst-over (lldb) ni
Step out of the currently selected frame.
(gdb) finish (lldb) thread step-out (lldb) finish
Return immediately from the currently selected frame, with an optional return value.
(gdb) return (lldb) thread return
Backtrace and disassemble every time you stop.
(lldb) target stop-hook add Enter your stop hook command(s). Type ‘DONE’ to end. > bt > disassemble –pc > DONE Stop hook #1 added.

Breakpoint Commands

GDB LLDB
Set a breakpoint at all functions named main.
(gdb) break main (lldb) breakpoint set –name main (lldb) br s -n main (lldb) b main
Set a breakpoint in file test.c at line 12.
(gdb) break test.c:12 (lldb) breakpoint set –file test.c –line 12 (lldb) br s -f test.c -l 12 (lldb) b test.c:12
Set a breakpoint at all C++ methods whose basename is main.
(gdb) break main (Hope that there are no C funtions named main). (lldb) breakpoint set –method main (lldb) br s -M main
Set a breakpoint at and object C function: -[NSString stringWithFormat:].
(gdb) break -[NSString stringWithFormat:] (lldb) breakpoint set –name “-[NSString stringWithFormat:]” (lldb) b -[NSString stringWithFormat:]
Set a breakpoint at all Objective C methods whose selector is count.
(gdb) break count (Hope that there are no C or C++ funtions named count). (lldb) breakpoint set –selector count (lldb) br s -S count
Set a breakpoint by regular expression on function name.
(gdb) rbreak regular-expression (lldb) breakpoint set –func-regex regular-expression (lldb) br s -r regular-expression
Ensure that breakpoints by file and line work for #included .c/.cpp/.m files.
(gdb) b foo.c:12 (lldb) settings set target.inline-breakpoint-strategy always (lldb) br s -f foo.c -l 12
Set a breakpoint by regular expression on source file contents.
(gdb) shell grep -e -n pattern source-file (gdb) break source-file:CopyLineNumbers (lldb) breakpoint set –source-pattern regular-expression –file SourceFile (lldb) br s -p regular-expression -f file
List all breakpoints.
(gdb) info break (lldb) breakpoint list (lldb) br l
Delete a breakpoint.
(gdb) delete 1 (lldb) breakpoint delete 1 (lldb) br del 1

Watchpoint Commands

GDB LLDB
Set a watchpoint on a variable when it is written to.
(gdb) watch global_var (lldb) watchpoint set variable global_var (lldb) wa s v global_var
Set a watchpoint on a memory location when it is written into. The size of the region to watch for defaults to the pointer size if no ‘-x byte_size’ is specified. This command takes raw input, evaluated as an expression returning an unsigned integer pointing to the start of the region, after the ‘–’ option terminator.
(gdb) watch -location g_char_ptr (lldb) watchpoint set expression – my_ptr (lldb) wa s e – my_ptr
Set a condition on a watchpoint.
(lldb) watch set var global (lldb) watchpoint modify -c ‘(global==5)’ (lldb) c … (lldb) bt * thread #1: tid = 0x1c03, 0x0000000100000ef5 a.outmodify + 21 at main.cpp:16, stop reason = watchpoint 1 frame #0: 0x0000000100000ef5 a.outmodify + 21 at main.cpp:16 frame #1: 0x0000000100000eac a.outmain + 108 at main.cpp:25 frame #2: 0x00007fff8ac9c7e1 libdyld.dylibstart + 1 (lldb) frame var global (int32_t) global = 5
List all watchpoints.
(gdb) info break (lldb) watchpoint list (lldb) watch l
Delete a watchpoint.
(gdb) delete 1 (lldb) watchpoint delete 1 (lldb) watch del 1

Examining Variables

GDB LLDB
Show the arguments and local variables for the current frame.
(gdb) info args and (gdb) info locals (lldb) frame variable (lldb) fr v
Show the local variables for the current frame.
(gdb) info locals (lldb) frame variable –no-args (lldb) fr v -a
Show the contents of local variable “bar”.
(gdb) p bar (lldb) frame variable bar (lldb) fr v bar (lldb) p bar
Show the contents of local variable “bar” formatted as hex.
(gdb) p/x bar (lldb) frame variable –format x bar (lldb) fr v -f x bar
Show the contents of global variable “baz”.
(gdb) p baz (lldb) target variable baz (lldb) ta v baz
Show the global/static variables defined in the current source file.
n/a (lldb) target variable (lldb) ta v
Display a the variable “argc” and “argv” every time you stop.
(gdb) display argc (gdb) display argv (lldb) target stop-hook add –one-liner “frame variable argc argv” (lldb) ta st a -o “fr v argc argv” (lldb) display argc (lldb) display argv
Display a the variable “argc” and “argv” only when you stop in the function named main.
(lldb) target stop-hook add –name main –one-liner “frame variable argc argv” (lldb) ta st a -n main -o “fr v argc argv”
Display the variable “*this” only when you stop in c class named MyClass.
(lldb) target stop-hook add –classname MyClass –one-liner “frame variable *this” (lldb) ta st a -c MyClass -o “fr v *this”

Evaluating expressions

GDB LLDB
Evaluating a generalized expression in the current frame.
(gdb) print (int) printf (“Print nine: %d.”, 4 + 5) or if you don’t want to see void returns: (gdb) call (int) printf (“Print nine: %d.”, 4 + 5) (lldb) expr (int) printf (“Print nine: %d.”, 4 + 5) or using the print alias: (lldb) print (int) printf (“Print nine: %d.”, 4 + 5)
Creating and assigning a value to a convenience variable.
(gdb) set $foo = 5 (gdb) set variable $foo = 5 or using the print command (gdb) print $foo = 5 or using the call command (gdb) call $foo = 5 and if you want to specify the type of the variable: (gdb) set $foo = (unsigned int) 5 In lldb you evaluate a variable declaration expression as you would write it in C: (lldb) expr unsigned int $foo = 5
Printing the ObjC “description” of an object.
(gdb) po [SomeClass returnAnObject] (lldb) expr -o – [SomeClass returnAnObject] or using the po alias: (lldb) po [SomeClass returnAnObject]
Print the dynamic type of the result of an expression.
(gdb) set print object 1 (gdb) p someCPPObjectPtrOrReference only works for C++ objects. (lldb) expr -d 1 – [SomeClass returnAnObject] (lldb) expr -d 1 – someCPPObjectPtrOrReference or set dynamic type printing to be the default: (lldb) settings set target.prefer-dynamic run-target
Calling a function so you can stop at a breakpoint in the function.
(gdb) set unwindonsignal 0 (gdb) p function_with_a_breakpoint() (lldb) expr -i 0 – function_with_a_breakpoint()
Calling a function that crashes, and stopping when the function crashes.
(gdb) set unwindonsignal 0 (gdb) p function_which_crashes() (lldb) expr -u 0 – function_which_crashes()

Examining Thread State

GDB LLDB
Show the stack backtrace for the current thread.
(gdb) bt (lldb) thread backtrace (lldb) bt
Show the stack backtraces for all threads.
(gdb) thread apply all bt (lldb) thread backtrace all (lldb) bt all
Backtrace the first five frames of the current thread.
(gdb) bt 5 (lldb) thread backtrace -c 5 (lldb) bt 5 (lldb-169 and later) (lldb) bt -c 5 (lldb-168 and earlier)
Select a different stack frame by index for the current thread.
(gdb) frame 12 (lldb) frame select 12 (lldb) fr s 12 (lldb) f 12
List information about the currently selected frame in the current thread.
(lldb) frame info
Select the stack frame that called the current stack frame.
(gdb) up (lldb) up (lldb) frame select –relative=1
Select the stack frame that is called by the current stack frame.
(gdb) down (lldb) down (lldb) frame select –relative=-1 (lldb) fr s -r-1
Select a different stack frame using a relative offset.
(gdb) up 2 (gdb) down 3 (lldb) frame select –relative 2 (lldb) fr s -r2 (lldb) frame select –relative -3 (lldb) fr s -r-3
Show the general purpose registers for the current thread.
(gdb) info registers (lldb) register read
Write a new decimal value ‘123’ to the current thread register ‘rax’.
(gdb) p $rax = 123 (lldb) register write rax 123
Skip 8 bytes ahead of the current program counter (instruction pointer). Note that we use backticks to evaluate an expression and insert the scalar result in LLDB.
(gdb) jump *$pc+8 (lldb) register write pc $pc+8
Show the general purpose registers for the current thread formatted as signed decimal. LLDB tries to use the same format characters as printf(3) when possible. Type “help format” to see the full list of format specifiers.
(lldb) register read –format i (lldb) re r -f i LLDB now supports the GDB shorthand format syntax but there can’t be space after the command: (lldb) register read/d
Show all registers in all register sets for the current thread.
(gdb) info all-registers (lldb) register read –all (lldb) re r -a
Show the values for the registers named “rax”, “rsp” and “rbp” in the current thread.
(gdb) info all-registers rax rsp rbp (lldb) register read rax rsp rbp
Show the values for the register named “rax” in the current thread formatted as binary.
(gdb) p/t $rax (lldb) register read –format binary rax (lldb) re r -f b rax LLDB now supports the GDB shorthand format syntax but there can’t be space after the command: (lldb) register read/t rax (lldb) p/t $rax
Read memory from address 0xbffff3c0 and show 4 hex uint32_t values.
(gdb) x/4xw 0xbffff3c0 (lldb) memory read –size 4 –format x –count 4 0xbffff3c0 (lldb) me r -s4 -fx -c4 0xbffff3c0 (lldb) x -s4 -fx -c4 0xbffff3c0 LLDB now supports the GDB shorthand format syntax but there can’t be space after the command: (lldb) memory read/4xw 0xbffff3c0 (lldb) x/4xw 0xbffff3c0 (lldb) memory read –gdb-format 4xw 0xbffff3c0
Read memory starting at the expression “argv[0]”.
(gdb) x argv[0] (lldb) memory read argv[0] NOTE: any command can inline a scalar expression result (as long as the target is stopped) using backticks around any expression: (lldb) memory read –size sizeof(int) argv[0]
Read 512 bytes of memory from address 0xbffff3c0 and save results to a local file as text.
(gdb) set logging on (gdb) set logging file /tmp/mem.txt (gdb) x/512bx 0xbffff3c0 (gdb) set logging off (lldb) memory read –outfile /tmp/mem.txt –count 512 0xbffff3c0 (lldb) me r -o/tmp/mem.txt -c512 0xbffff3c0 (lldb) x/512bx -o/tmp/mem.txt 0xbffff3c0
Save binary memory data starting at 0x1000 and ending at 0x2000 to a file.
(gdb) dump memory /tmp/mem.bin 0x1000 0x2000 (lldb) memory read –outfile /tmp/mem.bin –binary 0x1000 0x1200 (lldb) me r -o /tmp/mem.bin -b 0x1000 0x1200
Get information about a specific heap allocation (available on Mac OS X only).
(gdb) info malloc 0x10010d680 (lldb) script import lldb.macosx.heap (lldb) process launch –environment MallocStackLogging=1 – [ARGS] (lldb) malloc_info –stack-history 0x10010d680
Get information about a specific heap allocation and cast the result to any dynamic type that can be deduced (available on Mac OS X only)
(lldb) script import lldb.macosx.heap (lldb) malloc_info –type 0x10010d680
Find all heap blocks that contain a pointer specified by an expression EXPR (available on Mac OS X only).
(lldb) script import lldb.macosx.heap (lldb) ptr_refs EXPR
Find all heap blocks that contain a C string anywhere in the block (available on Mac OS X only).
(lldb) script import lldb.macosx.heap (lldb) cstr_refs CSTRING
Disassemble the current function for the current frame.
(gdb) disassemble (lldb) disassemble –frame (lldb) di -f
Disassemble any functions named main.
(gdb) disassemble main (lldb) disassemble –name main (lldb) di -n main
Disassemble an address range.
(gdb) disassemble 0x1eb8 0x1ec3 (lldb) disassemble –start-address 0x1eb8 –end-address 0x1ec3 (lldb) di -s 0x1eb8 -e 0x1ec3
Disassemble 20 instructions from a given address.
(gdb) x/20i 0x1eb8 (lldb) disassemble –start-address 0x1eb8 –count 20 (lldb) di -s 0x1eb8 -c 20
Show mixed source and disassembly for the current function for the current frame.
n/a (lldb) disassemble –frame –mixed (lldb) di -f -m
Disassemble the current function for the current frame and show the opcode bytes.
n/a (lldb) disassemble –frame –bytes (lldb) di -f -b
Disassemble the current source line for the current frame.
n/a (lldb) disassemble –line (lldb) di -l

Executable and Shared Library Query Commands

GDB LLDB
List the main executable and all dependent shared libraries.
(gdb) info shared (lldb) image list
Look up information for a raw address in the executable or any shared libraries.
(gdb) info symbol 0x1ec4 (lldb) image lookup –address 0x1ec4 (lldb) im loo -a 0x1ec4
Look up functions matching a regular expression in a binary.
(gdb) info function <FUNC_REGEX> This one finds debug symbols: (lldb) image lookup -r -n <FUNC_REGEX> This one finds non-debug symbols: (lldb) image lookup -r -s <FUNC_REGEX> Provide a list of binaries as arguments to limit the search.
Find full souce line information.
(gdb) info line 0x1ec4 This one is a bit messy at present. Do: (lldb) image lookup -v –address 0x1ec4 and look for the LineEntry line, which will have the full source path and line range information.
Look up functions matching a regular expression in a binary.
(gdb) info function <FUNC_REGEX> This one finds debug symbols: (lldb) image lookup -r -n <FUNC_REGEX> This one finds non-debug symbols: (lldb) image lookup -r -s <FUNC_REGEX> Provide a list of binaries as arguments to limit the search.
Look up information for an address in a.out only.
(lldb) image lookup –address 0x1ec4 a.out (lldb) im loo -a 0x1ec4 a.out
Look up information for for a typePointby name.
(gdb) ptype Point (lldb) image lookup –type Point (lldb) im loo -t Point
Dump all sections from the main executable and any shared libraries.
(gdb) maintenance info sections (lldb) image dump sections
Dump all sections in the a.out module.
(lldb) image dump sections a.out
Dump all symbols from the main executable and any shared libraries.
(lldb) image dump symtab
Dump all symbols in a.out and liba.so.
(lldb) image dump symtab a.out liba.so

Miscellaneous

GDB LLDB
Echo text to the screen.
(gdb) echo Here is some text\n (lldb) script print “Here is some text”
Remap source file pathnames for the debug session. If your source files are no longer located in the same location as when the program was built — maybe the program was built on a different computer — you need to tell the debugger how to find the sources at their local file path instead of the build system’s file path.
(gdb) set pathname-substitutions /buildbot/path /my/path (lldb) settings set target.source-map /buildbot/path /my/path
Supply a catchall directory to search for source files in.
(gdb) directory /my/path (No equivalent command yet.)

做个备份吧,呵呵,希望没有侵权

0%